开发者

Php stuck because to long load on mysqli query in php function, how to fix?

开发者 https://www.devze.com 2023-03-29 14:16 出处:网络
i\'m using a php function to get dome mysql data from other mysql host than my webserver. Function: public function theMysqli($build){ // $build is given by othe code (no usedata)

i'm using a php function to get dome mysql data from other mysql host than my webserver.

Function:

public function theMysqli($build){ // $build is given by othe code (no usedata)

   $mysqli = new mysqli($server, $user, $password, $database);
   $catid = array( /* +/- 40 id's */ );  //data is $catid = configs::songcats();
   $type = array( /* +/- 15 captical letters */ );  //data is $type = configs::songtype();
   $limit = 20;
   $lstart = $_post['page'];
     if($lstart == ''){
        $lstart = 0;
     }
     else{
        $lstart = $lstart * $limit;
     }
   $sletter = $_POST['letter'];
   $search = $sletter.'%';
   $catquery = "SELECT songid FROM category WHERE catID IN('".implode("', '", $catid)."')";
     if ($db = $mysqli->query($catquery)){
    while($row = $db->fetch_array()){
       $idsong[] = $row; 
        }
    $db->close();
     }

   foreach($idsong as $gt){
     $songid[] = $gt['songid'];
   }
   // songid is a array over the 30000 values

   $countquery = "SELECT id FROM songlist WHERE songtype IN('".implode("', '", $type)."') AND id IN('".implode("', '", $songid)."') AND songname LIKE '".$search."'";
     if ($db = $mysqli->query($countquery)){
    $countr = $db->num_rows;
    $db->close();
     }
   $pages = ceil($countr / $limit);
   $songquery = "SELECT id, songname, artist, copyright, duration FROM songlist WHERE songtype IN('".implode("', '", $type)."') AND id IN('".implode("', '", $songid)."') AND songname LIKE '".$search."' ORDER BY songname ASC LIMIT $lstart, $limit";

   if ($db = $mysqli->query($s开发者_JAVA技巧ongquery)){
      while($row2 = $db->fetch_array()){
         $result[] = $row2; 
      }
      $db->close();
   }
   if($built == 'counter'){
     $final == $pages;
   }
   else if($build == 'gresult'){
     $final == $result;
   }
   return $final;
}

Now my problem is the load time he need for this script it will be to long. Even when i set php.ini so that execute may be 300sec he will stuck by loading the page. Now i know you can get data grom mutiple mysql tables by one query but i can't find any solution to do that in combination with php implode function.

Total rows i must get by $_POST['letter'] M is +/- 1200; (web radio mp3 database)

Can someone help me to fix this function so i get no timeout's anymore. Thanks


The problem here is that you're fetching a list from the database, and then sending that list back as part of a query. You should really be doing most of this stuff in SQL, using either JOIN or nested queries. This will make your program much faster.

First, create a table for all of your catids and types. Your catquery should then be:

SELECT songid 
FROM category 
WHERE catID IN (
    SELECT id
    FROM catids
)

Use the same sort of pattern to join your queries together. It looks like you can cut down most of your code here to just one SQL query. You'll save a ton of time and memory by not having to send all that data back and forth between your program and the database.

Some reading material for you:

SQL Joins: http://beginner-sql-tutorial.com/sql-joins.htm

SQL Subquery: http://beginner-sql-tutorial.com/sql-subquery.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消