Re: SQL Taking too long .
From: Adam Machanic (amachanic_at_hotmail._removetoemail_.com)
Date: 02/22/05
- Next message: Michael C#: "Re: Last payment is also a bigger payment"
- Previous message: Michael C#: "Re: Last payment is also a bigger payment"
- In reply to: SQL Taking to LONG: "SQL Taking too long ."
- Next in thread: Mike Hodgson: "Re: SQL Taking too long ."
- Reply: Mike Hodgson: "Re: SQL Taking too long ."
- Reply: SQL Taking to LONG: "Re: SQL Taking too long ."
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 21 Feb 2005 20:41:18 -0500
Where is @Budget_Amount3 declared?
To speed this up, simply get rid of the cursor... here's the same logic,
cursorless:
update OT_ACTUAL_BUDGET_BALANCES
set [Budget_Amount3]=@Budget_Amount3
WHERE EXISTS
(SELECT *
FROM BUDGET_BALANCES B
WHERE B.account_code = OT_ACTUAL_BUDGET_BALANCES.account_code
AND B.district_code='OTML')
INSERT OT_ACTUAL_BUDGET_BALANCES
SELECT B.account_code
FROM BUDGET_BALANCES B
WHERE NOT EXISTS
(SELECT *
FROM OT_ACTUAL_BUDGET_BALANCES
WHERE B.account_code = OT_ACTUAL_BUDGET_BALANCES.account_code
AND B.district_code='OTML')
-- Adam Machanic SQL Server MVP http://www.sqljunkies.com/weblog/amachanic -- "SQL Taking to LONG" <SQL Taking to LONG@discussions.microsoft.com> wrote in message news:A428243E-C987-47F4-87C4-16D195A6C49F@microsoft.com... > Can someone please help me out. My sql is taking alsmost 16 hours to complete > and is not exceptable. Am creating a cursor from a table A which has about > 120,000 record. For each record if this record exists in table B then update > the columns otherwise insert a new record in this table B. > > I was able to reduce the time to 8 hours by creating an index on table B > however I still want to reduce this further. Attach is my sql. This is urgent > and if someone can help it will be greatly appreciated.. > > Thanks > > --******************************* > /**************************budget_balance row > olders********************************/ > set nocount on > declare @district_code [nvarchar] (4) > declare @account_code [nvarchar] (24) > > /*other variables*/ > DECLARE @TranBudget VARCHAR(20) > SELECT @TranBudget = 'Budgets' > > declare @rowcount int > declare @cnt int > declare @budgetTotalRows int > declare @msg nvarchar(200) > set @rowcount = 1 > set @cnt=0 > set @budgetTotalRows=(select count(*) from budget_balances) > /*budget cursor*/ > print 'Starting Actual Budget Balances...' > > DECLARE budget_cursor CURSOR FOR > SELECT account_code > FROM BUDGET_BALANCES > WHERE district_code='OTML' > > OPEN budget_cursor > > FETCH NEXT FROM budget_cursor > INTO @account_code > > WHILE @@FETCH_STATUS = 0 > BEGIN > BEGIN TRANSACTION @TranBudget > set @msg=N'RowNumber: ' + rtrim(cast(@rowcount as char)) + N'/' + > rtrim(cast(@budgetTotalRows as char)) --+ N' -->Processing Account No:' + > cast(@account_code as char) > set @cnt = @cnt + 1 > print @msg > > /*Check if this budget has a record existing in OT_ACTUAL_BUDGET_BALANCES > table*/ > if exists (select account_code from OT_ACTUAL_BUDGET_BALANCES > where account_code=@account_code > ) > > begin > /*Only update OT_ACTUAL_BUDGET_BALANCES table*/ > update OT_ACTUAL_BUDGET_BALANCES set [Budget_Amount3]=@Budget_Amount3 > > where account_code=@account_code > > > set @rowcount = @rowcount + 1 > > end > > else > begin > /*Insert this record*/ > insert into OT_ACTUAL_BUDGET_BALANCES (account_code) > values(@account_code) > set @rowcount = @rowcount + 1 > end > > if @rowcount=1000 > --print 'Commiting the A 1000 records-->::' + @msg > begin > COMMIT TRANSACTION Budgets > set @rowcount=1 > end > /*next fetch*/ > FETCH NEXT FROM budget_cursor > INTO @account_code > > END > CLOSE budget_cursor > DEALLOCATE budget_cursor > GO > --********************************* > > > >
- Next message: Michael C#: "Re: Last payment is also a bigger payment"
- Previous message: Michael C#: "Re: Last payment is also a bigger payment"
- In reply to: SQL Taking to LONG: "SQL Taking too long ."
- Next in thread: Mike Hodgson: "Re: SQL Taking too long ."
- Reply: Mike Hodgson: "Re: SQL Taking too long ."
- Reply: SQL Taking to LONG: "Re: SQL Taking too long ."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|