Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2012, 12:08 PM   PM User | #1
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Slow query.

I have the job of combing the databases from lots of smaller sites (all with same database structure) in to one big central database. This was going fine until i came across the first query that is really slow.

Code:
SELECT * , RANGE_ID AS ID
FROM SITES_RANGE
WHERE SITE_ID =00001
AND RANGE_ID
IN (

SELECT DISTINCT RANGE_ID
FROM SITES_PRODUCT
WHERE CATEGORY_ID = '00009'

)
ORDER BY ORDERING DESC
While the sub query takes only 0.13 seconds. The whole query combined takes 3.5 seconds. Which is too slow i think. How can i speed it up. The query basically checks product to select all the ranges that belong to a certain category for that site. Can i add an index to my tables? And if so what fields should i add the indexes to?
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 11-23-2012, 12:50 PM   PM User | #2
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
I tried using a join query instead of the sub query in the end

Code:
SELECT SITES_RANGE . * , SITES_RANGE.RANGE_ID AS ID
FROM SITES_RANGE
JOIN SITES_PRODUCT ON SITES_RANGE.RANGE_ID = SITES_PRODUCT.RANGE_ID
AND SITES_PRODUCT.CATEGORY_ID = '00001'
GROUP BY ID
Speed was 0.11 secs

Problem solved
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Reply

Bookmarks

Tags
select all ranges, slow query

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 PM.


Advertisement
Log in to turn off these ads.