简体   繁体   English

PHP查询加载太慢。 怎么加快?

[英]PHP Query Loading too slow. How to speed up?

Does anyone know a more efficient way to write the following. 有没有人知道一种更有效的方式来编写以下内容。 my load time over a minute right now. 我的加载时间现在超过一分钟。 This what I came up with after an even longer codeset. 这是我在更长的代码集之后提出的。

$max = $i + 25;
while ($i < $max)
  {
    $memberno = $memberid;
    if($pulledid!=$memberid)
      {
        $pulledid = mysql_result($result99,$i,"id_member");
        $sql_statement = "Select vinyl_fortrade.ImageID,vinyl_fortrade.MemberID from vinyl_fortrade where exists (Select ImageID from vinyl_wanted WHERE MemberID = $memberno AND vinyl_wanted.ImageID = vinyl_fortrade.ImageID) AND vinyl_fortrade.MemberID=$pulledid";
        $result=mysql_query($sql_statement)or die(mysql_error());    
        $num=mysql_numrows($result);
        if($num>0)
          {
            $sql_statement2 = "Select vinyl_wanted.ImageID, vinyl_wanted.MemberID from vinyl_wanted where exists (Select ImageID from vinyl_fortrade WHERE MemberID = $memberno AND vinyl_wanted.ImageID = vinyl_fortrade.ImageID) AND vinyl_wanted.MemberID=$pulledid";
            $result2=mysql_query($sql_statement2)or die(mysql_error());                
            $num2=mysql_numrows($result2);
            $membername = mysql_result($result99,$i,"smf_members.member_name");
            $userid2 = mysql_result($result99,$i,"smf_members.id_member");
            if ($num2 > 0)
              {
                $lastlogged = mysql_result($result99,$i,"smf_members.last_login");
                echo '<td><a href="http://vinylmationconnection.com/forum/index.php?action=pm;sa=send;    u='.$userid.'"style="color: #FFF">'.$membername.'</a><br>Last Logged In:<br>';
                echo date('Y-m-d', $lastlogged);
                echo ':</td>';
                echo '<td>';
                $n = 0;
                $m = 0;
                while ($n < $num)
                  {
                    $title=mysql_result($result,$n,"vinyl_fortrade.ImageID");
                    $sql_statementj = "Select * from vinyl_figures WHERE ImageID = '$title'";
                    $resultj=mysql_query($sql_statementj)or die(mysql_error());  
                    $seriess=mysql_result($resultj,0,"vinyl_figures.Series");
                    echo '<img  src="http://vinylmationconnection.com/images/p'.$seriess.'/'.$title.'.jpg">';
                    $n++;
                    if($n%4=="0")
                      echo "<br>";
                  }
                echo '</td><td>';
                while ($m < $num2)
                  {
                    $title=mysql_result($result2,$m,"vinyl_wanted.ImageID");
                    $sql_statementj = "Select Series from vinyl_figures WHERE ImageID = '$title'";
                    $resultj=mysql_query($sql_statementj)or die(mysql_error());  
                    $seriess=mysql_result($resultj,0,"vinyl_figures.Series");
                    echo '<img src="http://vinylmationconnection.com/images/p'.$seriess.'/'.$title.'.jpg">';
                    $m++;
                    if($m%4=="0")
                      echo "<br>";
                  }                              
                echo '</td> </tr>';
              }
          }
      }
    $i++;
  }

Please let me know if ths code is self explanatory. 如果代码是自我解释的,请告诉我。

$sql_statementj = "Select * from vinyl_figures WHERE ImageID = '$title'";
$sql_statementj = "Select Series from vinyl_figures WHERE ImageID = '$title'";

in these 2 statements you seem to make a mysql request for the same file if I got your code right. 在这两个语句中,如果我的代码正确,你似乎会对同一个文件发出一个mysql请求。 make these requests outside the loops, this should do some work. 在循环之外发出这些请求,这应该做一些工作。

编写一个存储过程(可能是一个简单的查询/ View就足够了)来完成所有这些查询工作并监视数据库索引。

The WHERE EXISTS clause is almost certainly the culprit, and it should be refactored. WHERE EXISTS子句几乎肯定是罪魁祸首,它应该被重构。 General guidelines for slow queries... 慢查询的一般准则......

  1. Use EXPLAIN SELECT to see what the query is really doing 使用EXPLAIN SELECT查看查询实际执行的操作
  2. Add indexes on all columns used in WHERE, JOIN, ORDER, GROUP 在WHERE,JOIN,ORDER,GROUP中使用的所有列上添加索引
  3. Avoid SELECT * and instead SELECT only the columns you need 避免使用SELECT *而只选择所需的列
  4. Add LIMIT to all queries that do not require a complete table scan. 将LIMIT添加到不需要完整表扫描的所有查询。

Except for the answeres that are alredy mentined I could suggest you to use mod_deflate to compress the page before sending it to the browser and in should have some effect. 除了回答的问题,我建议你在将页面发送到浏览器之前使用mod_deflate来压缩页面,并且应该有一些效果。 Althrough if you are on local environment that also may cause a slow execution, becouse of the load on the machine. 通过,如果您在本地环境中也可能导致执行缓慢,因为机器上的负载。

And as well I think that you could lose the first while by using LIMIT $i, 25 in the query I think that it will have the same effect. 而且我认为你可能会丢失第一个,而在查询中使用LIMIT $i, 25我认为它会产生相同的效果。 To when you fetch them all with a simple foreach and it is done and I would suggest you to push as much as possible in a single query that will increse the load a bit but it should decrease the loading time. 当你用一个简单的foreach获取所有这些并且它已完成时,我建议你尽可能多地推送一个查询,这会增加一点负载,但它应该减少加载时间。

I am not a 100% professional but am trying to be of help. 我不是100%的专业人士,但我正在努力寻求帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据需求从50k行表中选择一个随机行。 太慢了。 我如何加快速度? - Select a random row based on a requirement from 50k rows table. Too slow. How can I speed it up? Laravel 8 &amp; Laravel Windows 10 上的开发人员航行速度很慢。 如何加快速度? - Laravel 8 & Laravel Sail for dev on Windows 10 is slow. How to speed up? 不使用主键的 SQL LEFT JOIN 查询速度太慢,如何加快速度? - SQL LEFT JOIN Query not using Primary Keys is too slow, how can I speed up? php网站的初始加载速度太慢。 第二次是更快..如何解决? - Initial load of php website is too slow. second time it is faster .. how to solve? PHP MySQL递归速度慢,如何加速? - PHP MySQL recursion slow, how to speed up? PHP Phar在功能强大的PC上创建速度慢,如何加速(加载/读取~3000个文件)? - PHP Phar creation slow on a powerful PC, how to speed up (loading/reading ~3000 files)? 如何加快处理多个大型数组的缓慢加载PHP页面的速度? - How to speed up a slow loading PHP page that is processing several large arrays? GROUP BY和ORDER BY太慢了。 怎么做得更快? - GROUP BY and ORDER BY too slow. How to make faster? 使用imagemagick创建图片太慢了。 怎么提高? - creating pictures with imagemagick is too slow. how to improve? 内部联接太慢。 怎么会更快 - INNER JOIN too slow. how can it bee quicker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM