User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 402,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,771 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 632 | Replies: 4
Reply
Join Date: Jul 2008
Posts: 2
Reputation: elliaw is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
elliaw elliaw is offline Offline
Newbie Poster

How can I speed up multi select query?

  #1  
Jul 11th, 2008
Hi,

Can anyone help? I have 2 table each over 2M records and it is taking ages retrieving records.

1. A 'product' table that contains all products in store.
2. The second table is the 'offer' table which contains all offers related to the products (e.g price,store,product condition).

1. 'product' table fields
[pid, country, category, image]

2. 'offer' table fields
[offer_id, pid, store_id, condition, price, title, stock]

3. I need to get the image, min(price) and title for each new product.

Current Query:
  1. SELECT * FROM product x, offer y
  2. WHERE x.pid=y.pid AND x.country = 'US' AND x.category LIKE 'auto|accessories' AND y.condition = 'new'
  3. AND y.price=( SELECT min(z.price) FROM pdt_offer z
  4. WHERE z.pid=y.pid AND z.quality='new' AND z.stock>0)
  5. AND y.stock >0
  6. GROUP BY x.pid

Is there a better way to do this?

Thanks

Ee Loon
Last edited by peter_budo : Jul 11th, 2008 at 6:29 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 40
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: How can I speed up multi select query?

  #2  
Jul 11th, 2008
"SELECT *" e.g. select everything is always a bad idea.

To speed up, select only the things you want/need.

WOW! I look again at your query and cannot believe my eyes because you select everything (*) from two tables!

Things you need to do:
1) Select what you want: e.g. "SELECT x.id,y.pid FROM blabla..."
2) Use indexes to speed up you queries, for more info about indexes look here:
- http://dev.mysql.com/doc/refman/5.0/...l-indexes.html
- http://www.tutorialized.com/tutorial...d-Indexes/7139
- and you'd better look here: http://www.tizag.com/mysqlTutorial/mysql-index.php
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: How can I speed up multi select query?

  #3  
Jul 12th, 2008
Hello elliaw,

Originally Posted by elliaw View Post
SELECT * FROM product x, offer y
WHERE x.pid=y.pid AND x.country = 'US' AND x.category like 'auto|accessories' AND y.condition = 'new'
AND y.price=( select min(z.price) from pdt_offer z
where z.pid=y.pid AND z.quality='new' AND z.stock>0)
AND y.stock >0
GROUP BY x.pid

Actually, this query is wrong! It should not work! Because of * (the sign of slackers) more than one column will be selected. Therefore, GROUP BY clause must contain more than as x.pid. If you don't get an error message, something is wrong with your data base system.

>>> 3. I need to get the image, min(price) and title for each new product.

It is a very good idea to follow kvdd's advice !

So select these three columns only and put them into GROUP BY clause. But attention: I really doubt heavily that that mysql would be able to manage an image (what type, a blob type?) in that place!

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: elliaw is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
elliaw elliaw is offline Offline
Newbie Poster

Re: How can I speed up multi select query?

  #4  
Jul 13th, 2008
Thanks guys,

I did read up the articles as suggested and I am able to simplify the query and select only fields that is required.

Reply With Quote  
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

Re: How can I speed up multi select query?

  #5  
Jul 13th, 2008
Actually I think the lowest part would the LIKE clause... isn't it?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 7:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC