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 |
fan2005
Yak Posting Veteran
85 Posts |
Posted - 2011-05-17 : 04:06:13
|
Hi ,I'm looking for an alternative query for this one.It's runing on 1000000 rows and is realy slowI coundnt' make cursor to work with dynamic sql although I guess cursor is alsow slow.I didn't create index on ID column .If it'll help what kind should it be?(CLUSTERED /NON CLUSTERED )Thanksdeclare @total intset @total=(select max(id) from cashflow_1month_interests)declare @row int set @row=1DECLARE @pTDOPNDAT varchar(10)declare @TBTEMPRATE floatDECLARE @TDOPNAMT decimal(20,0)DECLARE @pdate nvarchar(10)DECLARE @interest decimal(20,0)DECLARE @sql varchar(4000)DECLARE @TFDPDURATN int DECLARE @i int while @row<=@totalbegin set @TDOPNAMT =(select cast (TDOPNAMT as decimal(20,0) ) from cashflow_1month_interests where id=@row) set @TBTEMPRATE =(select cast (TBTEMPRATE as float )from cashflow_1month_interests where id=@row) set @TFDPDURATN=(select cast(TFDPDURATN as int) from cashflow_1month_interests where id=@row) set @pdate=(select PTDOPNDAT from cashflow_1month_interests where id=@row)set @i =1 while @i<= @TFDPDURATN begin set @interest = @TDOPNAMT*(@TBTEMPRATE)* dbo.ShamsiDateDiff (@pdate,dbo.nextMonth(@pdate))/36500 if @i< cast(@TFDPDURATN as int) begin set @sql='UPDATE cashflow_1month_interests SET intersts_of_month'+cast(@i as varchar(2))+' ='+cast( @interest as nvarchar(255))+' where id='+cast (@row as nvarchar(255)) print( @sql ) exec (@sql) end if @i= cast(@TFDPDURATN as int) begin set @sql='UPDATE cashflow_1month_interests SET intersts_of_month'+cast(@i as varchar(2))+' = '+cast( @interest+@TDOPNAMT as nvarchar(255))+' where id='+cast (@row as nvarchar(255)) print( @sql ) exec (@sql) end set @pdate=dbo.nextMonth(@pdate) set @i=@i+1 endset @row=@row+1end |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-05-17 : 11:03:27
|
You have two nested WHILE clauses - for each column inside for each row. There is no way that design will run efficiently. If you want to take advantage of Sql Server's set-based advantages you need to change your model. How many [interest] columns are in the table? Do you keep adding columns as time goes by? You should make that table Long and narrow rather than wider (too many columns). One [Interest] column and a [month] column. That way, if the values need to be updated periodically you can do it in a single statement rather than (1m * <number of column>) statements.If you want some additional help please post the (scripted out) table structure including indexes as well as script out the user defined functions you are using.Be One with the OptimizerTG |
|
|
fan2005
Yak Posting Veteran
85 Posts |
Posted - 2011-05-17 : 12:26:52
|
Thanks for your replyThe table has columns intersts_of_month1 to intersts_of_month60 to be updated. I added these columns at once.Interests for all month has to be predicted the number of rows is about 1 million records.If I design it long and narrow (vertical) for a deposit which has TFDPDURATN =60 I'll need to insert that deposit 60 times and in each row i have to compute Interest and pass it as values.so it's faster.The one who wanted this report first wanted to read it like first design but they're OK with second also.(I don't know how they want to use it because it's long enough to not call it report.)I also think vertical design is more readable.If I'm wrong please correct me.if scripts of table-design and user-functions is needed I'll add it tomorrow.Thanks |
|
|
X002548
Not Just a Number
15586 Posts |
|
fan2005
Yak Posting Veteran
85 Posts |
Posted - 2011-05-18 : 00:16:29
|
[code]CREATE TABLE [salavizadeh].[cashflow_1month_interests]( [id] [bigint] IDENTITY(1,1) NOT NULL, [DEPOSITS] [varchar](255) NULL, [ABRNCHCOD] [varchar](255) NULL, [TBDPTYPE] [varchar](255) NULL, [CFCIFNO] [varchar](255) NULL, [TDSERIAL] [varchar](255) NULL, [MTDOPNDAT] [varchar](255) NULL, [TBRATEDATE] [varchar](255) NULL, [TDOPNAMT] [varchar](255) NULL, [TDINTDAY] [varchar](255) NULL, [TDXTRINT] [varchar](255) NULL, [PTDOPNDAT] [varchar](255) NULL, [TDXPRDAT] [varchar](255) NULL, [TDINTDAT] [varchar](255) NULL, [TDCLSDAT] [varchar](255) NULL, [TDLSTPRF] [varchar](255) NULL, [TDRNWDAT] [varchar](255) NULL, [TBPNLTYRATE] [varchar](255) NULL, [TBTEMPRATE] [varchar](255) NULL, [TFDPINTFRQ] [varchar](255) NULL, [TFDPDURATN] [varchar](255) NULL, ) ON [PRIMARY]GOSET ANSI_PADDING OFFGO[/code][code]/****** Object: Index [IXC_id] Script Date: 05/18/2011 08:12:39 ******/CREATE CLUSTERED INDEX [IXC_id] ON [salavizadeh].[cashflow_1month_interests] ( [id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]GO[/code][code]INSERT INTO [Center_Test].[salavizadeh].[cashflow_1month_interests] ([DEPOSITS] ,[ABRNCHCOD] ,[TBDPTYPE],[CFCIFNO],[TDSERIAL],[MTDOPNDAT] ,[TBRATEDATE],[TDOPNAMT],[TDINTDAY],[TDXTRINT] ,[PTDOPNDAT],[TDXPRDAT] ,[TDINTDAT],[TDCLSDAT],[TDLSTPRF],[TDRNWDAT],[TBPNLTYRATE],[TBTEMPRATE] ,[TFDPINTFRQ],[TFDPDURATN] ) VALUES ( 291-966-454890-12 ,291 ,966, 454890, 12, 2008/03/06, 1386/04/13, 1000000.00 , 1,'','1386/12/16', '1390/12/16',' ', ' ', ' ' ,'1386/12/16', 0.50 ,17.10, 1, 48 ), 360-901-1169058-2,360, 901, 1169058,2, 2010/10/09, 1389/07/10, 10000000.00 , 17 ,'',1389/07/17 ,1390/07/17 ,'','','', 1389/07/17, '', 15.00 ,1,12 ) [/code] |
|
|
MSquared
Yak Posting Veteran
52 Posts |
|
|
|
|
|
|