我有一个应用程序,需要通过大量条目更新大量数据。基本上,它执行约7,000次插入和/或更新,但需要花费很长的时间(例如将近9分钟…平均每个查询约0.08秒)。从本质上讲,我正在寻求提高速度来处理多个此类请求(我不希望对我模糊的示例提出具体的答案……只是希望,可以帮助解释)。
以下是对请求进行概要分析的一些示例:
SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (timestamp = '2010-10-15T07:30:00-07:00') AND (planet_id = '2010_Gl_581_c')
INSERT INTO `habitable_planets` (`planet_id`, `timestamp`, `weather_air_temp`, `weather_cell_temp`, `weather_irradiance`, `weather_wind_float`, `biolumin_to_date`, `biolumin_detected`, `craft_energy_usage`, `craft_energy_consumed_to_date`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (timestamp = '2010-10-15T07:45:00-07:00') AND (planet_id = '2010_Gl_581_c')
INSERT INTO `habitable_planets` (`planet_id`, `timestamp`, `weather_air_temp`, `weather_cell_temp`, `weather_irradiance`, `weather_wind_float`, `biolumin_to_date`, `biolumin_detected`, `craft_energy_usage`, `craft_energy_consumed_to_date`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
重复一遍恶作剧(大约7,000次)。这是一个更新,它收集在24小时内定期生成的数据,然后每天对数据库进行一次大规模更新。鉴于我所展示的有限,您对加快此过程有什么建议吗?
例如…是否有意义,而不是对每个时间戳进行选择,而是一次对一个范围进行选择,然后在脚本中对其进行迭代?
模糊地喜欢:
SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (planet_id = '2010_Gl_581_c')
将该结果分配给$foo然后执行:
foreach ($foo as $bar)
{
if ($bar['timestamp'] == $baz) // where $baz is the needed timestamp
{
// do the insert here
}
}
编辑: 对此添加一点,在我的情况下,提高了响应能力的一件事是更改了一堆代码,以检查是否存在现有记录,并根据结果使用INSERT... ON
DUPLICATE KEY UPDATESQL查询进行插入或更新。在我的特殊情况下,这导致大约30%的速度提高,因为它使方程式中至少减少了一次数据库访问,并且确实增加了数千个请求。