Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

MySQL in and limit performance

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
JianJunShen
Asked:
JianJunShen
  • 4
  • 2
5 Solutions
 
K VDatabase ConsultantCommented:
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
 
K VDatabase ConsultantCommented:
Mean while about pagination have a look at this:
http://www.percona.com/ppc2009/PPC2009_mysql_pagination.pdf
0
 
snoyes_jwCommented:
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
JianJunShenAuthor Commented:
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
 
K VDatabase ConsultantCommented:
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
 
JianJunShenAuthor Commented:
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
 
K VDatabase ConsultantCommented:
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now