简体   繁体   中英

Query optimization : Which SELECT syntax is faster?

Given 5,000 IDs of records fetch in the database, which query , in your opinion is faster?

  1. Loop through 5000 IDs using php and perform a SELECT query for each one,

     foreach($ids as $id){ // do the query $r = mysql_query("SELECT * FROM TABLE WHERE ID = {$id}"); } 
  2. Or collect all ids in an array, and use SELECT * FROM TABLE WHERE ID IN (1 up to 5000)

     //assuming $ids = array(1,2 ---- up to 5000); $r = mysql_query("SELECT * FROM TABLE WHERE ID IN (".join(",",$ids).")"); 

option 2 is definitely going to be faster. 5000 separate db queries are going to have huge network connection overhead.

Without a shadow of a doubt, loading them all in one go will be faster. Running 5,000 queries is going to be a lot slower as each query will carry a certain amount of overhead.

Also, to speed it up even more, DON'T use the * operator! Select the fields you are going to use, if you only need the ID column, specify this! If you want all the columns, specify them all, because you may later add fields in and you do not need to retrieve this new field.

The fastest way is not to request 5000 rows at all.
You barely need 100 to display them on one page. 5000 is way overkill

Sure measure it, but I'd certainly recommend letting the database doing the job. All depends, I hope you're not creating a connection for each call though.

Loop is faster if you use a Query Statement using bind variables. Declare the Statement off the loop; then inside the loop bind the variable per each id.

Do not underestimate the time going into SQL parsing; especially on these long winded things.

Option 2 is faster. With option 1 you do a full roundtrim to the server for each iteration.

I'd point out that in this case you might consider using paging to display the data.

Hint: Measure, Measure, Measure. With a code worth 10 minutes of your time you will have the answer right away.

Which is faster for many queries?
Try measure it for example like this:

<?php
    $start = getmicrotime();
    for ($i=0;$i<100000;$i++)
    {
            foreach($ids as $id){
              // do the query 
              $r = mysql_query("SELECT * FROM TABLE WHERE ID = {$id}");
            }

    }
    $end = getmicrotime();
    echo 'Time (1): '.($end- $start).' sec';



    $start = getmicrotime();
    for ($i=0;$i<100000;$i++)
    {
            //assuming $ids = array(1,2 ---- up to 5000);
            $r = mysql_query("SELECT * FROM TABLE WHERE ID IN (".join(",",$ids).")");
    }
    $end = getmicrotime();
    echo 'Time (2): '.($end- $start).' sec';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM