Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2009-11-15 : 04:16:56
|
| I am experiencing some trouble in my sql query, the following query hangs other users those who accessing the same sql server until my code close the query. Can anyone please explain why this happening and how can we get rid of that?3 tables involved Table_Rows, Table_Client, mStaffSelect *, (select staffname from mstaff where staffid=mtid) as mtname, (select staffname from mstaff where staffid=fcid) as fcname, (select staffname from mstaff where staffid=prid) as prname, (select staffname from staff where staffid=qcid) as qcname,c.divby,c.chartype from Table_Rows j, Table_Client c where j.clientid=c.clientid and j.fmflag ='P' and j.fmid='" & UserId & "' and j.clientid='1'BUT this NOT hangsSelect *, (select staffname from staff where staffid=mtid) as mtname, (select staffname from staff where staffid=fcid) as fcname, (select staffname from staff where staffid=prid) as prname, (select staffname from staff where staffid=qcid) as qcname from Table_Rows j where j.fmflag ='P' and j.fmid='" & userId & "' and j.clientid='1'____________Praba |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-11-17 : 07:38:55
|
Hi.You are performing a lot of sub queries there. That's really bad for performance. Also try and avoid using SELECT * for anything in production.Is there a reason you don't use joins -- aside from the fact that you couldn't then do a select *Does this work for you?/*Select * , (select staffname from mstaff where staffid=mtid) as mtname , (select staffname from mstaff where staffid=fcid) as fcname , (select staffname from mstaff where staffid=prid) as prname , (select staffname from staff where staffid=qcid) as qcname ,c.divby ,c.chartypefrom Table_Rows j , Table_Client cwhere j.clientid=c.clientid and j.fmflag ='P' and j.fmid='" & UserId & "' and j.clientid='1'*/SELECT -- DON'T USE * specify the columns you want back explicitly. mt.[staffname] AS [mtname] , fc.[staffname] AS [fcname] , pr.[staffname] as [prname] , s.[staffname] AS [qcname] , c.[divby] , c.[chartType]FROM Table_Rows j JOIN Table_Client c ON c.[clientID] = j.[clientID] LEFT JOIN mstaff mt ON mt.[staffId] = j.[mtid] -- This is a guess (I have no idea where mtid is) LEFT JOIN mstaff fc ON fc.[staffId] = j.[fcid] -- This is a guess (I have no idea where fcid is) LEFT JOIN mstaff pr ON pr.[staffId] = j.[prid] -- This is a guess (I have no idea where prid is) LEFT JOIN staff s ON s.[staffId] = j.[qcid] -- This is a guess (I have no idea where qcid is)WHERE j.[fmflag] = 'P' AND j.[fmid] = '" & UserId & "' AND j.[clientID] = '1' Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2009-11-17 : 08:23:50
|
| hi charlie, thank you for your reply. your guess right on everything.____________Praba |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2009-11-17 : 08:25:19
|
| can you pls clarify why the second query not hangs____________Praba |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-11-18 : 05:39:28
|
| I'm guessing here but:I think if you were to have a look at the execution plans you'll probably find that the query plans are different. I suspect that the query plan for the first query might be doing a implicit cross join and then applying the "where j.clientid=c.clientid" portion, depending on the size of the two tables that can take a long time. Even then you are issuing a sub select for each row in the resultant table.If you post the table structure of the 4 tables involved and some sample data we'd be able to give you nice optimised query that will eliminate the locking.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|
|