MySQL in and limit performance

Posted on 2009-07-09
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.
Question by:JianJunShen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 21

Accepted Solution

K V 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.
LVL 21

Assisted Solution

by:K V
K V earned 400 total points
ID: 24813106
Mean while about pagination have a look at this:
LVL 33

Assisted Solution

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.
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Author Comment

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?
LVL 21

Assisted Solution

by:K V
K V 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.

Author Comment

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?
LVL 21

Assisted Solution

by:K V
K V earned 400 total points
ID: 24846171

Featured Post

ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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