Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell...
-
Upload
quentin-griffin -
Category
Documents
-
view
215 -
download
2
Transcript of Sofia, Bulgaria | 9-10 October The Query Governor Richard Campbell Stephen Forte Richard Campbell...
Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October
The Query GovernorThe Query Governor
Richard CampbellStephen Forte
Richard CampbellStephen Forte
Sofia, Bulgaria | 9-10 October
Speakers.Bio.ToPhoto()Speakers.Bio.ToPhoto()
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
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
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.
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
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
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
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
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!
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
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.
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
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
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!
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
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
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
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
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
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
Sofia, Bulgaria | 9-10 October
Questions?Questions?
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!
Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October