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
 Better Way to Write This?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLNOVICE999
Yak Posting Veteran

57 Posts

Posted - 02/22/2013 :  13:42:58  Show Profile  Reply with Quote
Hi Guys,

I have this query that does the job, but it is too slow.. I have the correct indexes...

Is there a way to make this query little faster:

SELECT b.STUDENT_ID,
      COURSES = 
              STUFF ( ( SELECT ','+ rtrim(a.COURSE)
						FROM STUDENT_COURSE a
						WHERE a.STUDENT_ID = b.STUDENT_ID
						FOR XML PATH(''),TYPE 
					   ).value('.','VARCHAR(MAX)') 
					  , 1,1,SPACE(0))
FROM STUDENT_COURSE b
GROUP BY b.STUDENT_ID



It pulls a comma separated list of courses for each student_id.

Please suggest.

Thanks,
Laura

Edited by - SQLNOVICE999 on 02/22/2013 14:20:18

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 02/22/2013 :  14:41:48  Show Profile  Reply with Quote
Can you see if this is any faster?
SELECT
	b.STUDENT_ID,
	STUFF ( ( SELECT ','+ rtrim(a.COURSE)
			FROM STUDENT_COURSE a
			WHERE a.STUDENT_ID = b.STUDENT_ID
			FOR XML PATH(''),TYPE 
		   ).value('.','VARCHAR(MAX)') 
		  , 1,1,SPACE(0))
FROM 
	(SELECT DISTINCT STUDENT_ID FROM STUDENT_COURSE) AS b
Go to Top of Page

robvolk
Most Valuable Yak

USA
15678 Posts

Posted - 02/22/2013 :  15:15:47  Show Profile  Visit robvolk's Homepage  Reply with Quote
Is the STUDENT_ID column indexed in STUDENT_COURSE? What does the execution plan show?

My guess is this index may speed it up:
CREATE INDEX ix_student_id_inc_course ON STUDENT_COURSE(STUDENT_ID) INCLUDE(COURSE)
Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

57 Posts

Posted - 02/22/2013 :  15:42:39  Show Profile  Reply with Quote
Thanks Rob I added that index. Surprisingly that index slowed down the query further. Probably has to do with something else. I will check.
quote:
Originally posted by robvolk

Is the STUDENT_ID column indexed in STUDENT_COURSE? What does the execution plan show?

My guess is this index may speed it up:
CREATE INDEX ix_student_id_inc_course ON STUDENT_COURSE(STUDENT_ID) INCLUDE(COURSE)


Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 02/22/2013 :  16:10:33  Show Profile  Reply with Quote
Can you run the following query?
select count(*) as a, count(distinct student_id) as b from STUDENT_COURSE
The code I posted should have speeded up the query by approximately by a factor of a/b.

Also, surprising that Rob's suggestion slowed it down. What do the query plans show with and without the index?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 02/22/2013 :  16:14:11  Show Profile  Reply with Quote
Also, can you try this? Logically the same, but not sure if the query plan would be different:
SELECT
	b.STUDENT_ID,
	STUFF(a.CoursesXML.value('.','VARCHAR(256)'),1,1,'') AS Courses
FROM 
	(SELECT DISTINCT STUDENT_ID FROM STUDENT_COURSE) AS b
	CROSS APPLY
	(
		SELECT ',' + RTRIM(a.COURSE)
		FROM STUDENT_COURSE a
		WHERE a.STUDENT_ID = b.STUDENT_ID
		FOR XML PATH(''),TYPE
	) AS a(CoursesXML);
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.08 seconds. Powered By: Snitz Forums 2000