Occasionally, I get production performance issues of a long running SQL query, that is consuming lots of system resources and is running for much longer than usual.
You can see the long running query simply by using sp_who2, or some variant, that displays active SQL queries. So you now have the SPID of the running query that is taking a long time. I would typically then want to get the SQL command.
You can easily get the SQL for this query by using dbcc inputbuffer(SPID). I might then use the sql query to get a show plan to see how it should execute. You can quite easily do this by cutting and pasting the SQL into a new window and requesting a show plan. However, sometimes the query plan that is running is not the same as the query plan that SQL Server tells you it would use now. This can be for a variety of reasons including new statistics, parameter sniffing and different connection properties.
So what you really want to get is the plan that is currently executing. You can obtain this from the sys.dm_exec_query_plan table. First of all you will need the plan handle, which you can get from sys.dm_exec_requests using the SPID as the session_id.
Use the plan_handle to get the plan from sys.dm_exec_query_plan. The query plan will return as an xml document. Simply click on the xml in the query results window for SQLEM to open the query plan in a new window.
Now you have the query plan that is actually executing and you can compare that to the query plan you expected SQL to use. From here you can tune the query to use a more appropriate query plan.
Note, I would encourage you to use all other options before using index or join hints to get the query plan you desire. This is because once you override the optimiser with your index or join hint, it will always use that hint even when your table structures or data changes so much that it is obviously silly. Optimiser hints such as FASTFIRSTROW, redundant predicates etc are OK with me, as they don't override the optimiser, they just tell the optimiser that you want a plan that will get the first row fast.
Monday, January 13, 2014
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment