Download presentation
Presentation is loading. Please wait.
Published byQuentin Griffin Modified over 9 years ago
1
Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte
2
Sofia, Bulgaria | 9-10 October Speakers.Bio.ToPhoto()
3
Sofia, Bulgaria | 9-10 October Agenda ●What is a Query Governor? ●Evaluating the Query ●Evaluating the State of the Server ●Putting it All Together ●Implementation and Enforcement ●What is a Query Governor? ●Evaluating the Query ●Evaluating the State of the Server ●Putting it All Together ●Implementation and Enforcement
4
Sofia, Bulgaria | 9-10 October What is a Query Governor? ●Controls what queries can be run ●Two standard techniques: ●By cost ●By time ●Controls what queries can be run ●Two standard techniques: ●By cost ●By time
5
Sofia, Bulgaria | 9-10 October What is a Query Governor? ●SET QUERY_GOVERNOR_COST_LIMIT ●No queries above this value will execute ●EVER! ●You have to modify queries to stay below the limit ●Add indexes, break down steps, etc. ●SET QUERY_GOVERNOR_COST_LIMIT ●No queries above this value will execute ●EVER! ●You have to modify queries to stay below the limit ●Add indexes, break down steps, etc.
6
Sofia, Bulgaria | 9-10 October What is a Query Governor? ●Enforced outside SQL Server ●Set a maximum amount of time to execute a query ●If the query exceeds that time, it fails ●You have to run the query to see if it takes too long ●Enforced outside SQL Server ●Set a maximum amount of time to execute a query ●If the query exceeds that time, it fails ●You have to run the query to see if it takes too long
7
Sofia, Bulgaria | 9-10 October What is a Query Governor? ●Assess the cost of the query ●Assess the state of the server ●Algorithmically decide whether or not to execute ●Under low load conditions, more queries can run ●Under high load, only high priority queries run ●Assess the cost of the query ●Assess the state of the server ●Algorithmically decide whether or not to execute ●Under low load conditions, more queries can run ●Under high load, only high priority queries run
8
Sofia, Bulgaria | 9-10 October Evaluating the Query ●SQL Server 2005 will return query plans in XML format ●Makes it easy to programmatically extract useful information from a query, such as cost ●But you can’t turn showplan_xml on and off easily ●And that’s where the CLR comes in ●SQL Server 2005 will return query plans in XML format ●Makes it easy to programmatically extract useful information from a query, such as cost ●But you can’t turn showplan_xml on and off easily ●And that’s where the CLR comes in
9
Sofia, Bulgaria | 9-10 October Evaluating the Query ●The code to fetch the query plan is written in C# ●It takes the SQL statement and runs showplan_xml on it ●Loads that xml into a variable and returns it as a string ●The code to fetch the query plan is written in C# ●It takes the SQL statement and runs showplan_xml on it ●Loads that xml into a variable and returns it as a string
10
Sofia, Bulgaria | 9-10 October Evaluating the Query ●Using the C# code in SQL Server ●Turn on CLR ●Create an assembly from the compiled DLL ●Create a stored procedure that uses the assembly ●Go Crazy! ●Using the C# code in SQL Server ●Turn on CLR ●Create an assembly from the compiled DLL ●Create a stored procedure that uses the assembly ●Go Crazy!
11
Sofia, Bulgaria | 9-10 October Evaluating the Query ●The evaluator code will check any query ●Actual SQL statements like SELECT ●Stored procedures ●Showplan knows what kind of query is about to be run ●Could be useful for SQL Injection defense ●The evaluator code will check any query ●Actual SQL statements like SELECT ●Stored procedures ●Showplan knows what kind of query is about to be run ●Could be useful for SQL Injection defense
12
Sofia, Bulgaria | 9-10 October Evaluating the Query ●Use XQuery to extract data from the XML Showplan ●An expression language for finding XML elements ●Handles typing, multiple elements, etc. ●Use XQuery to extract data from the XML Showplan ●An expression language for finding XML elements ●Handles typing, multiple elements, etc.
13
Sofia, Bulgaria | 9-10 October Evaluating Server Status ●Could have written more C# code to pull WMI data ●But why? ●PerfMon Counter Logs write to SQL Server ●Simple, easy, effective ●Could have written more C# code to pull WMI data ●But why? ●PerfMon Counter Logs write to SQL Server ●Simple, easy, effective
14
Sofia, Bulgaria | 9-10 October Evaluating Server Status ●Avg. Disk Queue Length ●If its growing, your disk is falling behind ●Page Faults/sec ●If this number is high, you’re probably swapping memory to disk ●Processor Queue Length ●Tasks waiting for processor type, could be an indicator of a lot of queries running ●Avg. Disk Queue Length ●If its growing, your disk is falling behind ●Page Faults/sec ●If this number is high, you’re probably swapping memory to disk ●Processor Queue Length ●Tasks waiting for processor type, could be an indicator of a lot of queries running
15
Sofia, Bulgaria | 9-10 October Evaluating Server Status ●There are thousands of counters ●Counters for system status ●Counters specific to SQL Server ●You can pick whatever you like! ●There are thousands of counters ●Counters for system status ●Counters specific to SQL Server ●You can pick whatever you like!
16
Sofia, Bulgaria | 9-10 October Evaluating Server Status ●Use queries to get an overall picture of the status of the server ●The correct numbers depend on the server! ●You have to account for other work your server might be doing ●Determine baseline numbers specific to the way your server is working ●Use queries to get an overall picture of the status of the server ●The correct numbers depend on the server! ●You have to account for other work your server might be doing ●Determine baseline numbers specific to the way your server is working
17
Sofia, Bulgaria | 9-10 October Evaluating Server State ●You want a single assessment: Low, Medium, High ●Use a stored procedure to check relevant counters ●Write the assessment as a log entry ●Use the Agent to run the stored procedure routinely ●You want a single assessment: Low, Medium, High ●Use a stored procedure to check relevant counters ●Write the assessment as a log entry ●Use the Agent to run the stored procedure routinely
18
Sofia, Bulgaria | 9-10 October Putting it All Together ●Develop a matrix of assessment ●Cost of the query ●State of the server ●Who is running this query? ●Write algorithms to make decisions ●Again, this depends on your situation, its very flexible ●Develop a matrix of assessment ●Cost of the query ●State of the server ●Who is running this query? ●Write algorithms to make decisions ●Again, this depends on your situation, its very flexible
19
Sofia, Bulgaria | 9-10 October Implementation ●Put the governor at the top of each stored procedure ●Requires following the rules ●Total cost inside a stored procedure gets challenging ●Put the governor at the top of each stored procedure ●Requires following the rules ●Total cost inside a stored procedure gets challenging
20
Sofia, Bulgaria | 9-10 October Implementation ●Call the governor before executing ●Simple to implement ●Again, relies on good behavior ●Call the governor before executing ●Simple to implement ●Again, relies on good behavior
21
Sofia, Bulgaria | 9-10 October Implementation ●All queries through the governor ●No rights to tables ●Governor executes query ●Works for dynamic SQL ●Not so good for stored procs ●All queries through the governor ●No rights to tables ●Governor executes query ●Works for dynamic SQL ●Not so good for stored procs
22
Sofia, Bulgaria | 9-10 October Questions?
23
Sofia, Bulgaria | 9-10 October Please fill out the survey forms! They are the key to amazing prizes that you can get at the end of each day Thank you!
24
Sofia, Bulgaria | 9-10 October
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.