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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 any faster solution?

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 slow
I 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 )
Thanks



declare @total int
set @total=(select max(id) from cashflow_1month_interests)

declare @row int
set @row=1

DECLARE @pTDOPNDAT varchar(10)
declare @TBTEMPRATE float
DECLARE @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<=@total
begin

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
end
set @row=@row+1
end

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 Optimizer
TG
Go to Top of Page

fan2005
Yak Posting Veteran

85 Posts

Posted - 2011-05-17 : 12:26:52
Thanks for your reply
The 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



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-05-17 : 13:46:03
ooooo...scary stuff

give us a BRD and post the table DDL

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

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]

GO

SET ANSI_PADDING OFF
GO
[/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]


Go to Top of Page

MSquared
Yak Posting Veteran

52 Posts

Posted - 2011-05-18 : 08:17:11
Can you provide the code for these objects? Also, what are you expecting as results based on the data you provided?

dbo.ShamsiDateDiff
dbo.nextMonth

For Faster results please follow the posting guidelines here

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -