如何使用c#从SQL服务器获得一个估计的执行计划?

时间:2022-09-02 14:48:12

I know how to get it using SSMS, but how would I get the execution plan using C# and ADO.NET? (Assume I know how to handle normal queries in C#.)

我知道如何使用SSMS获得它,但是如何使用c#和ADO.NET获得执行计划?(假设我知道如何处理c#中的正常查询。)

1 个解决方案

#1


3  

Looks like you can use the below query taken from Are there any way to programmatically execute a query with Include Actual Execution Plan and see whether any index suggestion or not

看起来您可以使用下面的查询从是否有任何方法以编程方式执行包含实际执行计划的查询,并查看是否有任何索引建议

command.CommandText = "SET STATISTICS XML ON";

Also, you can use SHOWPLAN_ALL option and can set it in your stored procedure probably like SET SHOWPLAN_ALL ON (Though I have never tested that personally. So just a suggestion)

此外,您可以使用SHOWPLAN_ALL选项,并可以在存储过程中设置它,比如设置SHOWPLAN_ALL ON(尽管我从未亲自测试过这个选项)。所以只是一个建议)

When SET SHOWPLAN_ALL is ON, SQL Server returns execution information for each statement without executing it, and Transact-SQL statements are not executed. After this option is set ON, information about all subsequent Transact-SQL statements are returned until the option is set OFF. For example, if a CREATE TABLE statement is executed while SET SHOWPLAN_ALL is ON, SQL Server returns an error message from a subsequent SELECT statement involving that same table, informing users that the specified table does not exist. Therefore, subsequent references to this table fail. When SET SHOWPLAN_ALL is OFF, SQL Server executes the statements without generating a report.

当设置SHOWPLAN_ALL为ON时,SQL Server返回每个语句的执行信息而不执行它,并且不会执行Transact-SQL语句。此选项设置后,所有后续transact - SQL语句返回的信息,直到选项设置。例如,如果一个执行CREATE TABLE语句集SHOWPLAN_ALL的同时,SQL Server从随后的SELECT语句返回一个错误消息涉及同样的表,通知用户指定的表不存在。因此,对该表的后续引用将失败。当SET SHOWPLAN_ALL关闭时,SQL Server执行语句而不生成报告。

#1


3  

Looks like you can use the below query taken from Are there any way to programmatically execute a query with Include Actual Execution Plan and see whether any index suggestion or not

看起来您可以使用下面的查询从是否有任何方法以编程方式执行包含实际执行计划的查询,并查看是否有任何索引建议

command.CommandText = "SET STATISTICS XML ON";

Also, you can use SHOWPLAN_ALL option and can set it in your stored procedure probably like SET SHOWPLAN_ALL ON (Though I have never tested that personally. So just a suggestion)

此外,您可以使用SHOWPLAN_ALL选项,并可以在存储过程中设置它,比如设置SHOWPLAN_ALL ON(尽管我从未亲自测试过这个选项)。所以只是一个建议)

When SET SHOWPLAN_ALL is ON, SQL Server returns execution information for each statement without executing it, and Transact-SQL statements are not executed. After this option is set ON, information about all subsequent Transact-SQL statements are returned until the option is set OFF. For example, if a CREATE TABLE statement is executed while SET SHOWPLAN_ALL is ON, SQL Server returns an error message from a subsequent SELECT statement involving that same table, informing users that the specified table does not exist. Therefore, subsequent references to this table fail. When SET SHOWPLAN_ALL is OFF, SQL Server executes the statements without generating a report.

当设置SHOWPLAN_ALL为ON时,SQL Server返回每个语句的执行信息而不执行它,并且不会执行Transact-SQL语句。此选项设置后,所有后续transact - SQL语句返回的信息,直到选项设置。例如,如果一个执行CREATE TABLE语句集SHOWPLAN_ALL的同时,SQL Server从随后的SELECT语句返回一个错误消息涉及同样的表,通知用户指定的表不存在。因此,对该表的后续引用将失败。当SET SHOWPLAN_ALL关闭时,SQL Server执行语句而不生成报告。