thoughts on programming and computer related stuff RSS 2.0
# Monday, December 08, 2008
Last week I had to make a SQL Procedure go faster and a new join in the same operation. From the execution plan I could see that the thing that cost most time was a call to an external function made as a .NET CLR that got all the groups a person was member of, and returned it as a comma separated string. This tok a couple of milliseconds, each time, and with some of the users input resulting in over 8000 calls in a single statement, the procedure took to long time. I also had the requirement to add an other column in the result, showing a comma separated list of functions each persons had. Doing this the same way as before, would turn the procedure almost twice as sluggish as before.

To get the result wanted, a small trick was used. Here is a simplified version of what I wrote:
SELECT TOP 10
        substring(O.Functions,2,500)
FROM    dbo.Tp_Person P
CROSS APPLY ( SELECT    ',' + CAST(FunctionName AS varchar) AS [text()]
              FROM      dbo.Tp_Function F
              WHERE     F.Personid = P.Personid
              FOR XML PATH('')
             ) O( Functions )


This trick actually reduced the time of the procedure on 5000 people from about 30 seconds to 1,5 seconds. Made my day.

Monday, December 08, 2008 6:55:56 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
T-SQL
Comments are closed.
Navigation
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Gaute Magnussen
Sign In
Statistics
Total Posts: 17
This Year: 0
This Month: 0
This Week: 0
Comments: 1
Themes
Pick a theme:
All Content © 2010, Gaute Magnussen
DasBlog theme 'Business' created by Christoph De Baene (delarou)