开发者

How can i optimize this code for a better speed?

The code:

<?php $local_id = $_GET['id'];

mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("database");
$sql = mysql_query("SELECT * FROM `videos` WHERE `id` = ".$local_id." ");
while($row = mysql_fetch_array($sql)){$file = $row["file_url"];}

header("Expires: Mon, 20 Dec 1980 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: no-cache");
header("Content-Type: video/flv");

readfile($file); ?>

The file is a .flv one but it takes ~ 1 min. to load the video

What can i do to load it faster?

Is t开发者_如何学Chis code correct?


Do not use output buffering for transferring large files. php has to store the entire file in memory and wait till it's completely read before giving it out to the client. Just removing the ob_* lines should work.


First thing i would say is putting an index on the Id field to decrease lookup time... and use memcache to cache DB results If you don't have constraints on the environment I would put a caching reverse proxy like nginx which caches the HTTP response using your caching headers but will be for every client. Alternatively there are hardware devices that do this for you... usually quite expensive though

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜