Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


MySQL in and limit performance

Posted on 2009-07-09
Medium Priority
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 1600 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 1600 total points
ID: 24813106
Mean while about pagination have a look at this:
LVL 33

Assisted Solution

snoyes_jw earned 400 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.
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


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 1600 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 1600 total points
ID: 24846171

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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