Solved

MySQL in and limit performance

Posted on 2009-07-09
7
314 Views
Last Modified: 2012-05-07
HI: A performance questions: In MySQL, when display a list, I have two stratergy to display it:
SQL One:
select * from tablename where id in (some arrays);  

SQL Two:

select * from tablename limit 10,10;

Questions: Which query is faster?

In query one: If array size is 20 and 100, does it mean 100 is 5 times slower than 20.
0
Comment
Question by:JianJunShen
  • 4
  • 2
7 Comments
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 400 total points
ID: 24813073
Use explain to check query performances.
"in" should use indexes to perform quicker.
"limit n,m" will require full table scan which we try to avoid normally.

The difference wont be that large.
0
 
LVL 21

Assisted Solution

by:theGhost_k8
theGhost_k8 earned 400 total points
ID: 24813106
Mean while about pagination have a look at this:
http://www.percona.com/ppc2009/PPC2009_mysql_pagination.pdf
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 100 total points
ID: 24813165
The second one is probably faster as written, but usually if you have LIMIT you also have ORDER BY, and then it's probably slower. Depends on if there are indexes involved and how big the table is.

It's hard to say exactly how different looking up 20 values vs. 100 values would be; the easiest thing would be to just try it and see.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:JianJunShen
ID: 24814346
Hi theGhost_k8:

What about I have less than 10,000 records. Does that worthe indexing in order for faster speed? Or in other way, if my table rows are less than 10,000, use in or use limit?
0
 
LVL 21

Assisted Solution

by:theGhost_k8
theGhost_k8 earned 400 total points
ID: 24820439
Have you checked actually executing queries?
I see limit n,m easily manageable - considering there'd be less performance differences.
In case of "order by", you check the difference in both the queries.
Make sure, you keep the query_cache off / use SQL_NO_CACHE for comparison.
0
 

Author Comment

by:JianJunShen
ID: 24843688
Hi:  experts:

Do you have some links to tell how to check the SQL performance? How faster it is and how many memory it consumes?
0
 
LVL 21

Assisted Solution

by:theGhost_k8
theGhost_k8 earned 400 total points
ID: 24846171
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

20 Experts available now in Live!

Get 1:1 Help Now