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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to re-write a query

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-07-17 : 09:13:14
Hi All

My Below query is stuck and not displaying result.Performance is
very poor..


Select

Isnull(Max(fi.FileInstanceID), 0),
Isnull(Max(di.DocumentInstanceID), 0),
Isnull(Max(d.RecordID), 0)
From
dbo.DSFile fi with (nolock),
dbo.DSDoc di with (nolock),
dbo.DS d with (nolock)



I want to rewrite in a manner so that it can produce same result and
performance can be improved .

Vijay is here to learn something from you guys.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-07-17 : 09:24:17
This?
Select 

(select Isnull(Max(fi.FileInstanceID), 0) from dbo.DSFile fi with (nolock)),
(select Isnull(Max(di.DocumentInstanceID), 0) from dbo.DSDoc di with (nolock)),
(select Isnull(Max(d.RecordID), 0) from dbo.DS d with (nolock))






Too old to Rock'n'Roll too young to die.
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-07-17 : 09:31:19
Thanks webfred...

The developer had written this query .. Iwas confused about multiple tables in From clause . I tested your query and my problamatic query
on other tables which has less data.. and both the query has same result .. I dont understand why that developer has used three tables in from clause if he wanted to get the max from each table which your query also giving.. Can you please explain me the difference of this..



Select
@MaxRowCount_FileInstanceID = Isnull(Max(fi.FileInstanceID), 0),
@MaxRowCount_DocumentInstanceID = Isnull(Max(di.DocumentInstanceID), 0),
@MaxRowCount_DetailID = Isnull(Max(d.RecordID), 0)

-- Isnull(Max(fi.FileInstanceID), 0),
-- Isnull(Max(di.DocumentInstanceID), 0),
--Isnull(Max(d.RecordID), 0)
From
dbo.DSFile fi with (nolock),
dbo.DSDoc di with (nolock),
dbo.DS d with (nolock)




Vijay is here to learn something from you guys.
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-07-17 : 15:45:03
This:
From
dbo.DSFile fi with (nolock),
dbo.DSDoc di with (nolock),
dbo.DS d with (nolock)

will create fi * di * d number of rows and then work out the largest of each one (it's called a Cartesian product).
Webfred's solution is to simply get the max of each table, so fi + di + d number of rows to analyse which obviously will be quicker and less resource intensive.

I suspect it's just a mistake or ignorance of SQL. If you don't know SQL it certainly looks like it should do the job doesn't it? And more surprisingly it does! So I think he just got away with it.
Go to Top of Page
   

- Advertisement -