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 01-29-2011, 03:07 AM   PM User | #1
cooleo100d
Regular Coder

 
Join Date: Jan 2005
Location: NY, USA
Posts: 132
Thanks: 1
Thanked 1 Time in 1 Post
cooleo100d is an unknown quantity at this point
ORDER BY slows down join considerably

Hi,

I'm doing a select to grab some rows, and I need to select the 100 newest ones, so I'm doing a sort on an integer timestamp field.

Anyway, my query looks something like this (I've simplified it just to show the concept):
Code:
SELECT c.content_id, c.content_user_id, c.content_client_timestamp, u.user_username 
FROM tbl_content as c, tbl_user_relationships as r, tbl_users as u
WHERE r.relationship_user_id = VARIABLE_USER_ID_HERE && c.content_user_id = r.relationship_to_user_id && u.user_id = c.content_user_id 
ORDER BY c.content_client_timestamp DESC LIMIT 100
Now, the query works fine, but as far as I'm concerned, it's slow. With about 150,000 records in the content table, it takes about .4 seconds. By tweaking the indexes, I can get that time down a little, but that seems to effect other stuff. But, by removing the ORDER BY, it goes down to a very respectable 0.0013.

My indexes are like this: I of course have all the fields that are joined on indexed, and I have content_client_timestamp indexed. It seems like it's not properly using that index to order by. I tried doing an index like content_client_timestamp, content_user_id, and that brings it down to 0.15 seconds, but it seems I need to force MySQL to use that index in place of just content_user_id which I find strange.

Any insight into this issue is greatly appreciated. I can't tell if this is just a complex query that requires this amount of time, or I'm doing something wrong. I'm leaning towards it shouldn't be taking that long If you have any questions about the setup, please let me know.

Thanks in advance.
cooleo100d is offline   Reply With Quote
Old 01-29-2011, 04:04 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You'll need to run an EXPLAIN on the query to see if your indexes are being used.
__________________
Fumigator is offline   Reply With Quote
Old 01-29-2011, 04:11 AM   PM User | #3
cooleo100d
Regular Coder

 
Join Date: Jan 2005
Location: NY, USA
Posts: 132
Thanks: 1
Thanked 1 Time in 1 Post
cooleo100d is an unknown quantity at this point
When I run explain, and do tests, it seems to like the index with the timestamp first and then the content_user_id the best. With about 500,000 rows in the table, it takes about .03 seconds. I guess that's not bad. Does this seem reasonable?
cooleo100d is offline   Reply With Quote
Old 01-29-2011, 07:53 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
0.03 for selecting top 100 out of 500,000 rows? yes, that's quite reasonable.

Could it be faster if MySQL were smarter? You know it.

MySQL's use of indexes is...well, to be generous..."primitive", when compared to the professional DBs such as SQL Server and Oracle.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
cooleo100d (02-02-2011)
Old 02-02-2011, 12:33 PM   PM User | #5
DistantJob
New Coder

 
Join Date: Feb 2011
Posts: 48
Thanks: 1
Thanked 2 Times in 2 Posts
DistantJob is an unknown quantity at this point
SELECT c.content_id, c.content_user_id, c.content_client_timestamp, u.user_username
FROM tbl_content as c, tbl_user_relationships as r, tbl_users as u
WHERE r.relationship_user_id = VARIABLE_USER_ID_HERE && c.content_user_id = r.relationship_to_user_id && u.user_id = c.content_user_id
ORDER BY c.content_client_timestamp DESC LIMIT 0, 100

Instead of LIMIT 0, 100 you've put LIMIT 100, so every record was in the result set of your query except those you wanted to select.
DistantJob is offline   Reply With Quote
Old 02-02-2011, 05:31 PM   PM User | #6
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Quote:
Originally Posted by DistantJob View Post
SELECT c.content_id, c.content_user_id, c.content_client_timestamp, u.user_username
FROM tbl_content as c, tbl_user_relationships as r, tbl_users as u
WHERE r.relationship_user_id = VARIABLE_USER_ID_HERE && c.content_user_id = r.relationship_to_user_id && u.user_id = c.content_user_id
ORDER BY c.content_client_timestamp DESC LIMIT 0, 100

Instead of LIMIT 0, 100 you've put LIMIT 100, so every record was in the result set of your query except those you wanted to select.
Huh? I don't think so.

LIMIT 0, 100 is the same as LIMIT 100. If you only provide one argument to the LIMIT clause, it returns that many rows starting from the beginning of the result set.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 02-02-2011, 07:08 PM   PM User | #7
cooleo100d
Regular Coder

 
Join Date: Jan 2005
Location: NY, USA
Posts: 132
Thanks: 1
Thanked 1 Time in 1 Post
cooleo100d is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
0.03 for selecting top 100 out of 500,000 rows? yes, that's quite reasonable.

Could it be faster if MySQL were smarter? You know it.

MySQL's use of indexes is...well, to be generous..."primitive", when compared to the professional DBs such as SQL Server and Oracle.
OK, thanks for the info. Sounds good.

Quote:
Originally Posted by oracleguy View Post
Huh? I don't think so.

LIMIT 0, 100 is the same as LIMIT 100. If you only provide one argument to the LIMIT clause, it returns that many rows starting from the beginning of the result set.
Yeah, this is correct. The query is correct semantically, it just seems like it's a matter of tweaking the indexes.
cooleo100d is offline   Reply With Quote
Reply

Bookmarks

Tags
join, mysql, order by, slow

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 02:28 AM.


Advertisement
Log in to turn off these ads.