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 > query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-03-10, 14:03
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
query help

Hello, my mysql knowledge is limited and after doing some reading, tests I am unable to find any solution and would appreciate any help. The task is to find products that with specific tags.

The main products table

CREATE TABLE IF NOT EXISTS `ds_item` (
`iid` int(11) NOT NULL auto_increment,
`status` int(5) NOT NULL default '0',
`title` varchar(255) NOT NULL default '',
`date` int(11) NOT NULL default '0',
`dupdate` int(11) NOT NULL default '0',
`sale` smallint(2) NOT NULL default '0',
`price_list` varchar(50) NOT NULL default '0.00',
`price_sale` varchar(50) NOT NULL default '0.00',
`price_sale_avg` decimal(15,2) NOT NULL default '0.00',
`price_save` decimal(15,2) NOT NULL default '0.00',
`store_id` int(11) NOT NULL default '0',
`pid` varchar(110) NOT NULL,
`sku` varchar(100) NOT NULL,
`upc` varchar(100) NOT NULL,
`url` text NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`iid`),
KEY `store_id` (`store_id`),
KEY `status` (`status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2124683 ;


tags' table

CREATE TABLE IF NOT EXISTS `ds_item_cat` (
`rel_id` int(11) NOT NULL auto_increment,
`iid` int(11) NOT NULL default '0',
`cat` int(11) NOT NULL default '0',
PRIMARY KEY (`rel_id`),
KEY `iid_cat` (`iid`,`cat`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6127161 ;


stores' table

CREATE TABLE IF NOT EXISTS `ds_store` (
`store_id` int(11) NOT NULL auto_increment,
`store_name` varchar(255) NOT NULL default '',
`store_status` smallint(3) NOT NULL default '0',
`currency` char(3) NOT NULL default 'usd',
`note` varchar(255) NOT NULL,
PRIMARY KEY (`store_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=539 ;


now a sample query to get Women's Black Steve Madden Sandals

SELECT ds_item.iid, title, sale, price_list, price_sale, price_sale_avg, ds_item.store_id, description, store_name, currency FROM ds_item
INNER JOIN ds_item_cat AS c1 ON ds_item.iid=c1.iid
INNER JOIN ds_item_cat AS gender ON ds_item.iid=gender.iid AND gender.cat='4118'
INNER JOIN ds_item_cat AS brand ON ds_item.iid=brand.iid AND brand.cat='715'
INNER JOIN ds_item_cat AS color ON ds_item.iid=color.iid AND color.cat='4127'
LEFT JOIN ds_store ON (ds_item.store_id=ds_store.store_id)
WHERE status='0' AND c1.cat IN ('4160')
GROUP BY ds_item.iid
ORDER BY sale DESC, ds_item.dupdate DESC
LIMIT 0, 10

The problem: when I run explain on this query it shows that about 93947 rows need to be scanned. Any way to avoid this by optimizing the query, tables? Thank you.


the results of explain

http://img218.imageshack.us/img218/8274/explain.gif
Reply With Quote
  #2 (permalink)  
Old 03-03-10, 14:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
how many rows have status 0 versus other status values?

why are you using GROUP BY if there is no aggregation (SUM or COUNT) involved?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-03-10, 15:02
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
Thank you r937, you are right, the group by needs to be removed, will do it now.

159,519 rows have status 0 (0=active/in stock items)
318,117 rows are status 1 (out of stock items)
the rest are status 2 (soft deleted)
Reply With Quote
  #4 (permalink)  
Old 03-03-10, 15:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
and did the EXPLAIN improve without the GROUP BY?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-03-10, 16:11
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
no looks the same, 94007 rows are being scanned without the group by.


here are the results of the explain without the group by:

http://img34.imageshack.us/img34/7846/explainm.gif
Reply With Quote
  #6 (permalink)  
Old 03-03-10, 16:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by virap View Post
no looks the same, 94007 rows are being scanned without the group by.
not using temporary any more, though, is it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-03-10, 16:29
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
You are correct, sorry I completely missed that. That is a big improvement then, thank you.

Overall, do I have everything now correct? Is it normal for the query to scan so many rows just to return few?

Last edited by virap; 03-03-10 at 16:35.
Reply With Quote
  #8 (permalink)  
Old 03-03-10, 16:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by virap View Post
Is it normal for the query to scan so many rows just to return few?
ask yourself where the 10 biggest sale items are likely to be located within all hunnert thousand instock items
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 03-03-10, 16:54
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
do you mean "ORDER BY sale DESC" is causing all these rows to be scanned? If so I tried it without sort too, but it keeps scanning 94000+ rows.
Reply With Quote
  #10 (permalink)  
Old 03-03-10, 17:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by virap View Post
If so I tried it without sort too, but it keeps scanning 94000+ rows.
well, the omission of the ORDER BY clause would make your query results hardly useful at all, right?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 03-03-10, 17:41
virap virap is offline
Registered User
 
Join Date: Mar 2010
Posts: 6
Yes, but it still scans 94K rows either way, with our without the order part.
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