udf vs direct sql performance

后端 未结 1 1277
我在风中等你
我在风中等你 2021-01-18 19:52

Using MSSQL 2005

I was playing around today with a Scalar UDF in a where statement to see some of the costs associated with making the call and io differences etc.

相关标签:
1条回答
  • 2021-01-18 20:16
    • If you want to join on Purchases.CustomerID you should put an index on it.
    • If you often query on value ranges you should put an index on that too.

    As it is you are asking SQL server to choose between two bad plans.

    SQL Server can guess roughly how many purchases will be covered by the > 1000 query, and will pick a plan based on that.

    However it can't guess how many will be covered by the UDF query, so may pick a different plan. Because it is going on ignorance it might be better or worse than the other plan depending on how good it's guess is.

    You can see the plans generated and it will tell you the estimated number of rows in each plan, and also the actual number. Those estimated numbers account for the choice of plan in each case.

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