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 |
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-08-18 : 15:35:35
|
| I'm refining a rather large stored procedure that creates quite a few temp tables and takes forever to run. How much of a benefit speedwise will going to table variables give? |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 16:03:24
|
| There are a lot of factors needed to determine that. If any of them will hold a lot of data it could slow it down more. Post the procedure.Be One with the OptimizerTG |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-18 : 16:27:32
|
Probably none. I think the procedure will perform worse.The reason is that table variables doesn't have statistics, so the query engine assumes there is one and one record only in the table.Even if there are thousands. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 16:51:23
|
| If the procedure should perform quickly - like sub-second - and manipulation of temp table data is cause multiple re-compiles than that could cause a very significant performance hit.I'll bet there are other areas that could be improved - post the procedure.Be One with the OptimizerTG |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-08-18 : 18:15:23
|
| would need to see the code. and know things like the # of records involved (or the # of reads or bytes involved) #temp tables perform far better than too many DBAs think vs table var, if there are many records -- especially when manipulating the data contained within |
 |
|
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-08-20 : 16:35:06
|
| USE [sufi]GO/****** Object: StoredProcedure [dbo].[Texas_Quarterly_Market_Report] Script Date: 08/20/2009 15:34:29 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[Texas_Quarterly_Market_Report]@current_qtr int,@current_year int,@start_rate_table int,@end_rate_table intASDECLARE @start_date datetime,@end_date datetime,@end_date_prev datetime,@filename varchar(12),@current_month char(2),@accounting_date char(4),@begin_period varchar(6),@end_period varchar(6)set nocount on/* Set dates depending on quarter entered */select @start_date = case when @current_qtr = 1 then cast( '01' + '/01/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 2 then cast('04' + '/01/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 3 then cast('07' + '/01/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 4 then cast('10' + '/01/' + CONVERT(char(4),@current_year) as datetime) else '00/00/0000' endselect @end_date = case when @current_qtr = 1 then cast('03' + '/31/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 2 then cast('06' + '/30/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 3 then cast('09' + '/30/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 4 then cast('12' + '/31/' + CONVERT(char(4),@current_year) as datetime) else '00/00/0000' endselect @end_date_prev = case when @current_qtr = 1 then cast('12' + '/31/' + CONVERT(char(4),@current_year-1) as datetime) when @current_qtr = 2 then cast('03' + '/31/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 3 then cast('06' + '/30/' + CONVERT(char(4),@current_year) as datetime) when @current_qtr = 4 then cast('09' + '/30/' + CONVERT(char(4),@current_year) as datetime) else '00/00/0000' endselect @current_month = case when @current_qtr = 1 then '03' when @current_qtr = 2 then '06' when @current_qtr = 3 then '09' when @current_qtr = 4 then '12' else '00' endselect @accounting_date = cast(convert(char(2),right(@current_year,2)) + @current_month as char(4))select @begin_period = case when @current_qtr = 1 then cast(CONVERT(char(4),@current_year) + '01' as varchar(6)) when @current_qtr = 2 then cast(CONVERT(char(4),@current_year) + '04' as varchar(6)) when @current_qtr = 3 then cast(CONVERT(char(4),@current_year) + '07' as varchar(6)) when @current_qtr = 4 then cast(CONVERT(char(4),@current_year) + '10' as varchar(6)) else '000000' endselect @end_period = case when @current_qtr = 1 then cast(CONVERT(char(4),@current_year) + '03' as varchar(6)) when @current_qtr = 2 then cast(CONVERT(char(4),@current_year) + '06' as varchar(6)) when @current_qtr = 3 then cast(CONVERT(char(4),@current_year) + '09' as varchar(6)) when @current_qtr = 4 then cast(CONVERT(char(4),@current_year) + '12' as varchar(6)) else '000000' end/* Create Temp tables needed for proc */CREATE TABLE [dbo].[#add_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [add_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#add_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [add_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#add_vehiclestot] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [add_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#adri_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [add_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#adri_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [add_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#adri_vehiclesa] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [add_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#can_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#can_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#can_vehicles3] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#can_vehicles4] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [nonpay_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insured_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#can_vehiclestot] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [nonpay_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insured_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#canceled] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#candri_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#candri_vehiclesa] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#candri_vehiclesa2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [insurer_vehicles] [int] NULL , [nonpay_vehicles] [int] NULL , [insured_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#cdri_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [current_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#cdri_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [current_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#cdri_vehiclesa] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [current_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#curr_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [current_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#curr_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [current_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#curr_vehiclestot] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [current_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_fees] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [fees] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_fees2] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [fees] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_feesa] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [fees] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_written] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [written] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_written2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [written] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#driver_writtena] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [written] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#fees2] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fees] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#fees_total] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fees] [varchar] (18) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#fees] ( [coverage] [int] NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fees] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#pdri_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [previous_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#pdri_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [previous_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#pdri_vehiclesa] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [previous_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#prev_vehicles] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [previous_vehicles] [int] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#prev_vehicles2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [previous_vehicles] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#prev_vehiclestot] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [previous_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempadd] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempcan] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [cancel_code] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempcurr] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempfees] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written_premium] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempprev] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#tempwritten] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [vehicle_number] [smallint] NULL , [driver_number] [smallint] NULL , [coverage] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written_premium] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#texas_qtr] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written] [varchar] (21) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fees] [varchar] (18) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [previous_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [current_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [add_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insurer_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [nonpay_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [insured_vehicles] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehall] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [vehicle_endorsement] [smallint] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehall2] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [vehicle_endorsement] [smallint] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehinforce] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [vehicle_endorsement] [smallint] NULL , [driver_number] [smallint] NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehinforce2] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NOT NULL , [vehicle_number] [smallint] NOT NULL , [vehicle_endorsement] [smallint] NULL , [driver_number] [smallint] NULL , [driver_class] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehmax] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NULL , [vehicle_number] [smallint] NOT NULL , [max_endorsement] [smallint] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#vehmax2] ( [policy_base] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [policy_suffix] [smallint] NOT NULL , [endorsement] [smallint] NULL , [vehicle_number] [smallint] NOT NULL , [max_endorsement] [smallint] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#written] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written] [money] NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#written2] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#written_total] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [written] [varchar] (21) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#zip_codes] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]CREATE TABLE [dbo].[#zips] ( [coverage] [int] NULL , [rate_zip] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]/* Get initial vehicles and written */insert into #tempwrittenselect a.policy_base, a.policy_suffix, b.vehicle_number, b.driver_number, a.coverage, a.driver_class, a.rate_zip , written_premium = sum(written_premium) from history a WITH (NOLOCK) LEFT OUTER JOIN vehicle b WITH (NOLOCK) ON (a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and (b.current_flag = 'Y' or b.endorse_status = 'D'))where a.policy_state = 'TX' and a.coverage in ('BI','CL') and a.rate_zip is not null and a.accounting_period >= @begin_period and a.accounting_period <= @end_period and a.rate_table >= @start_rate_table and a.rate_table <= @end_rate_tablegroup by a.policy_base, a.policy_suffix, b.vehicle_number, b.driver_number, a.coverage, a.driver_class, a.rate_zip/* Get Written Amount by Coverage and Zip */insert into #writtenselect coverage = case when a.coverage = 'BI' then 1 when a.coverage = 'CL' then 3 end, a.rate_zip, written = sum(a.written_premium) from #tempwritten a WITH (NOLOCK) LEFT OUTER JOIN driver c WITH (NOLOCK) ON (a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.driver_number = c.driver_number and (c.current_flag = 'Y' or c.endorse_status = 'D'))group by a.coverage, a.rate_zip/* Get Written Amount by Coverage and Zip with Driver Class with business use*/insert into #driver_writtenaselect coverage = case when a.coverage = 'BI' then 1 when a.coverage = 'CL' then 3 end, rate_zip = case when a.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when a.Driver_Class in ('100','101','102') then '99250' when a.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when c.Business_Use = 'Y' and a.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, written = sum(a.written_premium) from #tempwritten a WITH (NOLOCK) LEFT OUTER JOIN driver c WITH (NOLOCK) ON (a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.driver_number = c.driver_number and (c.current_flag = 'Y' or c.endorse_status = 'D'))group by a.coverage, a.driver_class, c.business_use/* Combine amounts into one table by driver */insert into #driver_writtenselect * from #driver_writtenainsert into #driver_written2select coverage, rate_zip, sum(written) as written from #driver_written WITH (NOLOCK)group by coverage, rate_zip/* Combine amounts into one table */insert into #written2select coverage, rate_zip, rtrim(ltrim(str(written))) as written from #writteninsert into #written2select coverage, rate_zip, rtrim(ltrim(str(written))) as written from #driver_written2insert into #written_totalselect coverage, rate_zip, written = case when len( written ) = 1 then '00000000000' + written when len( written ) = 2 then '0000000000' + written when len( written ) = 3 then '000000000' + written when len( written ) = 4 then '00000000' + written when len( written ) = 5 then '0000000' + written when len( written ) = 6 then '000000' + written when len( written ) = 7 then '00000' + written when len( written ) = 8 then '0000' + written when len( written ) = 9 then '000' + written when len( written ) = 10 then '00' + written when len( written ) = 11 then '0' + written when written is null then '0000000000' end from #written2/* Get initial vehicles and fees */insert into #tempfeesselect a.policy_base, a.policy_suffix, b.vehicle_number, b.driver_number, a.coverage, a.driver_class, a.rate_zip , written_premium = sum(written_premium) from history a WITH (NOLOCK) LEFT OUTER JOIN vehicle b WITH (NOLOCK) ON (a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and (b.current_flag = 'Y' or b.endorse_status = 'D'))where a.policy_state = 'TX' and a.coverage in ('FE') and a.rate_zip is not null and a.accounting_period >= @begin_period and a.accounting_period <= @end_period and a.rate_table >= @start_rate_table and a.rate_table <= @end_rate_tablegroup by a.policy_base, a.policy_suffix, b.vehicle_number, b.driver_number, a.coverage, a.driver_class, a.rate_zip/* Get Policy and Membership Fees by Coverage and Zip */insert into #feesselect coverage = 1, a.rate_zip, fees = sum(a.written_premium)from #tempfees a WITH (NOLOCK) LEFT OUTER JOIN driver c WITH (NOLOCK) ON (a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.driver_number = c.driver_number and (c.current_flag = 'Y' or c.endorse_status = 'D'))group by a.coverage, a.rate_zip/* Get Policy and Membership Fees by Coverage and Zip with Driver Class with business use*/insert into #driver_feesaselect coverage = 1, rate_zip = case when a.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when a.Driver_Class in ('100','101','102') then '99250' when a.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when c.Business_Use = 'Y' and a.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, fees = sum(a.written_premium)from #tempfees a WITH (NOLOCK) LEFT OUTER JOIN driver c WITH (NOLOCK) ON (a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.driver_number = c.driver_number and (c.current_flag = 'Y' or c.endorse_status = 'D'))group by a.coverage, a.driver_class, c.business_use/* Combine amounts by driver class */insert into #driver_feesselect * from #driver_feesainsert into #driver_fees2select coverage, rate_zip, sum(fees) as fees from #driver_fees WITH (NOLOCK)group by coverage, rate_zip/* Combine all amounts into one table */insert into #fees2select coverage, rate_zip, rtrim(ltrim(str(fees))) as fees from #feesinsert into #fees2select coverage, rate_zip, rtrim(ltrim(str(fees))) as fees from #driver_fees2insert into #fees_totalselect coverage, rate_zip, fees = case when len( fees ) = 1 then '00000000' + fees when len( fees ) = 2 then '0000000' + fees when len( fees ) = 3 then '000000' + fees when len( fees ) = 4 then '00000' + fees when len( fees ) = 5 then '0000' + fees when len( fees ) = 6 then '000' + fees when len( fees ) = 7 then '00' + fees when len( fees ) = 8 then '0' + fees when fees is null then '000000000' end from #fees2/* Get initial Vehicles for Previous Quarter count *//* Get all Vehicles without a status of 'D' */insert into #vehallselect b.policy_base, b.policy_suffix, b.endorsement, b.vehicle_number, max(b.vehicle_endorsement) as vehicle_endorsement from vehicle b WITH (NOLOCK)where b.entered_date <= @end_date_prev and b.endorse_status <> 'D'group by b.policy_base, b.policy_suffix, b.endorsement, b.vehicle_number/* Get max endorsements of initial Vehicles */insert into #vehmaxselect b.policy_base, b.policy_suffix, max(b.endorsement) as endorsement, b.vehicle_number, max(b.vehicle_endorsement) as max_endorsement from #vehall a WITH (NOLOCK), vehicle b WITH (NOLOCK)where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.endorsement = b.endorsement and a.vehicle_number = b.vehicle_number and b.entered_date <= @end_date_prevgroup by b.policy_base, b.policy_suffix, b.vehicle_number/* Get vehicles last endorsed vehicles */insert into #vehinforceselect a.policy_base, a.policy_suffix, a.endorsement, a.vehicle_number, a.vehicle_endorsement, max(c.driver_number) as driver_number, c.driver_classfrom #vehall a WITH (NOLOCK), #vehmax b WITH (NOLOCK), vehicle c WITH (NOLOCK)where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and a.vehicle_endorsement = b.max_endorsement and a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.endorsement = c.endorsement and a.vehicle_number = c.vehicle_number and a.vehicle_endorsement = c.vehicle_endorsementgroup by a.policy_base, a.policy_suffix, a.endorsement, a.vehicle_number, a.vehicle_endorsement, c.driver_class /* Get vehicles last endorsed in 'TX' with 'BI' and 'CL' */insert into #tempprevselect distinct e.policy_base, e.policy_suffix, e.vehicle_number, f.driver_number, e.coverage, f.driver_class, e.rate_zip from policy a WITH (NOLOCK), history e WITH (NOLOCK), #vehinforce f WITH (NOLOCK)where a.policy_base = e.policy_base and a.policy_suffix = e.policy_suffix and a.endorsement = e.endorsement and e.policy_base = f.policy_base and e.policy_suffix = f.policy_suffix and e.endorsement = f.endorsement and e.vehicle_number = f.vehicle_number and e.vehicle_endorsement = f.vehicle_endorsement and e.rate_zip is not null and e.coverage in ('BI','CL') and a.policy_state = 'TX' and a.inception_date <= @end_date_prev and e.effective_date <= @end_date_prev and a.expiration_date >= @end_date_prev and ( a.cancel_date >= @end_date_prev or a.cancel_date is null ) and e.rate_table >= @start_rate_table and e.rate_table <= @end_rate_tablegroup by e.policy_base, e.policy_suffix, e.vehicle_number, f.driver_number, e.coverage, e.rate_zip, f.driver_class/* Get Previous Quarter Vehicle Count by Coverage and Zip */insert into #prev_vehiclesselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, e.rate_zip, previous_vehicles = count( e.vehicle_number ) from #tempprev e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y' ))group by e.coverage, e.rate_zip/* Get Previous Quarter Vehicle Count by Coverage and Zip with Driver Class with business use */insert into #pdri_vehiclesaselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, rate_zip = case when e.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when e.Driver_Class in ('100','101','102') then '99250' when e.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when d.Business_Use = 'Y' and e.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, previous_vehicles = count( e.vehicle_number ) from #tempprev e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y' )) group by e.coverage, e.driver_class, d.business_use/* Combine amounts by driver class */insert into #pdri_vehiclesselect * from #pdri_vehiclesainsert into #pdri_vehicles2select coverage, rate_zip, sum(previous_vehicles) as previous_vehicles from #pdri_vehicles WITH (NOLOCK)group by coverage, rate_zip/* Combine all amounts into one table */insert into #prev_vehicles2select coverage, rate_zip, rtrim(ltrim(str( previous_vehicles ))) as previous_vehicles from #prev_vehiclesinsert into #prev_vehicles2select coverage, rate_zip, rtrim(ltrim(str( previous_vehicles ))) as previous_vehicles from #pdri_vehicles2insert into #prev_vehiclestotselect coverage, rate_zip, previous_vehicles = case when len( previous_vehicles ) = 1 then '0000000' + previous_vehicles when len( previous_vehicles ) = 2 then '000000' + previous_vehicles when len( previous_vehicles ) = 3 then '00000' + previous_vehicles when len( previous_vehicles ) = 4 then '0000' + previous_vehicles when len( previous_vehicles ) = 5 then '000' + previous_vehicles when len( previous_vehicles ) = 6 then '00' + previous_vehicles when len( previous_vehicles ) = 7 then '0' + previous_vehicles when previous_vehicles is null then '00000000' endfrom #prev_vehicles2/* Get initial Vehicles for Current Quarter count *//* Get all Vehicles without a status of 'D' */insert into #vehall2select b.policy_base, b.policy_suffix, b.endorsement, b.vehicle_number, max(b.vehicle_endorsement) as vehicle_endorsement from vehicle b WITH (NOLOCK)where b.entered_date <= @end_date and b.endorse_status <> 'D'group by b.policy_base, b.policy_suffix, b.endorsement, b.vehicle_number/* Get max endorsements of initial Vehicles */insert into #vehmax2select b.policy_base, b.policy_suffix, max(b.endorsement) as endorsement, b.vehicle_number, max(b.vehicle_endorsement) as max_endorsement from #vehall2 a WITH (NOLOCK), vehicle b WITH (NOLOCK)where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.endorsement = b.endorsement and a.vehicle_number = b.vehicle_number and b.entered_date <= @end_dategroup by b.policy_base, b.policy_suffix, b.vehicle_number/* Get vehicles last endorsed vehicles */insert into #vehinforce2select a.policy_base, a.policy_suffix, a.endorsement, a.vehicle_number, a.vehicle_endorsement, max(c.driver_number) as driver_number, c.driver_class from #vehall2 a WITH (NOLOCK), #vehmax2 b WITH (NOLOCK), vehicle c WITH (NOLOCK)where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and a.vehicle_endorsement = b.max_endorsement and a.policy_base = c.policy_base and a.policy_suffix = c.policy_suffix and a.endorsement = c.endorsement and a.vehicle_number = c.vehicle_number and a.vehicle_endorsement = c.vehicle_endorsementgroup by a.policy_base, a.policy_suffix, a.endorsement, a.vehicle_number, a.vehicle_endorsement, c.driver_class /* Get vehicles last endorsed in 'TX' with 'BI' and 'CL' */insert into #tempcurrselect distinct e.policy_base, e.policy_suffix, e.vehicle_number, f.driver_number, e.coverage, f.driver_class, e.rate_zip from policy a WITH (NOLOCK), history e WITH (NOLOCK), #vehinforce2 f WITH (NOLOCK)where a.policy_base = e.policy_base and a.policy_suffix = e.policy_suffix and a.endorsement = e.endorsement and e.policy_base = f.policy_base and e.policy_suffix = f.policy_suffix and e.endorsement = f.endorsement and e.vehicle_number = f.vehicle_number and e.vehicle_endorsement = f.vehicle_endorsement and e.rate_zip is not null and e.coverage in ('BI','CL') and a.policy_state = 'TX' and a.inception_date <= @end_date and e.effective_date <= @end_date and a.expiration_date >= @end_date and ( a.cancel_date >= @end_date or a.cancel_date is null ) and e.rate_table >= @start_rate_table and e.rate_table <= @end_rate_tablegroup by e.policy_base, e.policy_suffix, e.vehicle_number, f.driver_number, e.coverage, e.rate_zip, f.driver_class/* Get Current Quarter Vehicle Count by Coverage and Zip */insert into #curr_vehiclesselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, e.rate_zip, current_vehicles = count( e.vehicle_number ) from #tempcurr e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y' )) group by e.coverage, e.rate_zip/* Get Current Quarter Vehicle Count by Coverage and Zip with Driver Class with business use */insert into #cdri_vehiclesaselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, rate_zip = case when e.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when e.Driver_Class in ('100','101','102') then '99250' when e.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when d.Business_Use = 'Y' and e.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, current_vehicles = count( e.vehicle_number ) from #tempcurr e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y' ))group by e.coverage, e.driver_class, d.business_use/* Combine amounts by driver class */insert into #cdri_vehiclesselect * from #cdri_vehiclesainsert into #cdri_vehicles2select coverage, rate_zip, sum(current_vehicles) as current_vehicles from #cdri_vehicles WITH (NOLOCK)group by coverage, rate_zip/* Combine all amounts into one table */insert into #curr_vehicles2select coverage, rate_zip, rtrim(ltrim(str( current_vehicles ))) as current_vehicles from #curr_vehiclesinsert into #curr_vehicles2select coverage, rate_zip, rtrim(ltrim(str( current_vehicles ))) as current_vehicles from #cdri_vehicles2insert into #curr_vehiclestotselect coverage, rate_zip, current_vehicles = case when len( current_vehicles ) = 1 then '0000000' + current_vehicles when len( current_vehicles ) = 2 then '000000' + current_vehicles when len( current_vehicles ) = 3 then '00000' + current_vehicles when len( current_vehicles ) = 4 then '0000' + current_vehicles when len( current_vehicles ) = 5 then '000' + current_vehicles when len( current_vehicles ) = 6 then '00' + current_vehicles when len( current_vehicles ) = 7 then '0' + current_vehicles when current_vehicles is null then '00000000' end from #curr_vehicles2/* Get Initial Added Vehicles for Current Quarter *//* Get all Vehicles added or new' */insert into #tempaddselect * from #tempcurr a WITH (NOLOCK)where not exists ( select * from #tempprev b WITH (NOLOCK) where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and a.driver_number = b.driver_number and a.coverage = b.coverage and a.driver_class = b.driver_class and a.rate_zip = b.rate_zip )/* Get Added Vehicles during Current Quarter by Coverage and Zip */insert into #add_vehiclesselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, e.rate_zip, add_vehicles = count( e.vehicle_number )from #tempadd e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y'))group by e.coverage, e.rate_zip/* Get Added Vehicles during Current Quarter by Coverage and Zip with Driver Class with business use */insert into #adri_vehiclesaselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, rate_zip = case when e.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when e.Driver_Class in ('100','101','102') then '99250' when e.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when d.Business_Use = 'Y' and e.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, add_vehicles = count( e.vehicle_number ) from #tempadd e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y' ))group by e.coverage, e.driver_class, d.business_use/* Combine amounts by driver class */insert into #adri_vehiclesselect * from #adri_vehiclesainsert into #adri_vehicles2select coverage, rate_zip, sum(add_vehicles) as add_vehiclesfrom #adri_vehicles WITH (NOLOCK)group by coverage, rate_zip/* Combine all amounts into one table */insert into #add_vehicles2select coverage, rate_zip, rtrim(ltrim(str( add_vehicles ))) as add_vehicles from #add_vehiclesinsert into #add_vehicles2select coverage, rate_zip, rtrim(ltrim(str( add_vehicles ))) as add_vehicles from #adri_vehicles2insert into #add_vehiclestotselect coverage, rate_zip, add_vehicles = case when len( add_vehicles ) = 1 then '0000000' + add_vehicles when len( add_vehicles ) = 2 then '000000' + add_vehicles when len( add_vehicles ) = 3 then '00000' + add_vehicles when len( add_vehicles ) = 4 then '0000' + add_vehicles when len( add_vehicles ) = 5 then '000' + add_vehicles when len( add_vehicles ) = 6 then '00' + add_vehicles when len( add_vehicles ) = 7 then '0' + add_vehicles when add_vehicles is null then '00000000' end from #add_vehicles2/* Get Canceled Vehicles during Current Quarter by Coverage and Zip */insert into #canceledselect * from #tempprev a WITH (NOLOCK)where not exists ( select * from #tempcurr b WITH (NOLOCK) where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.vehicle_number = b.vehicle_number and a.driver_number = b.driver_number and a.coverage = b.coverage and a.driver_class = b.driver_class and a.rate_zip = b.rate_zip )/* Get Cancel Codes for Vehicles */insert into #tempcanselect b.*, a.cancel_code from policy a WITH (NOLOCK), #canceled b WITH (NOLOCK)where a.policy_base = b.policy_base and a.policy_suffix = b.policy_suffix and a.current_flag = 'Y'/* Set Cancel Code to IR for Deleted and Expired Vehicles */update #tempcanset cancel_code = 'IR'where cancel_code is null or cancel_code like '% %'/* Get Canceled Vehicles during Current Quarter by Coverage and Zip */insert into #can_vehiclesselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, e.rate_zip, insurer_vehicles = case when e.cancel_code in ('CR','UD','UI','UL','UV','UW','RW') then count( e.vehicle_number ) end, nonpay_vehicles = case when e.cancel_code in ('AD','AP','AR','FN','NA','ND','NF','NI','NP','NS') then count( e.vehicle_number ) end, insured_vehicles = case when e.cancel_code in ('II','IR') then count( e.vehicle_number ) end from #tempcan e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y'))group by e.coverage, e.rate_zip, e.cancel_code/* Get Canceled Vehicles during Current Quarter by Coverage and Zip with Driver Class with business use*/insert into #candri_vehiclesaselect coverage = case when e.coverage = 'BI' then 1 when e.coverage = 'CL' then 3 end, rate_zip = case when e.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200' when e.Driver_Class in ('100','101','102') then '99250' when e.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600' when d.Business_Use = 'Y' and e.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300' else '99100' end, insurer_vehicles = case when e.cancel_code in ('CR','UD','UI','UL','UV','UW','RW') then count( e.vehicle_number ) end, nonpay_vehicles = case when e.cancel_code in ('AD','AP','AR','FN','NA','ND','NF','NI','NP','NS') then count( e.vehicle_number ) end, insured_vehicles = case when e.cancel_code in ('II','IR') then count( e.vehicle_number ) end from #tempcan e WITH (NOLOCK) LEFT OUTER JOIN driver d WITH (NOLOCK) ON (e.policy_base = d.policy_base and e.policy_suffix = d.policy_suffix and e.driver_number = d.driver_number and (d.current_flag = 'Y'))group by e.coverage, e.cancel_code, e.driver_class, d.business_use/* Combine canceled, expired and deleted into one table */insert into #can_vehicles2select * from #can_vehicles/* Combine canceled, expired and deleted into one table by driver class with business use */insert into #candri_vehiclesa2select * from #candri_vehiclesa/* Combine canceled and expired into one table by driver class */insert into #candri_vehiclesselect * from #candri_vehiclesa2/* Combine sums into one table */insert into #can_vehicles3select coverage, rate_zip, sum(insurer_vehicles) as insurer_vehicles, sum(nonpay_vehicles) as nonpay_vehicles, sum(insured_vehicles) as insured_vehicles from #can_vehicles2group by coverage, rate_zipinsert into #can_vehicles3select coverage, rate_zip, sum(insurer_vehicles) as insurer_vehicles, sum(nonpay_vehicles) as nonpay_vehicles, sum(insured_vehicles) as insured_vehicles from #candri_vehiclesgroup by coverage, rate_zipinsert into #can_vehicles4select coverage, rate_zip, rtrim(ltrim(str( insurer_vehicles ))) as insurer_vehicles, rtrim(ltrim(str( nonpay_vehicles ))) as nonpay_vehicles, rtrim(ltrim(str( insured_vehicles ))) as insured_vehicles from #can_vehicles3insert into #can_vehiclestotselect coverage, rate_zip, insurer_vehicles = case when len( insurer_vehicles ) = 1 then '0000000' + insurer_vehicles when len( insurer_vehicles ) = 2 then '000000' + insurer_vehicles when len( insurer_vehicles ) = 3 then '00000' + insurer_vehicles when len( insurer_vehicles ) = 4 then '0000' + insurer_vehicles when len( insurer_vehicles ) = 5 then '000' + insurer_vehicles when len( insurer_vehicles ) = 6 then '00' + insurer_vehicles when len( insurer_vehicles ) = 7 then '0' + insurer_vehicles when insurer_vehicles is null then '00000000' end, nonpay_vehicles = case when len( nonpay_vehicles ) = 1 then '0000000' + nonpay_vehicles when len( nonpay_vehicles ) = 2 then '000000' + nonpay_vehicles when len( nonpay_vehicles ) = 3 then '00000' + nonpay_vehicles when len( nonpay_vehicles ) = 4 then '0000' + nonpay_vehicles when len( nonpay_vehicles ) = 5 then '000' + nonpay_vehicles when len( nonpay_vehicles ) = 6 then '00' + nonpay_vehicles when len( nonpay_vehicles ) = 7 then '0' + nonpay_vehicles when nonpay_vehicles is null then '00000000' end, insured_vehicles = case when len( insured_vehicles ) = 1 then '0000000' + insured_vehicles when len( insured_vehicles ) = 2 then '000000' + insured_vehicles when len( insured_vehicles ) = 3 then '00000' + insured_vehicles when len( insured_vehicles ) = 4 then '0000' + insured_vehicles when len( insured_vehicles ) = 5 then '000' + insured_vehicles when len( insured_vehicles ) = 6 then '00' + insured_vehicles when len( insured_vehicles ) = 7 then '0' + insured_vehicles when insured_vehicles is null then '00000000' endfrom #can_vehicles4/* Find all possible coverage and zip code combinations */update #written_totalset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #written_total update #fees_totalset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #fees_totalupdate #prev_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #prev_vehiclestotupdate #curr_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #curr_vehiclestotupdate #add_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #add_vehiclestotupdate #can_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #can_vehiclestotupdate #zipsset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zip_codesselect distinct coverage, rate_zip from #zips order by rate_zip, coverageupdate #zip_codesset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #texas_qtrselect a.coverage, a.rate_zip, written = case when b.written is not null then b.written else '000000000000' end, fees = case when c.fees is not null then c.fees else '000000000' end, previous_vehicles = case when d.previous_vehicles is not null then d.previous_vehicles else '00000000' end, current_vehicles = case when e.current_vehicles is not null then e.current_vehicles else '00000000' end, add_vehicles = case when f.add_vehicles is not null then f.add_vehicles else '00000000' end, insurer_vehicles = case when g.insurer_vehicles is not null then g.insurer_vehicles else '00000000' end, nonpay_vehicles = case when g.nonpay_vehicles is not null then g.nonpay_vehicles else '00000000' end, insured_vehicles = case when g.insured_vehicles is not null then g.insured_vehicles else '00000000' end from #zip_codes a WITH (NOLOCK) LEFT OUTER JOIN #written_total b WITH (NOLOCK) ON (a.rate_zip = b.rate_zip and a.coverage = b.coverage) LEFT OUTER JOIN #fees_total c WITH (NOLOCK) ON (a.rate_zip = c.rate_zip and a.coverage = c.coverage) LEFT OUTER JOIN #prev_vehiclestot d WITH (NOLOCK) ON (a.rate_zip = d.rate_zip and a.coverage = d.coverage) LEFT OUTER JOIN #curr_vehiclestot e WITH (NOLOCK) ON (a.rate_zip = e.rate_zip and a.coverage = e.coverage) LEFT OUTER JOIN #add_vehiclestot f WITH (NOLOCK) ON (a.rate_zip = f.rate_zip and a.coverage = f.coverage) LEFT OUTER JOIN #can_vehiclestot g WITH (NOLOCK) ON (a.rate_zip = g.rate_zip and a.coverage = g.coverage) delete from texas_quarterly_marketinsert into Texas_Quarterly_Marketselect texas = '14' + '29378' + '590' + @accounting_date + str(coverage,1) + str(rate_zip,5) + '0000' + written + ' ' + fees + previous_vehicles + current_vehicles + ' 1' + add_vehicles + insurer_vehicles + nonpay_vehicles + insured_vehicles from #texas_qtr WITH (NOLOCK)select @filename = convert(char(12),'QM'+convert(char(1),@current_qtr)+'29378.txt')select * from Texas_Quarterly_Market WITH (NOLOCK) |
 |
|
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-08-20 : 16:37:19
|
| takes 1:20:00 to run, with table variables i let it run 16 hours and it did not finish. Execution plan shows a ton more table scans for some reason. |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-08-20 : 16:40:53
|
quote: Originally posted by Peso Probably none. I think the procedure will perform worse.The reason is that table variables doesn't have statistics, so the query engine assumes there is one and one record only in the table.Even if there are thousands. N 56°04'39.26"E 12°55'05.63"
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-20 : 16:41:44
|
You can do TONs of thing to speed things up.For example this two pieces of codeinsert into #prev_vehiclestotselect coverage,rate_zip,previous_vehicles = case when len( previous_vehicles ) = 1 then '0000000' + previous_vehicleswhen len( previous_vehicles ) = 2 then '000000' + previous_vehicleswhen len( previous_vehicles ) = 3 then '00000' + previous_vehicleswhen len( previous_vehicles ) = 4 then '0000' + previous_vehicleswhen len( previous_vehicles ) = 5 then '000' + previous_vehicleswhen len( previous_vehicles ) = 6 then '00' + previous_vehicleswhen len( previous_vehicles ) = 7 then '0' + previous_vehicleswhen previous_vehicles is null then '00000000' endfrom #prev_vehicles2update #can_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' ' can be rewritten asinsert #prev_vehiclestotselect coverage, COALESCE(NULLIF(rate_zip, ''), '00000') AS rate_zip, REPLACE(STR(COALESCE(previous_vehicles, 0), 8), ' ', '0') AS previous_vehiclesfrom #prev_vehicles2 N 56°04'39.26"E 12°55'05.63" |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-08-20 : 16:43:56
|
| I hope that this isn't for billing purposes with those nolock hints against the base tables |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-20 : 16:46:16
|
| One thing I would do is add some timestamp logging or at last print statement that includes the current time so that you can attack (optimize) the statements individually starting with the longest duration.Another thing I would do is see if any of the long running statements can be combined so that reduce the number of SELECTs from big tables.Another possibility is when you have large temp tables that you're using a lot it can pay to create statistics on them so that the optimizer has a better chance of choosing a good plan.Be One with the OptimizerTG |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-20 : 16:48:37
|
You also have a LOT of redundant code.See thisupdate #can_vehiclestotset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' 'insert into #zipsselect distinct coverage, rate_zip from #can_vehiclestotupdate #zipsset rate_zip = '00000'where rate_zip is null or rate_zip = ' ' or rate_zip = ' ' You can probably loose 60-70% of your code if you are willing to invest some time into it. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-08-20 : 16:50:00
|
| some of those update statements can be eliminated altogether and handled in the select/insert statements. |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-08-20 : 16:53:08
|
| Also eliminate multiple temp tables of the same structure (#driver_written, #driver_writtena, #driver_fees). Just create one temp and insert directly into it. You're consolidating most of these into a single table anyway. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-20 : 17:48:21
|
I have found over 80 cases of redundant code.For example, this is done 8 times on same datarate_zip = case when e.Driver_Class in ('300','301','302','303','304','400','401','402','403','404') then '99200'when e.Driver_Class in ('100','101','102') then '99250'when e.Driver_Class in ('114','115','116','214','215','216','314','315','316','414','415','416') then '99600'when d.Business_Use = 'Y' and e.Driver_Class not in ('100','101','102','300','301','302','303','304','400','401','402','403','404') then '99300'else '99100'end,insurer_vehicles = case when e.cancel_code in ('CR','UD','UI','UL','UV','UW','RW') then count( e.vehicle_number ) end,nonpay_vehicles = case when e.cancel_code in ('AD','AP','AR','FN','NA','ND','NF','NI','NP','NS') then count( e.vehicle_number ) end,insured_vehicles = case when e.cancel_code in ('II','IR') then count( e.vehicle_number ) end N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-20 : 17:54:21
|
This part is used 20 timesFROM History AS aLEFT JOIN Vehicle AS b ON b.policy_base = a.policy_base AND b.policy_suffix = a.policy_suffix AND b.vehicle_number = a.vehicle_number AND (b.current_flag = 'Y' or b.endorse_status = 'D')LEFT JOIN driver AS c ON c.policy_base = a.policy_base AND c.policy_suffix = a.policy_suffix AND c.driver_number = a.driver_number AND (c.current_flag = 'Y' or c.endorse_status = 'D')WHERE a.policy_state = 'TX' AND a.rate_zip IS NOT NULL AND a.rate_table >= @start_rate_table AND a.rate_table <= @end_rate_table N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-20 : 17:55:59
|
Here is a MUCH better approach, because you only reference History, driver and vehicle tables once!ALTER PROCEDURE dbo.Texas_Quarterly_Market_Report( @current_qtr TINYINT, @current_year int, @start_rate_table int, @end_rate_table int)ASSET NOCOUNT ON-- Check for invalid user supplied parameter valuesIF @current_qtr IS NULL OR @current_qtr NOT BETWEEN 1 AND 4 BEGIN RAISERROR('Invalid value (%d) for current quarter selected.', 16, 1, @current_qtr) RETURN -100 ENDIF @current_year IS NULL OR @current_year NOT BETWEEN 2000 AND 2099 BEGIN RAISERROR('Invalid value (%d) for current year selected.', 16, 1, @current_year) RETURN -110 END-- Declare work variablesDECLARE @start_date DATETIME, @end_date DATETIME, @end_date_prev DATETIME, @current_month CHAR(2), @accounting_date CHAR(4), @begin_period CHAR(6), @end_period CHAR(6), @filename VARCHAR(12)-- Set dates depending on quarter enteredSELECT @start_date = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22803, 0), @end_date = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22800, -1), @end_date_prev = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22803, -1), @current_month = REPLACE(STR(3 * @current_qtr, 2), ' ', '0'), @accounting_date = CONVERT(CHAR(4), @end_date, 12), @begin_period = CONVERT(CHAR(6), @start_date, 112), @end_period = CONVERT(CHAR(6), @end_date, 112)-- Stage data to work with laterSELECT CASE a.coverage WHEN 'BI' THEN 1 when 'CL' THEN 3 END AS coverage, a.driver_class, CASE WHEN a.Driver_Class LIKE '[34]0[01234]' THEN '99200' WHEN a.Driver_Class IN ('100', '101', '102') THEN '99250' WHEN a.Driver_Class LIKE '[1234]1[456]' THEN '99600' WHEN c.Business_Use = 'Y' AND a.Driver_Class NOT IN ('100', '101', '102', '300', '301', '302', '303', '304', '400', '401', '402', '403', '404') THEN '99300' ELSE '99100' END AS rate_zip, CASE WHEN a.cancel_code IN ('CR', 'UD', 'UI', 'UL', 'UV', 'UW', 'RW') THEN 1 ELSE 0 END AS insurer_vehicles, CASE WHEN a.cancel_code IN ('AD', 'AP', 'AR', 'FN', 'NA', 'ND', 'NF', 'NI', 'NP', 'NS') THEN 1 ELSE 0 END AS nonpay_vehicles, CASE WHEN a.cancel_code IN ('II', 'IR') THEN 1 WHEN a.cancel_code IS NULL THEN 1 WHEN a.cancel_code LIKE '% %' THEN 1 ELSE 0 END AS insured_vehicles, CASE WHEN a.coverage IN ('BI', 'CL') THEN written_premium ELSE 0 END AS Written, CASE WHEN a.coverage IN ('FE') THEN written_premium ELSE 0 END AS Fees, CASE WHEN a.accounting_period >= @begin_period AND a.accounting_period <= @end_period THEN 1 ELSE 0 END AS [Current] INTO #TexasFROM History AS aLEFT JOIN Vehicle AS b ON b.policy_base = a.policy_base AND b.policy_suffix = a.policy_suffix AND b.vehicle_number = a.vehicle_number AND (b.current_flag = 'Y' or b.endorse_status = 'D')LEFT JOIN driver AS c ON c.policy_base = a.policy_base AND c.policy_suffix = a.policy_suffix AND c.driver_number = a.driver_number AND (c.current_flag = 'Y' or c.endorse_status = 'D')WHERE a.policy_state = 'TX' AND a.rate_zip IS NOT NULL AND a.rate_table >= @start_rate_table AND a.rate_table <= @end_rate_table/* Now you have a staging table with all data you need. Use SUM() OVER (PARTITION BY ...) to accumulate the data you need.*/ N 56°04'39.26"E 12°55'05.63" |
 |
|
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-08-21 : 14:45:44
|
| Thanks for all the great tips, guys.I was a little worried about posting so much code all at once, didn't think any one would bother sorting through it all.This is the biggest procedure I have ever had to work on. I only sat down to it a couple of days ago and read over it over and over trying to understand exactly what it does. The fact the it outputs to a single 100 length string(first two chars = something, 3-5 equals something, etc) didn't help.First thing I thought of was to use table variables, but that made it run way, way longer. The stuff you guys pointed out will really help me focus on areas that can be improved.I will work on it and post what I come up with from your suggestions and my own ideas.Thank you so much. |
 |
|
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-08-24 : 15:59:00
|
| Peso, I had to modify what you posted a bit. Is my logic correct?alter PROCEDURE dbo.Texas_Quarterly_Market_Report_dev2( @current_qtr TINYINT, @current_year int, @start_rate_table int, @end_rate_table int)ASSET NOCOUNT ON-- Declare work variablesDECLARE @start_date DATETIME, @end_date DATETIME, @end_date_prev DATETIME, @current_month CHAR(2), @accounting_date CHAR(4), @begin_period CHAR(6), @end_period CHAR(6), @filename VARCHAR(12)-- Set dates depending on quarter enteredSELECT @start_date = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22803, 0), @end_date = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22800, -1), @end_date_prev = DATEADD(MONTH, 12 * @current_year + 3 * @current_qtr - 22803, -1), @current_month = REPLACE(STR(3 * @current_qtr, 2), ' ', '0'), @accounting_date = CONVERT(CHAR(4), @end_date, 12), @begin_period = CONVERT(CHAR(6), @start_date, 112), @end_period = CONVERT(CHAR(6), @end_date, 112)set nocount off-- Stage data to work with laterSELECT CASE a.coverage WHEN 'BI' THEN 1 when 'CL' THEN 3 END AS coverage, a.driver_class, CASE WHEN a.Driver_Class LIKE '[34]0[01234]' THEN '99200' WHEN a.Driver_Class IN ('100', '101', '102') THEN '99250' WHEN a.Driver_Class LIKE '[1234]1[456]' THEN '99600' WHEN c.Business_Use = 'Y' AND a.Driver_Class NOT IN ('100', '101', '102', '300', '301', '302', '303', '304', '400', '401', '402', '403', '404') THEN '99300' ELSE '99100' END AS rate_zip, CASE WHEN d.cancel_code IN ('CR', 'UD', 'UI', 'UL', 'UV', 'UW', 'RW') THEN 1 ELSE 0 END AS insurer_vehicles, CASE WHEN d.cancel_code IN ('AD', 'AP', 'AR', 'FN', 'NA', 'ND', 'NF', 'NI', 'NP', 'NS') THEN 1 ELSE 0 END AS nonpay_vehicles, CASE WHEN d.cancel_code IN ('II', 'IR') THEN 1 WHEN d.cancel_code IS NULL THEN 1 WHEN d.cancel_code LIKE '% %' THEN 1 ELSE 0 END AS insured_vehicles, CASE WHEN a.coverage IN ('BI', 'CL') THEN written_premium ELSE 0 END AS Written, CASE WHEN a.coverage IN ('FE') THEN written_premium ELSE 0 END AS Fees, CASE WHEN a.accounting_period >= @begin_period AND a.accounting_period <= @end_period THEN 1 ELSE 0 END AS [Current] INTO #TexasFROM History AS aLEFT JOIN Vehicle AS bON b.policy_base = a.policy_baseAND b.policy_suffix = a.policy_suffixAND b.vehicle_number = a.vehicle_numberAND (b.current_flag = 'Y' or b.endorse_status = 'D')LEFT JOIN driver AS cON c.policy_base = a.policy_baseAND c.policy_suffix = a.policy_suffixAND c.driver_number = b.driver_numberAND (c.current_flag = 'Y' or c.endorse_status = 'D')LEFT JOIN policy AS dON d.policy_base = a.policy_baseAND d.policy_suffix = a.policy_suffixWHERE a.policy_state = 'TX' AND a.rate_zip IS NOT NULL AND a.rate_table >= @start_rate_table AND a.rate_table <= @end_rate_table |
 |
|
|
|
|
|
|
|