Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL table - Insert and Deletes time out, select OK

I have a database in SQL Server 2008 R2 where all tables seem to be functioning normally except for one table.

In this table, I can't delete a row or insert a row because it goes for over 30 min and times out.

My insert looks like this:

INSERT INTO [dbo].[brokenTable] ([Change], [Date], [ProdId], [IntCol]) 
VALUES ('test', getdate(), null, '99999')

However, I can select. Selecting top 200,000 takes 33 seconds. There are only ~260,000 rows in the table. There are five columns(one int primary key, one date, another int columns, and 2 varchar columns.)

This table used to work fine and be quick, I have made no structure changes at all.

Does anyone have any ideas why this might have happened, and how to fix it?

like image 722
Kyle Avatar asked Dec 26 '22 23:12

Kyle


1 Answers

To people of the future who may have the same problem.

I thought the query was going very slow, this was not the case. It was being locked by another session. If you follow Martin Smith's comments, he helped me find which one to fix it.

First, I ran

select * from sys.dm_os_waiting_tasks

while running a query that would not finish, and again while not running it, to find a LCK_M_IX. I used the Session Id from that row, and matched it with the row from

select * from sys.dm_exec_sessions

and found the offending session that was blocking my query!

Since I knew this was a safe session to end, i ended it with

Kill [sessionId]
like image 52
Kyle Avatar answered Jan 15 '23 17:01

Kyle