Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell...

24
Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte

Transcript of Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell...

Page 1: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October

The Query GovernorThe Query Governor

Richard CampbellStephen Forte

Richard CampbellStephen Forte

Page 2: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Speakers.Bio.ToPhoto()Speakers.Bio.ToPhoto()

Page 3: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

Page 4: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

What is a Query Governor?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

Page 5: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

What is a Query Governor?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.

Page 6: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

What is a Query Governor?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

Page 7: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

What is a Query Governor?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

Page 8: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating the QueryEvaluating 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

Page 9: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating the QueryEvaluating 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

Page 10: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating the QueryEvaluating 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!

Page 11: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating the QueryEvaluating 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

Page 12: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating the QueryEvaluating 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.

Page 13: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating Server StatusEvaluating 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

Page 14: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating Server StatusEvaluating 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

Page 15: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating Server StatusEvaluating 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!

Page 16: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating Server StatusEvaluating 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

Page 17: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Evaluating Server StateEvaluating 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

Page 18: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Putting it All TogetherPutting 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

Page 19: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

ImplementationImplementation

● 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

Page 20: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

ImplementationImplementation

● 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

Page 21: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

ImplementationImplementation

● 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

Page 22: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 October

Questions?Questions?

Page 23: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 OctoberSofia, 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

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!Thank you!

Page 24: Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell Stephen Forte.

Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October