MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 18th, 2003, 06:57 AM
cesarsir cesarsir is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 cesarsir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking MySQL LIMIT + ORDER possible performance bug

Hi, I made a huge SELECT query, ordering by name (varchar 50). The query was very time consuming, though it only gave back about 100 or so entries (of a total of 60,000).

However, making the same SELECT with a LIMIT command was 4-plus times faster.

That is very surprising, because the number (N) I used in LIMIT was dynamical, made with a previous SELECT COUNT(*) query (without ORDER BY). So I obtained the same results using LIMIT and without LIMIT.

So we have two identical query results, but if you make an additional (previous) SELECT COUNT, you get 4 times faster speed. What is the reason for that? Theoretically, you should go faster with one query, but for some reason the LIMIT was speeding things, even if the results displayed were identical.

Thanks in advance.

Reply With Quote
  #2  
Old February 18th, 2003, 09:10 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
I assume you made the SELECT COUNT(*) query first... is that right?

BTW, you don't have to do SELECT COUNT(*). You can speed it up a little by doing SELECT COUNT(field).

The 60,000 record select count query is fast because really, 60,000 records really isn't a lot to count straight down through. Not to mention you probably have it indexed which should be helping you out.

From the manual:

If you are selecting only a few rows with LIMIT, MySQL will use indexes in some cases when it normally would prefer to do a full table scan.
If you use LIMIT # with ORDER BY, MySQL will end the sorting as soon as it has found the first # lines instead of sorting the whole table.

As soon as MySQL has sent the first # rows to the client, it will abort the query (if you are not using SQL_CALC_FOUND_ROWS).


Also, you may want to run your query through:

EXPLAIN SELECT select_options
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old February 18th, 2003, 10:08 AM
cesarsir cesarsir is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 cesarsir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your reply. Well, in fact the actual query was done in two correlated tables, that is because it was time demanding.

You are right about COUNT(field), I wrote COUNT(*) for simplicity.

About the quotes from the manual, I still don't understand it. In order to make the LIMIT with the correct entries, it MUST make the Order - BEFORE !!- applying the LIMIT N. So still the difference in times in not understandable. Why don't MySQL internal engine makes the first SELECT COUNT(field) silently ALWAYS that it encounters a SELECT...ORDER statement???

Cesar

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MySQL LIMIT + ORDER possible performance bug


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway