Will using a Function to simplify SQL Query massively imapact performance?

前端 未结 1 1542
遥遥无期
遥遥无期 2021-01-24 07:52

I have a query which uses a complicated set of CASE statements, some nested, some with a COALESCE of CASE statements, and it is a pain to manage.

The same basic logic ap

相关标签:
1条回答
  • 2021-01-24 08:26

    Normally, selecting a result of a scalar function won't hurt much, but filtering by it may easily cost hundreds of seconds (not necessarily though).

    If you need to filter by a scalar function result (WHERE col = dbo.scalar_function()), it often helps to make an inline table-valued function instead. It would return its value as the only row of the result table. You would then do inner join with the function result, effectively filtering by the returned value. This works because SQL Server is always able to unwind inline table-valued functions and inline them into the calling query.

    Note this trick won't work if the function is a multi-step one. These cannot be unwound.

    0 讨论(0)
提交回复
热议问题