drkwpgrdb092239314 2010-11-18 04:53
浏览 110
已采纳

PHP查询MYSQL很慢。 有什么可能导致它?

I have a php page query mysql database, it will return about 20000 rows. However the browser will take above 20 minutes to present. I have added index on my database and it do used it, the query time in command line is about 1 second for 20000 rows. but in web application, it takes long. is anyone know which causing this problem? and better way to improve it?Below is my php code to retrieve the data:

select * from table where Date between '2010-01-01' and '2010-12-31' 
$result1 = mysql_query($query1) or die('Query failed: ' . mysql_error());

while ($line = mysql_fetch_assoc($result1)) {
    echo "\t\t<tr>
";
    $Data['Date'] = $line['Date'];
    $Data['Time'] = $line['Time'];
    $Data['Serial_No'] = $line['Serial_No'];
    $Data['Department'] = $line['Department'];
    $Data['Team'] = $line['Team'];

    foreach ($Data as $col_value) {
        echo "\t\t\t<td>$col_value</td>
";
    };
    echo "\t\t</tr>
";
}
  • 写回答

3条回答 默认 最新

  • duanqianruan8448 2010-11-18 04:54
    关注

    Try adding an index to your date column.

    Also, it's a good idea to learn about the EXPLAIN command.

    As mentioned in the comments above, 1 second is still pretty long for your results.

    You might consider putting all your output into a single variable and then echoing the variable once the loop is complete.

    Also, browsers wait for tables to be completely formed before showing them, so that will slow your results (at least slow the process of building the results in the browser). A list may work better - or better yet a paged view if possible (as recommended in other answers).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏