Database Function VS Case Statement

前端 未结 3 1287
萌比男神i
萌比男神i 2021-02-20 14:11

Yesterday we got a scenario where had to get type of a db field and on base of that we had to write the description of the field. Like

Select ( Cas         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-20 14:38

    When using a scalar function (a function that returns one value) the contents of the function will be executed once per row but the case statement will be executed across the entire set.

    By operating against the entire set you allow the server to optimise your query more efficiently.

    So the theory goes that the same query run both ways against a large dataset then the function should be slower. However, the difference may be trivial when operating against your data so you should try both methods and test them to determine if any performance trade off is worth the increased utility of a function.

提交回复
热议问题