gpt4 book ai didi

php - LEFT JOIN 在 PHP 中执行时非常慢

转载 作者:行者123 更新时间:2023-11-29 02:57:40 26 4
gpt4 key购买 nike

我已经面对这个问题好几天了,我不知道该怎么办,当我在 mysql 中执行这个查询时大约需要 8 秒:

CREATE TEMPORARY TABLE temp_forecast AS 
(SELECT gfs.spot, wind, winddir, clouds, temperature, gust,
precipitation, pressure, humidity, wavedir, waveperiod,
waveheight, gfs.hour, gfs.UTCdate, localdate, modelcicle,
tide, sst
FROM table1 AS gfs
left outer join table2 AS waves
on gfs.spot=waves.spot AND gfs.utcdate=waves.utcdate)

我对这个查询执行了解释,索引没问题,它说它只需要为 gfs 的每一行检查 waves 表中的一行,但是当我在 PHP 中执行它时,它可能需要长达 20 分钟的时间使服务器过载,我不知道发生了什么

我还尝试了 RESET QUERY CACHE; 在 mysql 中执行查询之前查看它是否被缓存,但仍然需要 8 秒,似乎查询在 PHP 中执行时没有使用索引,插入后立即执行查询,也许索引还没有准备好?

这是在 PHP 上执行查询的代码:

public function dumpData(Logger $log){
$q['sql'] = sprintf("CREATE TEMPORARY TABLE temp_forecast AS (SELECT gfs.spot, wind, winddir, clouds, temperature, gust, precipitation, pressure, humidity, wavedir, waveperiod,
waveheight, gfs.hour, gfs.UTCdate, localdate, modelcicle, tide, sst FROM %s AS gfs left outer join %s AS waves on
gfs.spot=waves.spot AND gfs.utcdate=waves.utcdate",
$this->GFSforecast,
$this->wavesForecast);
$q['ret'] = 'row_count';
$this->pdo->smartQuery($q, __FUNCTION__);
$q['sql'] = sprintf("TRUNCATE TABLE %s", $this->forecast);
$q['ret'] = 'row_count';
$this->pdo->smartQuery($q, __FUNCTION__);
$q['sql'] = sprintf("INSERT INTO %s (SELECT * FROM temp_forecast)", $this->forecast);
$q['ret'] = 'row_count';
return $this->pdo->smartQuery($q, __FUNCTION__);
}

最佳答案

waves 上,有复合 INDEX(spot, utcdate)

如果那不能解决问题,请告诉我们 SHOW CREATE TABLE 并告诉我们每个表有多大。

重新填充表的更好方法是

  1. CREATE TABLE new LIKE 预测;
  2. 插入新的选择...;
  3. 将 TABLE 预测重命名为旧预测,将新预测重命名为预测;
  4. 删除旧表;

您永远不会没有表格预测; RENAME 是原子的并且是“即时的”。因此,即使 SELECT(第 2 步)很慢,也没有什么坏处(除了延迟更新)。

关于php - LEFT JOIN 在 PHP 中执行时非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com