简体   繁体   中英

SQL / PHP - Optimize query inside a while loop

I have a fairly simple query:

$r = $dbh->prepare("SELECT user FROM this_users_rented WHERE user_by=:user LIMIT $offset, $rowsperpage");
$r->bindParam(':user', $userdata['username']);
$r->execute();

($offset and $rowsperpage is representing the offset of the list, based on the current page, and how many records there should be shown per page. (Example: 0,100))

This will gather all the data from this_users_rented where the user_by is = $userdata['username'];

I am running this query in a WHILE LOOP:

    while($data=$r->fetch()):
      //Get data from table: this_users_rented to print out in the while loop.
          $stmt = $dbh->prepare("SELECT * FROM xeon_users_rented_stats WHERE urs_user=:user");
          $stmt->bindParam(':user', $data['user']);
          $stmt->execute();
          $refStat = $stmt->fetch();

    endwhile;

So, imagine that there is hundreds of records in the $r query - yielding hundreds of queries to be run (due to the lack of optimization of the $stmt query )

So my question is, how can I optimize the $stmt query ?

You have a LIMIT clause on your user table so you could use following trick to overcome the MySQL limitation that you can't use a LIMIT clause in a subselect for the preparing of the statement:

$r = $dbh->prepare("
               SELECT x.* 
               FROM xeon_users_rented_stats x
               INNER JOIN (
                   SELECT 
                       user_by 
                   FROM this_users_rented 
                   WHERE user_by = :user 
                   LIMIT $offset, $rowsperpage
               ) t
               ON x.urs_user = t.user_by
               ORDER BY x.urs_user;"
      );
$r->bindParam(':user', $userdata['username']);
$r->execute();
while($refstat=$r->fetch()){
    // do what you want to do ...
}

This trick changes the subselect to a materialized derived table where you can use LIMIT.

Note:

Of course you should test the sql statement in a sql client first to make sure you get the data you need.

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