Hello...this is a nightmare, please help!
I have a stored procedure that is a combination of 15 separate queries which each return a result in the form of 'description', count(some_field), and I am using UNION ALL to combine all of these. The result is something that might look like this
description count
---------------------
value1 50
value2 100
value3 75
and so on. the reason I am doing it this way is so that I can return a bunch of metrics for use in excel dashboards.
here is an example of the stored procedure:
BEGIN
EXEC ('
/**OVERALL VISITS**/
select count(distinct(cs.sessionID)), ''OVERALL: visits'' as what from ['+@logging+'].dbo.customlogs as cl WITH (NOLOCK) , ['+@logging+'].dbo.customlogssubmissions as cs WITH (NOLOCK) where cl.submissionid = cs.submissionid and cl.customlogtypeid=15 and cs.submissiondatetime between '''+@DateMin+''' and '''+@DateMax+'''
union all
/**OVERALL VISITORS**/
select count(distinct(cs.userID)), ''OVERALL: visitors'' as what from ['+@logging+'].dbo.customlogs as cl WITH (NOLOCK) , ['+@logging+'].dbo.customlogssubmissions as cs WITH (NOLOCK) where cl.submissionid = cs.submissionid and cl.customlogtypeid=15 and cs.submissiondatetime between '''+@DateMin+''' and '''+@DateMax+'''
This goes on with many more queries some of which are pulling data from different databases, as well (JOINs).
The queries have absolutely no queries other than SELECT so I am unsure why there are so many extent locks in the database?
Anyways, these queries have started becoming troublesome as the database grows in size. It used to take 40 minutes but now can run for over 15 hours and return nothing. Other times it stops due to the tempdb filling up the hard drive, intra-query parallelism, or other tempdb related errors.
I'm unsure how to fix this and would appreciate a short-term fix as well as any suggestions as to how I could better produce the results in this format.
Thanks in advance.
I have a stored procedure that is a combination of 15 separate queries which each return a result in the form of 'description', count(some_field), and I am using UNION ALL to combine all of these. The result is something that might look like this
description count
---------------------
value1 50
value2 100
value3 75
and so on. the reason I am doing it this way is so that I can return a bunch of metrics for use in excel dashboards.
here is an example of the stored procedure:
BEGIN
EXEC ('
/**OVERALL VISITS**/
select count(distinct(cs.sessionID)), ''OVERALL: visits'' as what from ['+@logging+'].dbo.customlogs as cl WITH (NOLOCK) , ['+@logging+'].dbo.customlogssubmissions as cs WITH (NOLOCK) where cl.submissionid = cs.submissionid and cl.customlogtypeid=15 and cs.submissiondatetime between '''+@DateMin+''' and '''+@DateMax+'''
union all
/**OVERALL VISITORS**/
select count(distinct(cs.userID)), ''OVERALL: visitors'' as what from ['+@logging+'].dbo.customlogs as cl WITH (NOLOCK) , ['+@logging+'].dbo.customlogssubmissions as cs WITH (NOLOCK) where cl.submissionid = cs.submissionid and cl.customlogtypeid=15 and cs.submissiondatetime between '''+@DateMin+''' and '''+@DateMax+'''
This goes on with many more queries some of which are pulling data from different databases, as well (JOINs).
The queries have absolutely no queries other than SELECT so I am unsure why there are so many extent locks in the database?
Anyways, these queries have started becoming troublesome as the database grows in size. It used to take 40 minutes but now can run for over 15 hours and return nothing. Other times it stops due to the tempdb filling up the hard drive, intra-query parallelism, or other tempdb related errors.
I'm unsure how to fix this and would appreciate a short-term fix as well as any suggestions as to how I could better produce the results in this format.
Thanks in advance.