Performance of SQL “EXISTS” usage variants

后端 未结 9 689
-上瘾入骨i
-上瘾入骨i 2020-12-05 04:39

Is there any difference in the performance of the following three SQL statements?

SELECT * FROM tableA WHERE EXISTS (SELECT * FROM tableB WHERE tableA.x = ta         


        
9条回答
  •  粉色の甜心
    2020-12-05 05:35

    In addition to what others have said, the practice of using SELECT 1 originated on old Microsoft SQL Server (prior 2005) - its query optimizer wasn't clever enough to avoid physically fetching fields from the table for SELECT *. No other DBMS, to my knowledge, has this deficiency.

    The EXISTS tests for existence of rows, not what's in them, so other than some optimizer quirk similar to above, it doesn't really matter what's in the SELECT list.

    The SELECT * seems to be most usual, but others are acceptable as well.

提交回复
热议问题