Solved

Help optimizing this query...

Posted on 2011-03-11
6
278 Views
Last Modified: 2012-05-11
SELECT p.*, pa.`id_product_attribute`, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL 20 DAY)) > 0 AS new,
(p.`price` * ((100 + (t.`rate`))/100) - IF((DATEDIFF(`reduction_from`, CURDATE()) <= 0 AND DATEDIFF(`reduction_to`, CURDATE()) >=0) OR `reduction_from` = `reduction_to`, IF(`reduction_price` > 0, `reduction_price`, (p.`price` * ((100 + (t.`rate`))/100) * `reduction_percent` / 100)),0)) AS orderprice
FROM `ps_category_product` cp
LEFT JOIN `ps_product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `ps_product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
LEFT JOIN `ps_category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = 1)
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = 1)
LEFT JOIN `ps_image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `ps_image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = 1)
LEFT JOIN `ps_tax` t ON t.`id_tax` = p.`id_tax`
LEFT JOIN `ps_tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = 1)
LEFT JOIN `ps_manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` = 14 AND p.`active` = 1
ORDER BY `orderprice` ASC
LIMIT 10,10

Open in new window

Here is the EXPLAIN:
"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
"1","SIMPLE","pa","system","product_attribute_product,product_default",NULL,NULL,NULL,"0","const row not found"
"1","SIMPLE","cp","range","category_product_index,id_product","category_product_index","4",NULL,"135000","Using where; Using index; Using temporary; Using filesort"
"1","SIMPLE","p","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.cp.id_product","1","Using where"
"1","SIMPLE","cl","eq_ref","category_lang_index","category_lang_index","8","partrepo_store.p.id_category_default,const","1",""
"1","SIMPLE","pl","eq_ref","product_lang_index,id_lang","product_lang_index","8","partrepo_store.p.id_product,const","1",""
"1","SIMPLE","i","ref","image_product,product_position,id_product_cover","id_product_cover","5","partrepo_store.p.id_product,const","12",""
"1","SIMPLE","il","eq_ref","image_lang_index,id_image","image_lang_index","8","partrepo_store.i.id_image,const","1",""
"1","SIMPLE","t","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.p.id_tax","1",""
"1","SIMPLE","tl","eq_ref","tax_lang_index","tax_lang_index","8","partrepo_store.t.id_tax,const","1",""
"1","SIMPLE","m","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.p.id_manufacturer","1",""

Open in new window

table                         #rows
----------------------------  -----------
ps_category_product           342,386
ps_product                    330,111
ps_product_attribute          0
ps_category_lang              12
ps_product_lang               1,027,155
ps_image                      342,384
ps_image_lang                 1,027,149
ps_tax                        1
ps_tax_lang                   3
ps_manufacturer               1

Open in new window

What can be done to optimize this, it currently takes 56 seconds to run even when there is a limit.
0
Comment
Question by:EddieShipman
  • 5
6 Comments
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112651
Seems that the ORDER BY calculated value is what is causing the problem.
If I run the same query without the ORDER BY it runs in < 1 sec.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112682
This query, when run on my host returns this:
Incorrect key file for table '/tmp/#sql_15b5_0.MYI'; try to repair it
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112719
Well, it's not the calculated column causing the problem. It also runs very slow when I change the ORDER BY to quantity
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 38

Accepted Solution

by:
Aaron Tomosky earned 500 total points
ID: 35114471
Take out the order by, wrap the whole thing in
Select * from (
--current query
) order by quantity

This way it just sorts the output instead of the whole thing
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35114551
Holy bejeezus. What a difference. Thanks a million.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 35114553
Excellent
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now