 | 
August 6th, 2008, 10:50 PM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California Age: 23
Posts: 193
| | query takes 4 secs to run... any streamlining suggestions?
hi, i have this query that works perfectly aside from the 4 sec load times. this query is for a webstore that loads the 10most popular items purchased within the last week whose status meets a few conditions in the products table and compares it with the sums of the total quantity purchased... there isn't any way i can restructure this as to speed the query time is there? i'm specifically wondering if there is a better way to search for orders within the last week... as it is, i am using a subquery and TO_DAYS - SELECT t.`prod_id` , p.`name` , p.`category` , SUM( t.`prod_qty` ) AS `SumOfprod_qty`
-
FROM `store_products` p
-
INNER JOIN `store_tranlog` t ON p.`id` = t.`prod_id`
-
WHERE p.`wholesale_tf` = "1"
-
AND t.`status` > "0"
-
AND p.`status` = "1"
-
AND p.`catalog` = "NON"
-
AND TO_DAYS( NOW( ) ) -7 < (
-
SELECT TO_DAYS( MAX( `date` ) ) AS `seven`
-
FROM `store_products` )
-
GROUP BY `prod_id`
-
ORDER BY `SumOfprod_qty` DESC
-
LIMIT 0 , 10
i suppose i could restructure the tables but seeing as i wasn't the one who originally designed the store there may be more problems than i can imagine with this idea. the only other option would be to use a bunch of UPDATE queries that ran all the time but i'm hoping i can find a quick fix somehow. thanks for your time, guys
| 
August 7th, 2008, 12:57 AM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland Age: 22
Posts: 2,790
| |
Try replacing your sub-query with something like: -
AND `date` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 7 DAY)
-
If that doesn't work, try adding a EXPLAIN in front of your query. That should show you what is causing the delay.
Last edited by Atli; August 7th, 2008 at 05:53 PM.
Reason: Changed the INTERVAL unit from DAYS to DAY
| 
August 7th, 2008, 01:40 AM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California Age: 23
Posts: 193
| | Quote: |
Originally Posted by Atli Try replacing your sub-query with something like: -
AND `date` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 7 DAY)
-
If that doesn't work, try adding a EXPLAIN in front of your query. That should show you what is causing the delay. | thanks for the quick reply!
i'm trying a simple version of that query and i cannot get it to return any results... can you see what i'm doing wrong? -
SELECT t.`prod_id`, t.`date`
-
FROM `store_tranlog` t
-
WHERE CAST(t.`date` AS DATETIME) BETWEEN NOW() AND DATE_SUB(NOW() , INTERVAL 7 DAY)
-
LIMIT 0,10
as for the EXPLAIN this is what it returned
(i'm attaching a zipped htm file with the details)
| 
August 7th, 2008, 02:23 AM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland Age: 22
Posts: 2,790
| |
O yea sorry, forgot. The lower number (or the older date) must be first in the BETWEEN clause.
As for the EXPLAIN, the first table, the one you named t, doesn't seem to have any indexes that can be used in your query.
Try adding an index to the "status" field of the "store_tranlog" table.
You can do that by using ALTER TABLE: -
ALTER TABLE `store_tranlog`
-
ADD INDEX (`status`)
-
If there is any valuable data in there, I would back it up first tho.
| 
August 7th, 2008, 02:46 AM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Southern California Age: 23
Posts: 193
| | Quote: |
Originally Posted by Atli O yea sorry, forgot. The lower number (or the older date) must be first in the BETWEEN clause.
As for the EXPLAIN, the first table, the one you named t, doesn't seem to have any indexes that can be used in your query.
Try adding an index to the "status" field of the "store_tranlog" table.
You can do that by using ALTER TABLE: -
ALTER TABLE `store_tranlog`
-
ADD INDEX (`status`)
-
If there is any valuable data in there, I would back it up first tho. | alright i added the index and... on average it shaved 0.2 - 0.3 seconds off the time :)...
then i changed my query to: - SELECT t.`prod_id`, p.`name`, p.`category`, SUM(t.`prod_qty`) AS `SumOfprod_qty`
-
FROM `store_products` p INNER JOIN `store_tranlog` t ON p.`id` = t.`prod_id`
-
WHERE p.`wholesale_tf`="1" AND t.`status`>"0" AND p.`status` = "1" AND p.`catalog`="NON" AND CAST(t.`date` AS DATETIME) BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
-
GROUP BY `prod_id`
-
ORDER BY `SumOfprod_qty` DESC
-
LIMIT 0,10
and BOOM! it took only 0.04 seconds! seems like a significant enough difference for me ;-)
thank you!!!!
| 
August 7th, 2008, 03:42 AM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland Age: 22
Posts: 2,790
| |
Glad I could help :)
| 
August 7th, 2008, 07:18 AM
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 305
| |
Yes it is amazing! I once changed a 9 second query to under 0.1 seconds by adding only one index :)
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|