Which SQL statement is faster? (HAVING vs. WHERE…)

后端 未结 8 1592
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 20:18
SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU
    FROM  PRACOWNICY
    GROUP BY NR_DZIALU
    HAVING NR_DZIALU = 30

or

SE         


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-02 20:37

    The theory (by theory I mean SQL Standard) says that WHERE restricts the result set before returning rows and HAVING restricts the result set after bringing all the rows. So WHERE is faster. On SQL Standard compliant DBMSs in this regard, only use HAVING where you cannot put the condition on a WHERE (like computed columns in some RDBMSs.)

    You can just see the execution plan for both and check for yourself, nothing will beat that (measurement for your specific query in your specific environment with your data.)

提交回复
热议问题