Register
Sunday, February 05, 2012
 
 DBAs And ProgrammersBlog
  
News! Minimize
   
 
 Print   
 
Misc Blog Stuff Minimize
   
 
 Print   
 
The Reluctant DBA Minimize
 
 
 
 Print   
 
Reluctant DBA Minimize
   
 
  
 
Reluctant DBA Minimize
   
 
  
 
The Reluctant DBA Minimize
 
Apr24

Written by:CarpDeus
4/24/2009 6:37 AM 

I recently modified a stored procedure at work and ended up removing a sort order and was reminded that Explicit Sort Order Matters.

The short version of the procedure is that it fills a temporary table with data, does some manipulation of that data and then returns a subset of the data. The problem was, the data contains an NTEXT column which contains XML data. Before you ask, the system was architected before SQL 2005 and the XML choice wasn't available. After the database was moved to a 2005 server, there was no need to change the data to XML because the database doesn't actually care about the data, it handles information used by the client.

NTEXT columns can be expensive, even in a temporary table, and especially when you've got several thousand rows to be slinging about and the average data length in the NTEXT is a tad under 1K. So, to optimize performance, I removed the NTEXT from the temporary table and added it in using a JOIN at the end of the procedure.

select *, Row_Number() OVER(ORDER BY DateUpdated Desc) RowNumber FROM #HoldingTable) A
inner join content c on a.indexguid = c.indexguid
where RowNumber between @LowRank  and @HighRank

Before I added the JOIN, everything worked. After I added the JOIN, it mostly worked, but there were some strange items that weren't sorted correctly. That's because the JOIN introduced some optimization issues that threw off the sort order. So I added ORDER BY RowNumber and everything works.

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment  Cancel 
 
 
  
 
Privacy Statement | Terms Of Use Copyright 2001-2008 by ReluctantDBA.com