我有这样的疑问:

select distinct somecolumn from sometable
          where something = somethingelse and
          someid not in (select anotherid from another tabele where ...);

我认为由于子选择,查询运行得非常慢。是否有方法重写,以便可以删除“不在”和子选择?

最佳答案

使用LEFT JOIN

SELECT  someClumn
FROM    sometable a
        LEFT JOIN anotherTable b
            ON a.someID = b.anotherID
               -- AND condition for anotherTable
WHERE   a.something = somethingelse AND
        b.anotherID IS NULL

为了获得更好的性能,请在列上定义索引:someIDanotherID

10-02 19:37