reuse sql with view or function

前端 未结 4 537
说谎
说谎 2021-01-08 00:00

I have a sql query that I will be reusing in multiple stored procedures. The query works against multiple tables and returns an integer value based on 2 variables passed to

4条回答
  •  广开言路
    2021-01-08 00:24

    If you will always be using the same parametrised predicate to filter the results then I'd go for a parametrised inline table valued function. In theory this is treated the same as a View in that they both get expanded out by the optimiser in practice it can avoid predicate pushing issues. An example of such a case can be seen in the second part of this article.

    As Andomar points out in the comments most of the time the query optimiser does do a good job of pushing down the predicate to where it is needed but I'm not aware of any circumstance in which using the inline TVF will perform worse so this seems a rational default choice between the two (very similar) constructs.

    The one advantage I can see for the View would be that it would allow you to select without a filter or with different filters so is more versatile.

    Inline TVFs can also be used to replace scalar UDFs for efficiency gains as in this example.

提交回复
热议问题