I have SQL Server 2008. I run a query in a table on a database. The weirdest thing keeps happening. I run a simple select statement on the table. I know there are 62 rows in the table but it gets stuck at row 48 and goes on "querying...". Waited already for hours and it didn't move on from there. I only know of two programs, and one reporting service connecting to that particular table and one other user. Does anyone have any idea on what could be causing this and how I could trace the source of the lock on that table?
As a side note, I noted that the logs only had a notice that Autogrow failed the day before I checked. Could this have something to do with it?
What if you do a
SELECT * FROM YourTable WITH(NOLOCK)
Does it still hang?
Additionally when it does appear to be blocked you can try running
exec sp_who2
And looking in the BlkBy column to see what process is blocking you.
If that doesn't shed any light this article gives some info on some DMVs that might help get some insight into reasons for waits.
Related
I have Powershell script which runs the below SQL query. This script is scheduled to run every 5 minutes. Could you please tell me whether querying database every 5 minutes will cause database deadlock. Could someone please throw light on how deadlock works in SQL.
SELECT TOP 1000 * FROM TABLE1
I am not concerned about the data which I get from select query. I am pulling this data to check the response time. Does the parameter WITH(NOLOCK) prevent deadlock?
This script alone will never deadlock your table because it is just a SELECT.
Check your SQL Activity Monitor in SSMS to if any other processes are updating the table, because a deadlock happens when you select and update the record at the same time.
I ran into an issue where no queries could be executed at all against a single table in our database. The table was in a complete deadlock. I checked sp_whoisactive for answers and found the following.
A simple SELECT on this table with session id 172, was waiting for a DELETE query on this table with session id 478, which was waiting for an INDEX REORGANIZE on this table with session id 207, which was waiting for a simple SELECT on this table with session id 598.
After killing session 598 everything completed immediately. Afterwards, executing that same query that was keeping the table in a deadlock in a separate window in SSMS only took 2 seconds. I asked around and this query is executed by an Excel file. Apparently we have lots of Excel files floating around which run queries against our SQL database. Obviously this is extremely bad practice as the connection string can be found inside, but as with everything legacy that's just the way it is now until it is fixed.
When googling I'm finding a lot of resources about Excel actually locking tables. Now as far as my limited understanding of locks goes if the query executed by session id 598 would actually lock the table it should only do so for the duration of the query. And since running the query separately only took a few seconds I don't understand how it was running for over 12 hours. If I can trust the results of sp_whoisactive it wasn't waiting on anything else. So why didn't it complete?
Before I suggest something like adding WITH(NOLOCK) to every query in Excel files, which is just a patch and not a solution I'd like to find out why this happened so that we can avoid this in the future. What causes a deadlock like this and how can it be avoided?
Excel will do a page lock for the duration of the query and until the query is released, which is basically when the spreadsheet is closed. So, if the spreadsheet was left open by a user overnight or for an entire day, then the table would be locked. We have the same issue and we are trying to get the offending spreadsheets replaced with SSRS reports but not having much luck with the take up from staff because the Excel files do a lot of other things that SSRS doesn't.
I'm using SQL Server 2012 and I'm stuck with a strange problem.
I tried to restore a database snapshot to a database. Usually it doesn't take much time to restore, but now it took 5 minutes and is still restoring, so I stopped query execution. It was trying to stop the query execution for more that 5 minutes, so i closed SSMS using task manager.
Then I tried to kill the restore process using KILL.
Now I am able to can connect to that server, but the list of databases is not opening. I mean: whoever is connected to this server, they are not able to get the databases. When I checked the sysprocesses table, it is showing lastwaittype as LCK_M_S.
None of my users can see databases. Looks like I kind of messed up. I cannot restart SQL Server as others are connected to the server.
How do I solve this? Please help.
EDIT:
i tried this approach and when i checked with
select db_name(dbid), * from sysprocesses where blocked <> 0
i got two records,
do you think because of these two rest of the process are getting locked up.
I'm guessing there still is some hidden lock on the sysdatabases table in the master database. This could very well be caused by the KILL of the restore command.
The article here might help you:
http://ellisweb.net/2012/02/clearing-out-a-mysterious-table-lock-lck_m_s-in-sql-server-2008/
It basically advises you to identify where the hidden lock is coming from, and then issuing a KILL on that process ID.
Try restarting the instance. Can't hurt if your users can't see any of the databases anyway.
I have a scheduled job with a SP running on daily basis (SQL Server 2005). Recently I frequently encounter deadlock problem for this SP. Here is the error message:
Message
Executed as user: dbo. Transaction (Process ID 56) was deadlocked on thread |
communication buffer resources with another process and has been chosen as the deadlock
victim. Rerun the transaction. [SQLSTATE 40001] (Error 1205). The step failed.
The SP uses some inter joined views to some tables, one of them is a large size data table with several million rows of data(and keep growing). I am not sure if any job or query against to the table will cause the SP un-accessible to the table? I am going to investigate who is on line by using the query. That may expose some query or person on SQL server during that time.
Not sure if any one have similar issue or this is known SQL 2005 issue? Any additional way I should do in my SP or on SQL server to avoid the deadlock?
Use the SQL Server Profiler to track all the queries that are running. I put the output into SQL Server. This will help you figure out which ones are accessing your particular table / tables. Post your findings, and we can help you with that.
Deadlocks are when two transactions are each holding onto some resources and want a resource that the other one has as well - neither can proceed as they are both waiting for each other. They cannot be completely eliminated, but a lot can be done to mitigate them. Remus and Raj suggest capturing more information about them in Profiler - which I also recommend - generally optimizing your queries (if you know which ones are involved) can also help. Here is an MSDN article that can help get you going: "Minimizing Deadlocks".
Every morning we have a process that issues numerous queries (~10000) to DB2 on an AS400/iSeries/i6 (whatever IBM calls it nowadays), in the last 2 months, the operators have been complaining that our query locks a couple of files preventing them from completing their nightly processing. The queries are very simplisitic, e.g
Select [FieldName] from OpenQuery('<LinkedServerName>', 'Select [FieldName] from [LibraryName].[FieldName] where [SomeField]=[SomeParameter]')
I am not an expert on the iSeries side of the house and was wondering if anyone had any insight on lock escalation from an AS400/Db2 perspective. The ID that is causing the lock has been confirmed to be the ID we registered our linked server as and we know its most likely us because the [Library] and [FileName] are consistent with the query we are issuing.
This has just started happening recently. Is it possible that our select statements which are causing the AS400 to escalate locks? The problem is they are not being released without manual intervention.
Try adding "FOR READ ONLY" to the query then it won't lock records as you retrieve them.
Writes to the files on the AS/400 side from an RPG/COBOL/JPL job program will cause a file lock (by default I think). The job will be unable to get this lock when you are reading. The solution we used was ... don't read the files when jobs are running. We created a big schedule sheet in excel and put all the sql servers' and as/400's jobs on it in times slots w/ color coding for importance and server. That way no conflicts or out of date extract files either.
You might have Commitment Control causing a lock for a Repeatable Read. Check the SQL Server ODBC connection associated with <linkedServerName> to change the commitment control.