SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to re-write a query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vijays3
Constraint Violating Yak Guru

India
338 Posts

Posted - 07/17/2013 :  09:13:14  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 07/17/2013 :  09:24:17  Show Profile  Visit webfred's Homepage  Reply with Quote
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

India
338 Posts

Posted - 07/17/2013 :  09:31:19  Show Profile  Reply with Quote
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

939 Posts

Posted - 07/17/2013 :  15:45:03  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000