If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Lot of small queries or a Large 1

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-09, 11:39
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
Lot of small queries or a Large 1

Hi

I am creating a searching api for an e-shop with a mysql db.

I use these tables from the db
1)Products
1)Manufacturers_xref_prods
2)Prices_xref_prods
3)Attributes_xref_prods

each of these has a "product_id" column.
I need to retreive the common product_id's from the selected by the user Manufacturer,Price....etc

I have 2 options to do that
1) Run small queries that refers on 1 table for each of the above tables

(SELECT product_id FROM Manufacturers_xref_prods WHERE manuf_id=5)
and then find the common product_id's beetween the tables with php.

2) Run a large query that returns the desired results.

SELECT Products.product_id FROM Products WHERE Products.product_id IN(SELECT product_id FROM Manufacturers_xref_prods WHERE manuf_id=5) AND Products.product_id IN(SELECT product_id FROM Prices_xref_prods WHERE price_id=6) ;

Does anyone knows Which option offers better query performance?
Reply With Quote
  #2 (permalink)  
Old 11-27-09, 15:57
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
Quote:
I am creating a searching api for an e-shop with a mysql db.
Is this going to be an API used by many apps, or is it a purpose-built system?

Quote:
I need to retreive the common product_id's from the selected by the user Manufacturer,Price....etc
Your first option looks very much like you're trying to do the join in your app. Even if you could write a better join algorithm than the MySQL team, you'll be making a huge number of requests to the DBMS and incurring a lot of latency.

Code:
SELECT Products.product_id FROM Products WHERE Products.product_id IN (SELECT product_id FROM Manufacturers_xref_prods WHERE manuf_id=5) AND Products.product_id IN(SELECT product_id FROM Prices_xref_prods WHERE price_id=6) ;
Those subqueries are entirely equivalent to:

Code:
SELECT Products.product_id 
FROM Products p INNER JOIN Manudacturers m
ON p.product_id = m.product_id
INNER JOIN Prices_xref_prods x
ON p.product_id = x.product_id
WHERE m.manuf_id = 5 AND x.price_id = 6
Now, which will perform better? It depends on the optimizer. A good optimizer can recognize a subquery and convert it to the equivalent join. It's unlikely that the join will perform worse than the subquery, unless the joins aren't ordered well and confuse the optimizer.

Generally, try to make your queries readable and minimize the amount of logic in your application. Unless rewriting a query gives you an order of magnitude increase in performance, you may find some future upgrade will make all your careful optimizations obsolete.

Last edited by sco08y; 11-27-09 at 16:05.
Reply With Quote
  #3 (permalink)  
Old 12-02-09, 09:53
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
Thank you very much

Yes the api will work for a lot aplications.
1) Joins perform better even if there are no indexes in tables ?
2) Could you suggest me an optimizer or a way to optimize my queries ?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On