The timeout period elapsed.. in my query - sql

I 've a query that links many tables. So when executes a stored procedure it gives...
Timeout expired.
The timeout period elapsed prior to completion of the operation or the server is not responding.
What can I do for this.. May I have to increase the time out of the SQL server. I'm using SQl 2008. Could you please help me to resolve this..

When one transaction holds a lock on a data resource and another transaction requests an incompatible lock on the same resource, the request is blocked and the requester enters a wait state. By default, the blocked request keeps waiting until the blocker releases the interfering lock.
To get lock information, including both locks that are currently granted to sessions and locks that sessions are waiting for, query the dynamic management view (DMV) sys.dm_tran_locks as:
SELECT -- use * to explore other available attributes
request_session_id AS spid,
resource_type AS restype,
resource_database_id AS dbid,
DB_NAME(resource_database_id) AS dbname,
resource_description AS res,
resource_associated_entity_id AS resid,
request_mode AS mode,
request_status AS status
FROM sys.dm_tran_locks;
In the output of the query you will get the spid which is waiting for a shared/exclusive lock.You can get the involved spid's by observing rows with same res and resid values.
Further to get more information you can execute the following code to obtain connection, session, and blocking information about the processes involved in the blocking chain.
-- Connection info:
SELECT -- use * to explore
session_id AS spid,
connect_time,
last_read,
last_write,
most_recent_sql_handle
FROM sys.dm_exec_connections
WHERE session_id IN(--spid found in above query);
-- Session info
SELECT -- use * to explore
session_id AS spid,
login_time,
host_name,
program_name,
login_name,
nt_user_name,
last_request_start_time,
last_request_end_time
FROM sys.dm_exec_sessions
WHERE session_id IN(--spid found in above query);
-- Blocking
SELECT -- use * to explore
session_id AS spid,
blocking_session_id,
command,
sql_handle,
database_id,
wait_type,
wait_time,
wait_resource
FROM sys.dm_exec_requests
WHERE blocking_session_id > 0;
--SQL text of the connections involved in the blocking chain:
SELECT session_id, text
FROM sys.dm_exec_connections
CROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handle) AS ST
WHERE session_id IN(--spid found in above query);
Once you get involved spid's you can either KILL the spid using KILL <spid> command or set lock_timeout in your stored procedure using command: SET LOCK_TIMEOUT <milliseconds>;

Using SQL Server Management Studio
To configure the remote query timeout option
In Object Explorer, right-click a server and select Properties.
Click the Connections node.
Under Remote server connections, in the Remote query timeout box,
type or select a value from 0 through 2,147,483,647 to set the
maximum number seconds for SQL Server to wait before timing out.
See full details here

Timeouts are never in sql server, but always in the client calling them. So, if you can not tune the query (which may be the case) change the timeout in the application you use to issue the query.

Related

Azure SQL: Negative SPID = -5

i have AzureSQL database (Provisioned - 2vCore, General Purpose) and sometimes my queries are blocked by session with SPID = -5.
I didnt find anything about this negative SPID and how to avoid blocking my queries.
Thanks for any info and help
Sessions with negative SPID are probably orphaned transactions. You cannot kill the session using KILL command as it needs a positive SPID number. Try running below query on the Azure SQL Database:
SELECT
DISTINCT(request_owner_guid) as UoW_Guid
FROM sys.dm_tran_locks
WHERE request_session_id =-5
GO
This should return a GUID like with the following format: 00000000-0000-0000-0000-000000000000
Try to kill the session using the GUID instead of the SPID as shown below:
KILL '00000000-0000-0000-0000-000000000000' -- replace GUID value with UoW_Guid value from above query

SQL - Table waiting for Commit/Rollback after closing session

I have a large table without any indexes on it, about 27.5m rows.
I tried to delete about half of those using using BEGIN TRAN.
I then tried canceling the querie, but because it was taking long I decided to just close the Management Studio.
Now when I try to look into that table, it does not return anything, just keeps running.
When I run SELECT TOP 10 * FROM Tbl it just executes without returning anything.
But when I run SELECT TOP 10 * FROM Tbl(NOLOCK) it return 10 rows.
This tells me that it is waiting on a Rollback/Commit from my BEGIN TRAN.
I thought it would rollback automatically after closing the session and Management Studio.
How do I fix this issue?
Thanks.
Execute SP: SP_LOCK
In Results you will get SPID, DBID, OBJID, INDID, TYPE, RESOURCE,
MODE, STATUS
Now check the status column, if it is showing wait then
kill that SPID. To kill a particular SPID Execute SP: Kill 65 (Where
65 is SPID)
MSDN Forum
Apparently, this worked:
Terminate/Close connection to the database would solve this. To close connection you can try terminating sqlserver.exe from Task Manager or Activity Monitor if you have a Mac.

Number of open connections and the meaning of sleeping status of a connection

I ran this query in my database :
SELECT
DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnections,
loginame as LoginName
FROM
sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid, loginame
---------------------------------------------------
SELECT COUNT(dbid) as TotalConnections
FROM sys.sysprocesses
WHERE
dbid > 0
---------------------------------------------------
exec sp_who2 'Active'
I want to know the total number of connections to my database. The sum of first query and amount of second query are equal but the third query returns a different number of rows.
I want to know what the third query returns? I see some of the status' in the result of the third query are sleeping. What does this mean? Is the connection idle, or it is ready in the pool? What does it mean if I have many sleeping connections in my result?
thanks
A status of sleeping means the session is connected but not actively running anything (e.g. the simplest definition, while perhaps not 100% accurate, is that there is nothing for that session_id in sys.dm_exec_requests).
sp_who2 'active' filters out any spid that has a status of sleeping or has a last command of AWAITING COMMAND, LAZY WRITER or CHECKPOINT SLEEP. No I did not memorize what sp_who2 does; I simply looked at the source code:
EXEC master..sp_helptext sp_who2;
Note that sp_who2 is undocumented and unsupported, and sysprocesses is deprecated and currently present only for backward compatibility reasons. You will be better served, I think, with a procedure like Adam Machanic's sp_whoisactive. Or at the very least knowing how to use the more modern DMVs like sys.dm_exec_sessions and sys.dm_exec_requests.
EDIT
I should add the unfortunate disclaimer that the more modern DMVs still don't properly reveal the database(s) involved. But what does it really mean anyway? If you have a query like this:
USE database_A;
GO
SELECT [column] FROM database_B.dbo.[table]
UNION ALL
SELECT [column] FROM database_C.dbo.[table];
What database id do you expect will get reflected in sys.sysprocesses.dbid for that session while this query is running? I'll leave it as an exercise to the reader to determine what actually happens. Long story short, that's not the view/column you want to rely on to know which databases are currently being "touched."

Creation time of a lock in SQL Server 2008

So I'm trying to monitor the longest living lock in my database. The idea is that if a lock has been held for a certain amount of time, I will receive a warning in my application.
But for the life of I can't find the creation time of the locks.
I have used:
exec msdb..sp_lock
exec msdb..sp_who2
SELECT * FROM sys.dm_tran_locks
select * from sys.syslockinfo
select cmd, * from sys.sysprocesses where blocked > 0
But none of these seem to have the information I need.
Any ideas?
G
A long running transaction makes more sense which you can do by looking at database_transaction_begin_time column in sys.dm_tran_database_transactions
I've never known anyone to try and monitor locks... which is possibly why there is no start date/time information for them...
The Lock:Acquired EventClass in SQL Profiler has StartTime and EndTime. You might want to check that

SQl statement to monitor SQL Server Locks

Which SQL sentence (function or stored procedure) I can use to monitor an SQL Server DeadLock, caused by an UPDATE statement?
try this query to see blocking processes, including the actual SQL query text:
SELECT
r.session_id AS spid
,r.cpu_time,r.reads,r.writes,r.logical_reads
,r.blocking_session_id AS BlockingSPID
,LEFT(OBJECT_NAME(st.objectid, st.dbid),50) AS ShortObjectName
,LEFT(DB_NAME(r.database_id),50) AS DatabaseName
,s.program_name
,s.login_name
,OBJECT_NAME(st.objectid, st.dbid) AS ObjectName
,SUBSTRING(st.text, (r.statement_start_offset/2)+1,( (CASE r.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE r.statement_end_offset
END - r.statement_start_offset
)/2
) + 1
) AS SQLText
FROM sys.dm_exec_requests r
JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text (sql_handle) st
--WHERE r.session_id!=##SPID --uncomment to not see this query
deadlocks are easily trapped with the profiler:
Try running the run a trace in the profiler (pick the blank template), select the "deadlock graph event", and on the new tab that appears (Events Extraction Settings), save each (check "save deadlock XML events separately") in its own file. Open this file in an xml viewer and it will be easy to tell what is happening. Each process is contained, with an execution stack of procedure/trigger/etc calls, etc. and all locks are in there too.
Look at the "resource list" section of the file, it will show what is being locked and held by each process causing the deadlock. Figure out how to not have one of these locked and the deadlock will be resolved.
Let this trace run until the deadlock happens again, info is only recorded when a deadlock happens, so there isn't much overhead. If it never happens again, good it is solved, if not you have captured all the info.
I think you're better placed to use SQL Profiler to monitor deadlocks - see here. SQL Profiler shows the deadlocks in a diagram to make it easier to determine what caused them.
SELECT * FROM sys.dm_exec_requests WHERE blocking_session_id <> 0
But that will show you requests that are blocking, which is normal for SQL Server and doesn't indicate a deadlock. If SQL Server detects a deadlock it will kill one of the processes to allow the others to continue. So you can't use a function or stored procedure to monitor this as by the time it happens it has already finished.
You can either use the SQL Profiler or Trace Flags