问题描述
我有一个包含许多sql语句(postgres-db)的PHP文件,执行查询大约需要1秒钟。但是我认为获取数据需要很长时间。有没有一种方法可以使这部分更快:
<?PHP
// Call this at each point of interest,passing a descriptive string
function prof_flag($str)
{
global $prof_timing,$prof_names;
$prof_timing[] = microtime(true);
$prof_names[] = $str;
}
// Call this when you're done and want to see the results
function prof_print()
{
global $prof_timing,$prof_names;
$size = count($prof_timing);
for($i=0;$i<$size - 1; $i++)
{
echo "<b>{$prof_names[$i]}</b><br>";
echo sprintf(" %f<br>",$prof_timing[$i+1]-$prof_timing[$i]);
}
echo "<b>{$prof_names[$size-1]}</b><br>";
}
prof_flag("Start");
$time_start = microtime(true);
prof_flag("Connect to DB");
require("PHP/00connection.PHP");
prof_flag("Perform query");
$stmt_dbstorage_helium = $conn->query("SELECT used_space,epoch_time FROM db_storage WHERE host_id = 1 ORDER BY epoch_time;")->fetchAll();
$stmt_dbstorage_neon = $conn->query("SELECT used_space,epoch_time FROM db_storage WHERE host_id = 2 ORDER BY epoch_time;")->fetchAll();
$stmt_dbstorage_argon = $conn->query("SELECT used_space,epoch_time FROM db_storage WHERE host_id = 3 ORDER BY epoch_time;")->fetchAll();
$stmt_dbstorage_krypton = $conn->query("SELECT used_space,epoch_time FROM db_storage WHERE host_id = 4 ORDER BY epoch_time;")->fetchAll();
$stmt_available_tickets_helium = $conn->query("SELECT read_tickets,write_tickets,epoch_time FROM tickets_available WHERE host_id=1 ORDER BY epoch_time;")->fetchAll();
$stmt_available_tickets_neon = $conn->query("SELECT read_tickets,epoch_time FROM tickets_available WHERE host_id=2 ORDER BY epoch_time;")->fetchAll();
$stmt_available_tickets_argon = $conn->query("SELECT read_tickets,epoch_time FROM tickets_available WHERE host_id=3 ORDER BY epoch_time;")->fetchAll();
$stmt_available_tickets_krypton = $conn->query("SELECT read_tickets,epoch_time FROM tickets_available WHERE host_id=4 ORDER BY epoch_time;")->fetchAll();
$stmt_connections_helium = $conn->query("SELECT connections,epoch_time FROM connections WHERE host_id = 1 ORDER BY epoch_time;")->fetchAll();
$stmt_connections_neon = $conn->query("SELECT connections,epoch_time FROM connections WHERE host_id = 2 ORDER BY epoch_time;")->fetchAll();
$stmt_connections_argon = $conn->query("SELECT connections,epoch_time FROM connections WHERE host_id = 3 ORDER BY epoch_time;")->fetchAll();
$stmt_connections_krypton = $conn->query("SELECT connections,epoch_time FROM connections WHERE host_id = 4 ORDER BY epoch_time;")->fetchAll();
prof_flag("Retrieve data");
foreach ($stmt_dbstorage_argon as $row) { $used_space_argon[] = "[".$row['epoch_time'].",".$row['used_space']."]"; }
foreach ($stmt_dbstorage_helium as $row) { $used_space_helium[] = "[".$row['epoch_time'].",".$row['used_space']."]"; }
foreach ($stmt_dbstorage_neon as $row) { $used_space_neon[] = "[".$row['epoch_time'].",".$row['used_space']."]"; }
foreach ($stmt_dbstorage_krypton as $row) { $used_space_krypton[] = "[".$row['epoch_time'].",".$row['used_space']."]"; }
foreach ($stmt_available_tickets_helium as $row) {
$read_tickets_helium[] = "[".$row['epoch_time'].",".$row['read_tickets']."]";
$write_tickets_helium[] = "[".$row['epoch_time'].",".$row['write_tickets']."]";
}
foreach ($stmt_available_tickets_neon as $row) {
$read_tickets_neon[] = "[".$row['epoch_time'].",".$row['read_tickets']."]";
$write_tickets_neon[] = "[".$row['epoch_time'].",".$row['write_tickets']."]";
}
foreach ($stmt_available_tickets_argon as $row) {
$read_tickets_argon[] = "[".$row['epoch_time'].",".$row['read_tickets']."]";
$write_tickets_argon[] = "[".$row['epoch_time'].",".$row['write_tickets']."]";
}
foreach ($stmt_available_tickets_krypton as $row) {
$read_tickets_krypton[] = "[".$row['epoch_time'].",".$row['read_tickets']."]";
$write_tickets_krypton[] = "[".$row['epoch_time'].",".$row['write_tickets']."]";
}
foreach ($stmt_connections_helium as $row) {
$connections_helium[] = "[".$row['epoch_time'].",".$row['connections']."]";
}
foreach ($stmt_connections_neon as $row) {
$connections_neon[] = "[".$row['epoch_time'].",".$row['connections']."]";
}
foreach ($stmt_connections_argon as $row) {
$connections_argon[] = "[".$row['epoch_time'].",".$row['connections']."]";
}
foreach ($stmt_connections_krypton as $row) {
$connections_krypton[] = "[".$row['epoch_time'].",".$row['connections']."]";
}
prof_flag("Done");
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' secs';
prof_print();
?>
总执行时间:34.110798120499秒 开始 0.000032 连接到数据库 0.045901 执行查询 9.475136 检索数据 24.588582 完成
使用我的foreach语句检索数据部分花费的时间最多。
您有什么想法可以让我更快吗?一个查询返回大约5万至15万行。
谢谢!
解决方法
您可以通过使用
将所有查询合并为一个查询来优化代码...WHERE host_id IN (1,2,3,4)
或使用
UNION
这将最大程度地减少所有数据库访问开销。
4 x 50k-150k行很多,您真的一次需要它们吗?也许您可以使用批处理作业来处理它们,缓存结果并仅访问缓存?