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 |
|
buzzi
Starting Member
48 Posts |
Posted - 2008-10-24 : 17:35:38
|
| Hello all,I have the below query can some one please tell me whats wrong with this query.declare @ActiveStatusId intSELECT @ActiveStatusId = StatusId FROM [Status] where statusname = 'Active'select uid,uname,email from #R1 S inner join tblusers u on s.RecipientId=u.companyId and s.RecipientTypeId=102 and u.StatusId = @ActiveStatusId inner join tblcompanies C on C.CompanyId = U.CompanyId and C.StatusId = @ActiveStatusId inner join tbllocsusermap l on u.userid=l.userid inner join tblLocs Loc on Loc.LocationId = l.LocationId inner join tblNetworkLocsMap vl on vl.locationId=l.locationId AND vl.StatusId = @ActiveStatusId inner join tblnetworks v on v.networkId =vl.networkId left join tblDepts D on D.DepartmentId = U.DepartmentId where v.StatusId = @ActiveStatusId and ( (v.OriginatorCompanyId=u.companyid and (v.OToR=1 or v.OToO=1 )) or (v.RToR=1 ) or (v.RToO=1 and v.OriginatorCompanyId = SenderCompanyId)) and v.networkId=s.sendernetworkIdIf i run the above query there logical reads in profiler shows me in millions,But i replace the @ActiveStatusId as 1(which is the id for Active Status), the reads comes down to 1000's. I am breaking my head to figure this, anyhelp greatly appreciated, by the way all these tables have few 1000's of records none are huge tablessample data in the temp tableRecipientId RecipientTypeId SenderCompanyId SenderNetworkId 4205 104 4365 3 4388 104 4365 3 Thanks you all |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
buzzi
Starting Member
48 Posts |
Posted - 2008-10-25 : 10:04:29
|
| Thank you Tara,Sorry i didn't get this right. The varible value is set by the statement above the querydeclare @ActiveStatusId intSELECT @ActiveStatusId = StatusId FROM [Status] where statusname = 'Active', so by the time the query is executed the value of the variable is already set to 1.why it will be NULL??, also You have mentioned " You are using the variable in your join conditions"?? shouldn't i use variables in join conditions, Thanks again |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-10-25 : 15:28:49
|
| What I think you have there is a case of parameter sniffing. The optimiser can't see the values of variable and hence has to guess how many rows are affected by the query. This can lead to bad execution plans.[url]http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-2/[/url]Can you try putting OPTION (RECOMPILE) at the end of the query and see if it resolves the issue. If the status value is unlikely to change, you could also try OPTION (OPTIMIZE FOR @ActiveStatusId = 1)Where does this query typically get called from?--Gail ShawSQL Server MVP |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-10-25 : 22:37:04
|
quote: Originally posted by buzzi Thank you Tara,Sorry i didn't get this right. The varible value is set by the statement above the querydeclare @ActiveStatusId intSELECT @ActiveStatusId = StatusId FROM [Status] where statusname = 'Active', so by the time the query is executed the value of the variable is already set to 1.why it will be NULL??, also You have mentioned " You are using the variable in your join conditions"?? shouldn't i use variables in join conditions, Thanks again
Sorry yes I missed that. You didn't have hard returns in between the lines, so I didn't notice the two select statements. You should add code tags around any code posted to retain formatting in a forum so that your code is very clear.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|
|
|
|
|