Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ssfier
Starting Member
2 Posts |
Posted - 2012-01-30 : 09:13:33
|
--I have a table that's being inserted every night and then queried as a reporting table.--the store proc that works for the query, has dynamic sql string, paging and two temporary tables within it.--it works well for the first week--starting from the second week, its performance starts to drop sharply--I have acquired and then run the output string of the dynamic sql, it's dramatically faster, so I guess it could be related to compiler--then I did some optimization, like changing count(*) to count(event_id), the performance is immediately back, but the next morning the performance is down again.--then I changed 'select into' to declaring the temp table explicitly, the performance is immediately back, but the next morning the performance is down again.--then I changed declaring the temp table explicitly back to 'select into', the performance is immediately back, but the next morning the performance is down again.--so I guess it has nothing to do with code optimization, it seems every time the SP got compiled, the performance can be better for only less than 24 hours--I am thinking about the nightly insertion, which is also 24 hours' cycle, then I found this no (nolock) thing, which could have locked up Table1--After adding Nolock, the store procedure ran well for a week, after which we got the same problem again, except that this time, only the web page calling the SP is slow, running SP from DB is fast…--If you have time, please give me some clue here.--Here is the dynamic sql store proc:CREATE PROCEDURE [dbo].[fs_1_usp_query]@paramerter_client_id int = null, @paramerter_event_type_id int = null, @paramerter_start_date datetime = null, @paramerter_end_date datetime = null, @paramerter_page_index int = 1,paramerter_sort_direction varchar(20), @paramerter_page_count int = 30 AS BEGIN SET ARITHABORT ON; SET NOCOUNT ON; declare @sql nvarchar(max) set @sql = ' create table #output2 ( page_index int, rownumber int, page_count int, client_id int, date datetime, ) --insert into #output1 select page_count = count(event_id) over(), table1.* into #output1' set @sql = @sql + ' from table1 table1 with (nolock) inner join table2 table2 with (nolock) on ............................ inner join table3 table3 with (nolock) on ............................ inner join table4 table4 on ............................ where ............................ if (@paramerter_client_id is not null) set @sql = @sql + ' and table2.client_id = @paramerter_client_id' if (@paramerter_event_type_id is not null) set @sql = @sql + ' and table2.event_type_id = @paramerter_event_type_id' if (@paramerter_start_date is not null) set @sql = @sql + ' and table2.created_date >= @paramerter_start_date' if (@paramerter_end_date is not null) set @sql = @sql + ' and table2.created_date <= @paramerter_end_date' declare @lv_begin_index int declare @lv_end_index int set @lv_begin_index = ((@paramerter_page_index - 1) * @paramerter_page_count) + 1 set @lv_end_index = @lv_begin_index + @paramerter_page_count set @sql = @sql + ' UPDATE #output1 SET osat_rating = ''-'' WHERE LEFT( osat_rating , 1 ) = ''-'' insert into #output2 select page_index = ' + convert(varchar, @paramerter_page_index) + ', row_number() over (order by [' + @paramerter_sort_expression + '] '+ @paramerter_sort_direction + ') as rownumber, #output1.* from #output1 select #output2.* from #output2 where rownumber >= ' + convert(varchar, @lv_begin_index) + ' and rownumber < ' + convert(varchar, @lv_end_index) ' set @sql = @sql + ' drop table #output1 drop table #output2 ' |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
ssfier
Starting Member
2 Posts |
Posted - 2012-02-14 : 09:43:26
|
Thanks for the tips, :=)Yes, regarding Plan Caching, I am taking off dynamic strings and doing static SQL, hopefully within the next two weeks I will get back here with an update whether this will work out:Where Column3 = Coalesce(@parameter3, Column3) and (@parameter_start_date is null or Column_created_date >= @parameter_start_date) and (@parameter_1 is null or (@parameter_1 not in (‘ConstantString1’, 'ConstantString2’) and Column1 = @parameter_1) or (@parameter_1 = ‘ConstantString1’ and Column1 like 'ConstantString1%') or (@parameter_1 = ‘ConstantString2’ and (Column1 is null or Column1 = '')) ) If(@parameter_sort_direction = 'DESC') Begin insert into #temp_table_result select page_index = convert(varchar, @parameter_page_index), row_number() over ( order by CASE WHEN @parameter_sort_expression = 'Column1' THEN Column1 WHEN @parameter_sort_expression = 'Column2' THEN Column2 WHEN @parameter_sort_expression = 'Column3' THEN Column3 WHEN @parameter_sort_expression = 'Column4' THEN Column4 WHEN @parameter_sort_expression = 'Column5' THEN Column5 WHEN @parameter_sort_expression = 'Column6' THEN Column6 WHEN @parameter_sort_expression = 'Column7' THEN Column7 WHEN @parameter_sort_expression = 'Column8' THEN Column8 END desc--CASE -- WHEN @parameter_sort_direction = 'ASC' THEN asc -- WHEN @parameter_sort_expression = 'DESC' THEN desc --END ) as rownumber, #temp_table_staging.* from #temp_table_staging END ELSE Begin insert into #temp_table_result select page_index = convert(varchar, @parameter_page_index), row_number() over ( order by CASE WHEN @parameter_sort_expression = 'Column1' THEN Column1 WHEN @parameter_sort_expression = 'Column2' THEN Column2 WHEN @parameter_sort_expression = 'Column3' THEN Column3 WHEN @parameter_sort_expression = 'Column4' THEN Column4 WHEN @parameter_sort_expression = 'Column5' THEN Column5 WHEN @parameter_sort_expression = 'Column6' THEN Column6 WHEN @parameter_sort_expression = 'Column7' THEN Column7 WHEN @parameter_sort_expression = 'Column8' THEN Column8 END asc ) as rownumber, #temp_table_staging.* from #temp_table_staging END |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 09:58:19
|
the set of or condition in where clause can also cause bad query plans getting created and cause query to perform poorly especially for large datasets. In such cases, using dynamic sql with sp_executesql may perform better------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|