Solved

MySQL in and limit performance

Posted on 2009-07-09
7
325 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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