mySQL NOT IN SubQuery 耗时太长

mySQL NOT IN SubQuery takes too long

我想使用 table 查看街道。

  1. 查询不是最优的
  2. 查询时间过长

谁能帮帮我

SELECT id, strasse, plz
FROM `adress`
WHERE strasse NOT IN (
    SELECT street
    FROM post_plz
    WHERE `street` like adress.strasse AND plz like adress.plz
)
LIMIT 5; 

您可以尝试在 table 之间使用左连接并检查不匹配的值

SELECT id, strasse, plz 

from `adress` 
left join  post_plz on strasse = street
  AND `street` like adress.strasse 
    AND plz like adress.plz
WHERE street is null
 LIMIT 5;
SELECT id, strasse, plz
FROM adress
WHERE NOT EXISTS ( SELECT NULL
                   FROM post_plz
                   WHERE post_plz.street = adress.strasse 
                     AND post_plz.plz = adress.plz )
-- ORDER BY {expression}  
LIMIT 5

没有 ORDER BY 查询 returns non-deterministic 输出(两次查询执行可能产生不同的输出)。所以强烈建议添加一些行排序。

指数 post_plz (adress, plz)adress (strasse, plz, id) 必须改善。