sean
sean

Reputation: 141

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

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>\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";
}

Upvotes: 0

Views: 1587

Answers (3)

bcosca
bcosca

Reputation: 17555

It's not PHP that's causing it to be slow, but the browser itself rendering a huge page. 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. 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";
}

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

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. 20 minutes is extreme, though...

Upvotes: 0

enobrev
enobrev

Reputation: 22532

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).

Upvotes: 2

Related Questions