简体   繁体   English

PHP查询MYSQL很慢。 有什么可能的原因呢?

[英]Php query MYSQL very slow. what possible to cause it?

I have a php page query mysql database, it will return about 20000 rows. 我有一个php页面查询mysql数据库,它将返回约20000行。 However the browser will take above 20 minutes to present. 但是,浏览器将需要20分钟以上的时间才能显示。 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. 我已经在数据库中添加了索引,并且确实使用了它,命令行中的查询时间对于20000行大约为1秒。 but in web application, it takes long. 但是在Web应用程序中,它需要很长时间。 is anyone know which causing this problem? 有谁知道是什么引起了这个问题? and better way to improve it?Below is my php code to retrieve the data: 更好的方法来改善它?下面是我的PHP代码来检索数据:

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>\n";
    $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>\n";
    };
    echo "\t\t</tr>\n";
}

Try adding an index to your date column. 尝试在date列中添加索引。

Also, it's a good idea to learn about the EXPLAIN command. 另外,了解EXPLAIN命令也是一个好主意。

As mentioned in the comments above, 1 second is still pretty long for your results. 正如上面的评论中提到的,您的结果仍然需要1秒的时间。

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). 列表可能会更好-如果可能的话,最好还是使用分页视图(如其他答案所建议)。

It's not PHP that's causing it to be slow, but the browser itself rendering a huge page. 导致运行缓慢的不是PHP,而是浏览器本身呈现了一个很大的页面。 Why do you have to display all that data anyway? 为什么仍然要显示所有这些数据? You should paginate the results instead. 您应该对结果进行分页。

Try constructing a static HTML page with 20,000 table elements. 尝试使用20,000个表格元素构造一个静态HTML页面。 You'll see how slow it is. 您会看到它有多慢。

You can also improve that code: 您还可以改进该代码:

while ($line = mysql_fetch_assoc($result1)) {
    echo "\t\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t\t<td>$col_value</td>\n";
        flush(); // optional, but gives your program a sense of responsiveness
    }
    echo "\t\t</tr>\n";
}

In addition, you should increase your acceptance rate. 此外,您应该提高接受率。

You could time any steps of the script, by echoing the time before and after connecting to the database, running the query and outputting the code. 通过回显连接到数据库之前和之后的时间,运行查询并输出代码,可以计时脚本的任何步骤。 This will tell you how long the different steps will take. 这将告诉您不同的步骤将花费多长时间。 You may find out that it is indeed the traffic causing the delay and not the query. 您可能会发现确实是造成延迟的流量,而不是查询。

On the other hand, when you got a table with millions of records, retreiving 20000 of them can take a long time, even when it is indexed. 另一方面,当您获得一个包含数百万条记录的表时,即使对其进行了索引,检索其中的20000条记录也可能需要很长时间。 20 minutes is extreme, though... 不过20分钟是极端的...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM