Question : Simple Query Hangs

SQL Server 2005..
I have a database procedure that has always worked quickly, recently it is hanging.   I narrowed it down to one query with multiple joins, then dissected it to a single table and a simple where clause.

When entered directly into SQL Management Studio, this query hangs...

 select * from myTable where Fkey = 123

Yet this query returns immediately..
 
  select * from myTable

The column "Fkey" is a foreign key to a parent table.    There is a foreign key constraint defined on it.

The database is in use, after a reboot, I can fetch from the table OK for a few minutes, then it just hangs.     My last run (which I let go on and on) took 11 minutes to complete.   There are only 100 records in this table !


After 11 minutes, this message was returned..

[Macromedia][SQLServer JDBC Driver][SQLServer]Transaction (Process ID 60) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

So if its a deadlock and all I'm doing is a select.. how do I figure out why its deadlocking and how do I keep it from happening?  

Thanks!

Answer : Simple Query Hangs

mmmmm found this

Listing 1: Procedure That Finds the Head of a Chain of Blockers
USE master
SET QUOTED_IDENTIFIER OFF
GO

IF EXISTS (SELECT * FROM sysobjects WHERE name='sp_leadblocker' AND type='P')
   DROP PROC sp_leadblocker
GO

CREATE PROCEDURE sp_leadblocker
AS
IF EXISTS
    (SELECT * FROM master.dbo.sysprocesses
    WHERE spid IN (SELECT blocked FROM master.dbo.sysprocesses))
    SELECT
        spid, status, loginame=substring(loginame, 1, 12),
        hostname=substring(hostname, 1, 12),
            blk=CONVERT(char(3), blocked),
            open_tran,
        dbname=substring(db_name(dbid),1,10),cmd,
            waittype, waittime, last_batch
        FROM master.dbo.sysprocesses
        WHERE spid IN (SELECT blocked FROM master.dbo.sysprocesses)
            AND blocked=0
ELSE
SELECT "No blocking processes found!"
GO
Random Solutions  
 
programming4us programming4us