简体   繁体   中英

Optimization with many queries in page

I have in my page 5 queries that im wishing to have just one query if its possible , i dont know if there is a better strategy to what im doing or its ok .

this is my main query to load the books

 $mess = $mysqli->prepare('SELECT * from ( SELECT m.id, cm.voteup,
                                                  cm.votedown,cr.book_descr FROM books m
                           INNER JOIN  books_vote cm ON cm.bookid = m.id
                           INNER JOIN  book_info cr ON cr.bookid = m.id
                           WHERE bookid = ?  ORDER BY m.date DESC LIMIT 30)ddd 
                           ORDER BY m.date ASC');
    $mess->bind_param("i", $book);                              
    $mess->execute();
    $mess->store_result();
    $mess->bind_result($id,$voteup,$votedown);
        while($row = $mess->fetch()){
             //  im fetching here in this <div class='areabooks'>  
              }

Then i reset the query to fetch in other div with IF condition

  $mess->data_seek(0);
  while($row = $mess->fetch()){
    if($voteup - $votedown >= 5){
      //I fetch here in this <div class='areabooks2'>
        }
        }

Then im cheking the top MAX(voteup - votedown ) with this query

 $messtop = $mysqli->prepare('SELECT ..........
                           INNER JOIN ...
                           INNER JOIN ( select id ,MAX(voteup - votedown ) as maxe
                                from books
                                where voteup - votedown >= 5
                                group by id  ) tt
                           WHERE bookid = ?
                           ORDER BY maxe DESC,cm.votedown asc,cm.voteup DESC
                           limit 1');
    $messtop->bind_param("i", $book);                              
    $messtop->execute();
    $messtop->store_result();
    $messtop->bind_result($id,$voteup,$votedown);
    $messtop->fetch();
    // then i fetch in this <div class='topbook' > 

Then i reset the first query again to fetch in other div with IF condition

$mess->data_seek(0); 
while($row = $mess->fetch()){
    if($votedown - $voteup >= 5){
       //I fetch here in this <div class='areabooks3' >
    }
}

Then im getting the top MAX(votedown - voteup ) with this query

   $messtop = $mysqli->prepare('SELECT ..........
                           INNER JOIN ...
                           INNER JOIN ( select id ,MAX(votedown - voteup) as maxe
                                from books where votedown - voteup >= 5
                                group by id  ) tt
                           WHERE bookid = ?
                           ORDER BY maxe DESC,cm.voteup ASC,cm.votedown DESC
                           limit 1');
    $messdown->bind_param("i", $book);                              
    $messdown->execute();
    $messdown->store_result();
    $messdown->bind_result($id,$voteup,$votedown);
    $messdown->fetch();
    // then i fetch in this <div class='topbottonbook' > 

What im looking for is if there is a strategy to use just one query and fetch it in all these divs , those divs are separated like that .

 <first div>
 <second div>
 <3rd div>
 <4th div>
  5th div 

How to simplify this thanks ?

I doubt if the 3 queries can be combined into one.

First query:

Postpone the JOIN until you have the 30 'latest' bookids . That is, move the JOINs out to the outer SELECT .

Also have INDEX(id, date) . But I am confused. Ordinarily id is the PRIMARY KEY , hence unique. Yet the query implies that one book has multiple dates. What gives? ( Please provide SHOW CREATE TABLE .)

The mysql commandline tool (or Workbench or phpmyadmin) is your friend. Manually run the queries using one of them. I think that you will find errors. Please fix.

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