SQL 来自 Azure 的强制参数化建议

SQL Forced parameterization recommendation from Azure

我在我的项目中使用 Azure SQL 数据库,并且在其中非常频繁地执行一些相同的查询集。 最近我收到了一条性能建议,上面写着 - Non-Parameterized queries are causing performance issues。并建议在我的数据库中执行以下语句。

ALTER DATABASE [TestDB] SET PARAMETERIZATION FORCED

我了解到强制参数化可以通过降低查询编译和重新编译的频率来提高某些数据库的性能。 此外,众所周知,存储过程是可执行代码,会自动缓存并在用户之间共享,并且可以防止重新编译。

请帮我解决下面列出的问题。

1)将数据库转换为强制参数化是否比将常用查询转换为存储过程更好?

2) 在我的数据库中执行强制参数化选项是否安全?

如果不进行进一步测试,很难说它是否会更好地工作,但如果顾问告诉您启用参数化会提高性能,那么您绝对应该尝试一下。原因如下:

You can apply this recommendation quickly and easily by clicking on the Apply command. Once you apply this recommendation, it will enable forced parameterization within minutes on your database and it starts the monitoring process which approximately lasts for 24 hours. After this period, you will be able to see the validation report that shows CPU usage of your database 24 hours before and after the recommendation has been applied. SQL Database Advisor has a safety mechanism that automatically reverts the applied recommendation in case a performance regression has been detected.

更多信息在这里:

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-advisor#parameterize-queries-recommendations

1)Do turning database into Forced PARAMETERIZATION would work better than making frequently used queries into stored procedures?

没有。 Forced Parameterization 是针对未正确参数化查询的应用程序的解决方法。最好对频繁 运行 查询使用参数,并在您希望计划基于单个值的地方使用硬编码值。

EG

select * 
from Orders
where CustomerId = @customerID
and Active = 1