需要加快WHERE NOT EXISTS查询

时间:2018-12-19 21:40:20

标签: mysql sql subquery left-join

我相信这会导致5分钟到20分钟的延迟,具体取决于记录的数量。我需要将其转换为LEFT JOIN,但需要一些帮助才能实现。

qry_arr = array(':bill_type' => "INT");
$sql = "update ".$billing_table." c set c.bill_type = :bill_type";
$sql .= " WHERE  NOT EXISTS (SELECT s.abbreviation FROM   state s WHERE  s.abbreviation = c.out_location)";
$sql .= " and c.out_location != 'UNKNOWN' and c.out_location != ''";

1 个答案:

答案 0 :(得分:1)

UPDATE $billing_table c
LEFT JOIN state s ON s.abbreviation = c.out_location
SET c.bill_type = :bill_type
WHERE s.abbreviation IS NULL
AND c.out_location NOT IN ('UNKNOWN', '')

对于与不匹配的行,这基本上与SELECT的语法相同。参见Return row only if value doesn't exist。只需将SELECT ... FROM替换为UPDATE,然后在SET之前插入WHERE子句。

确保在out_locationabbreviation上有索引。