Use of With Clause in SQL Server

前端 未结 3 1035
北恋
北恋 2020-12-21 11:17

How does with clause work in SQL Server? Does it really give me some performance boost or does it just help to make more readable scripts?

When it is ri

3条回答
  •  生来不讨喜
    2020-12-21 12:09

    I'm not entirely sure about performance advantages, but I think it can definitely help in the case where using a subquery results in the subquery being performed multiple times.

    Apart from that it can definitely make code more readable, and can also be used in the case where multiple subqueries would be a cut and paste of the same code in different places.

    What should you know before you use it? A big downside is that when you have a CTE in a view, you cannot create a clustered index on that view. This can be a big pain because SQL Server does not have materialised views, and has certainly bitten me before.

提交回复
热议问题