Getting a SQL Server Query Plan
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Getting a SQL Server Query Plan[ Team LiB ]Recipe 10.9 Getting a SQL Server Query PlanProblemYou need to retrieve information about how query statements are executed by the SQLServer.SolutionUse the SET SHOWPLAN_TEXT statement.The sample code executes the SET SHOWPLAN_TEXT statement, using theExecuteNonQuery( ) method of the Command object, to retrieve how query statementsare executed by the SQL Server.The C# code is shown in Example 10-9.Example 10-9. File: ShowPlanForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// . . .StringBuilder sb = new StringBuilder( );// Open a new connection.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);// Create and execute the command to retrieve the plan.SqlCommand cmd = new SqlCommand(SET SHOWPLAN_TEXT ON, conn);conn.Open( );cmd.ExecuteNonQuery( );// Create the command to get the plan for.cmd.CommandText = SELECT * FROM Customers WHERE Country=USA + ORDER BY CompanyName;// Retrieve the plan into DataReader.SqlDataReader dr = cmd.ExecuteReader( );// Iterate over all result sets and all rows to get plan.do{ while (dr.Read( )) sb.Append(dr.GetString(0) + Environment.NewLine); sb.Append(Environment.NewLine);} while(dr.NextResult( ));dr.Close( );// Create and execute the command to retrieve query results.cmd = new SqlCommand(SET SHOWPLAN_TEXT OFF, conn);cmd.ExecuteNonQuery( );conn.Close( );resultTextBox.Text = sb.ToString( );DiscussionThe SQL SET statement alters current session handling of specific information. Table 10-4 describes the categories of SET statements. Table 10-4. SET statement categories Category DescriptionDate and Time Alters current session settings for handling of date and time dataLocking Alters current session settings for handling SQL Server locking Alters current session settings for miscellaneous SQL ServerMiscellaneous functionalityQuery Alters current session settings for query execution and processingExecutionSQL-92 Settings Alters current session settings for using SQL-92 default settingsStatistics Alters current session settings for displaying statisticsTransactions Alters current session settings for handling SQL Server TransactionsWhen SHOWPLAN_TEXT (from the Query Execution category) is ON, SQL Serverreturns a result set containing detailed information about how the SQL statements aregoing to be executed rather than actually executing the statements. Two result sets arereturned for each statement, both containing a single column StmtText. The first result setcontains the SQL statement while the second contains rows detailing the plan. For batchSQL statements, the result sets alternate between statement and plan for each statement inthe batch.SHOWPLAN_TEXT does not need to be explicitly set to OFF. It only affects thecommand issued subsequent to the statement in which it is SET ON, not all of thecommands executed while the connection object is open.SHOWPLAN_ALL returns more information about the plan than just the StmtTextcolumn but is turned on and off in the same way.For more information about the SET statement, SHOWPLAN_TEXT, orSHOWPLAN_ALL, see the topic SET in Microsoft SQL Server Books Online.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Getting a SQL Server Query PlanGợi ý tài liệu liên quan:
-
52 trang 431 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 318 0 0 -
74 trang 302 0 0
-
96 trang 296 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 283 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 267 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 266 0 0 -
64 trang 264 0 0
-
Bài giảng An toàn và bảo mật thông tin - Trường đại học Thương Mại
31 trang 255 0 0 -
47 trang 231 0 0
-
Giáo trình Hệ điều hành: Phần 2
53 trang 221 0 0 -
LUẬN VĂN: TÌM HIỂU PHƯƠNG PHÁP HỌC TÍCH CỰC VÀ ỨNG DỤNG CHO BÀI TOÁN LỌC THƯ RÁC
65 trang 216 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng quản lý kho hàng trên nền Web
61 trang 215 0 0 -
83 trang 213 0 0
-
Giáo trình Autocad - Nghề: Quản trị mạng máy tính - Trình độ: Cao đẳng nghề (Phần 2)
52 trang 210 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
BÀI GIẢNG KINH TẾ CHÍNH TRỊ MÁC - LÊNIN - TS. NGUYỄN VĂN LỊCH - 5
23 trang 205 0 0