views:

58

answers:

4

Using SQL Server 2005. I was doing some simple queries on a table that has about 200k records. As of today when I got to work, a simple SELECT * FROM executes till it retrieves about 20k rows...then stops. It won't go past 20k rows. If I try to select just ONE row while using ORDER BY Created DESC, the query runs indefinitely. I've never encountered this before. All other tables are acting normally. Is it possible for my table to have become corrupted? This literally happened overnight. The table does take live data, but has been doing so (via a form) for several months without issue. Is it possibly some errant record that's breaking the query? If so, how could I even find it...since I can't get a result set back anymore?

I apologize if this is vague, but I'm not sure how else to word it.

+1  A: 

Has SET ROWCOUNT been applied to your session?

DaveE
That wouldn't explain why `select TOP 1 ... ORDER BY Created DESC` wouldn't work.
Martin Smith
The fact that a more expert user can discount this possibility does not make the answer unhelpful. +1 to compensate for the downvote.
Andomar
Indeed, ROWCOUNT seems an intelligent suggestion to me
smirkingman
+1  A: 

Does a SELECT COUNT(*) return anything? Is it accurate (e.g. roughly 200k)? How long does it take?

You should try doing a full backup of the database, then restore to a new database name. After that, see if you are having the same issue with the restored database.

Jonathan
First suspect for this would surely be an `X` lock on one of the rows by an uncommitted transaction. I wouldn't mess about restoring backups at this point.
Martin Smith
@aludiab My suggestion did not help with the solution? No problem - just let me know and I'll remove this answer.
Jonathan
+1 Why is this voted down? Helpful and useful answer; exactly what I would do
Andomar
@Andomar - Looking at the up/down vote stats I don't think it actually was. There is an issue with retracted downvotes where the display doesn't update.
Martin Smith
A: 

DBCC CHECKTABLE can be used to check the integrity of a single table and its indexes.

'SET ROWCOUNT' seems the most likely culprit, but presumably you are not deliberately setting it. This value/setting might be being set in the background for you somehow; it can be set within SSMS to apply to all windows (Tools / Options / Query Execution), or even to just the current Window (Query / Query Options / Execution).

Other (and, frankly, ridiculous) possibilities exist. Tracking your session (from Login to submitted query) via SQL Profiler may reveal more and subtler information.

Philip Kelley
Locking/blocking is an option. I've little experience with this on BIG tables. What indexes do you have? Might they be being updated by other connections while your query is running?
Philip Kelley
A: 

I would assume that an uncommitted transaction is holding a lock on at least one of the rows or pages that is incompatible with the shared locks required by your SELECT query.

To troubleshoot this you can in one SSMS window do your SELECT ... query that fails.

Then in a second window while the first query is still executing and blocked run the following script.

This should show you the offending SQL along with sufficient details to troubleshoot it.

SELECT  
 Blocking.session_id as BlockingSessionId 
, Sess.login_name AS BlockingUser  
, BlockingSQL.text AS BlockingSQL 
, Waits.wait_type WhyBlocked 
, Blocked.session_id AS BlockedSessionId 
, USER_NAME(Blocked.user_id) AS BlockedUser 
, BlockedSQL.text AS BlockedSQL 
, DB_NAME(Blocked.database_id) AS DatabaseName 
FROM sys.dm_exec_connections AS Blocking 
INNER JOIN sys.dm_exec_requests AS Blocked  
ON Blocking.session_id = Blocked.blocking_session_id 
INNER JOIN sys.dm_os_waiting_tasks AS Waits  
ON Blocked.session_id = Waits.session_id 
RIGHT OUTER JOIN sys.dm_exec_sessions Sess  
ON Blocking.session_id = Sess.session_id   
CROSS APPLY sys.dm_exec_sql_text(Blocking.most_recent_sql_handle) AS BlockingSQL 
CROSS APPLY sys.dm_exec_sql_text(Blocked.sql_handle) AS BlockedSQL 
ORDER BY BlockingSessionId, BlockedSessionId
Martin Smith
Ask him for the time and he'll build you a chronograph
smirkingman