snippetsqlMinor
SQL Server 2012 CPU spike due to LinkedServer - how to find the offending query?
Viewed 0 times
spikeduefind2012thesqllinkedserverqueryhowserver
Problem
As part of another question (SQL Server 2012 performance issues (maybe due to linked server)) I've identified a CPU spike to be directly related to a spike in Bytes Received, which leads me to believe it is likely due to the link server.
THE ENVIRONMENT
2 servers, one server accesses a database on the first via a linked server, for nice diagram, please see SQL Server 2012 performance issues (maybe due to linked server)
CPU spike directly related to Bytes Received Spike
QUESTION
How to find which queries could be causing it??
TYPES OF QUERIES WE RUN
I've went through all our stored procedures, and they are optimized. Here are the types of linked server access that we do:
select one value from remote table
execute stored procedure that returns result set of max few hundred rows
execute stored procedure that returns one value
delete a few rows from remove table
Update one row in remote table, or add one row to remote table
I believe all of these are optimized, yet, it appears we are joining tables over the linked server!
Some interesting wait stats that I do not know how to interpret.
Ran a query from http://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/
I get this DURING the CPU spike:
I get this when there is no CPU spike:
These wait times are on the server with DB2, the SQL 2012 server which accesses a remote database on an
THE ENVIRONMENT
2 servers, one server accesses a database on the first via a linked server, for nice diagram, please see SQL Server 2012 performance issues (maybe due to linked server)
CPU spike directly related to Bytes Received Spike
QUESTION
How to find which queries could be causing it??
TYPES OF QUERIES WE RUN
I've went through all our stored procedures, and they are optimized. Here are the types of linked server access that we do:
select one value from remote table
select @userid = userid from FBGC.FBGCommon.dbo.players where playerid = @playeridexecute stored procedure that returns result set of max few hundred rows
exec FBGC.Fbgcommon.dbo.qPlayersFriends @PlayerIDexecute stored procedure that returns one value
exec @HasEnoughCredits = FBGC.FBGCommon.dbo.qDoesPlayerHaveEnoughCredits @PlayerID, @Costdelete a few rows from remove table
delete from FBGC.FBGCommon.dbo.InactivePlayersToBeWarned where PlayerID = @AccountOwnerPlayerIDUpdate one row in remote table, or add one row to remote table
update FBGC.FBGCommon.dbo.users set XP = XP + @XPForTitle where userid = @userid
insert into FBGC.FBGCommon.dbo.UserXPHistory (userid, time, XPReceived, XPFromTypeID, XPFromRealmID) values (@UserID, GETDATE(), @XPForTitle, 1, @ThisRealmID)I believe all of these are optimized, yet, it appears we are joining tables over the linked server!
Some interesting wait stats that I do not know how to interpret.
Ran a query from http://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/
I get this DURING the CPU spike:
I get this when there is no CPU spike:
These wait times are on the server with DB2, the SQL 2012 server which accesses a remote database on an
Solution
To answer the question: How to find the offending query:
Since the spikes in the graph you posted last for several minutes you have plenty of time to use the following method:
Download sysinternals process explorer
Use this query and lookup the query that is currently being executed by that thread:
Since the spikes in the graph you posted last for several minutes you have plenty of time to use the following method:
Download sysinternals process explorer
- start process explorer and find the SQL Server process.
- right click and select properties
- look at the thread tab.
- Sort on the CPU column and note the thread id (TID) that is consuming the most CPU.
Use this query and lookup the query that is currently being executed by that thread:
SELECT r.session_id, st.text, qp.query_plan
FROM sys.dm_os_threads AS ot
JOIN sys.dm_os_tasks AS t
ON t.worker_address = ot.worker_address
JOIN sys.dm_exec_requests AS r
ON t.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp
WHERE os_thread_id = Code Snippets
SELECT r.session_id, st.text, qp.query_plan
FROM sys.dm_os_threads AS ot
JOIN sys.dm_os_tasks AS t
ON t.worker_address = ot.worker_address
JOIN sys.dm_exec_requests AS r
ON t.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp
WHERE os_thread_id = <thread id>Context
StackExchange Database Administrators Q#54463, answer score: 3
Revisions (0)
No revisions yet.