If i had a mysql db with 7000 records in it and i had to display all 7000 on my webpage. Are there any tips and tricks to speed the process up (the browser practically crashes before showing all开发者_如何学Go the records) i know i should query it to reduce the number of records selected but i actually need to display 7000...
Turn on output buffering : http://php.net/manual/en/function.ob-start.php
<?php
ob_start();
// do work...
ob_end_flush();
?>
EDIT
The following php fetches 10K products and outputs them as a table with no problems in 0.014 seconds.
<?php
ob_start();
$conn = new Mysqli("localhost", "vldb_dbo", "pass", "vldb_db");
$startTime = microtime(true);
$result = $conn->query(sprintf("call list_products(%d)", 10000));
echo "<table border='1'>";
while($row = $result->fetch_assoc()){
echo sprintf("<tr><td>%s</td><td>%s</td></tr>", $row["prod_id"], $row["name"]);
}
echo "</table>";
echo sprintf("<br/>Page generated in %s secs",number_format(microtime(true) - $startTime, 6, ".", ""));
$result->close();
$conn->close();
ob_end_flush();
?>
SQL Script
drop procedure if exists list_products;
delimiter #
create procedure list_products
(
in p_prod_id int unsigned
)
begin
select * from product where prod_id between 1 and p_prod_id;
end #
delimiter ;
Works fine with 50K records too except runtime dives - Page generated in 0.112902 secs
EDIT2
http://phplens.com/lens/php-book/optimizing-debugging-php.php
An excerpt from the above link:
An alternate way of speeding the above code would be to use output buffering. This will accumulate the output string internally, and send the output in one shot at the end of the script. This reduces networking overhead substantially at the cost of more memory and an increase in latency. In some of my code consisting entirely of echo statements, performance improvements of 15% have been observed.
I'm assuming you're doing this in PHP (which I'm not familiar with) but this is generic to all languages.
Query 7000 records into a list first, then display all the list in a scripting page (e.g. php, jsp for java, etc.). Don't allow your php page to open the connection, read each item from the list and displaying them. The client (browser) might wait indefinitely and cause a session timeout
f00: if he displays the result in table and the browser will survive to display the data, they wont be displayed correcly anyways. As far as I know, the table wont render correctly (in firefox)... I think that paging the table is the only possible solution...
精彩评论