Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server 2005 Performance Tuning
»
please help me tunnig part of below query?
please help me tunnig part of below query?
Rate Topic
Display Mode
Topic Options
Author
Message
santoshpatil.kumar
santoshpatil.kumar
Posted Tuesday, December 18, 2012 11:44 PM
Forum Newbie
Group: General Forum Members
Last Login: Wednesday, December 19, 2012 11:28 PM
Points: 1,
Visits: 3
USE [PD_ODS]
GO
/****** Object: StoredProcedure [dbo].[sp_replicate_nxtdate] Script Date: 12/13/2012 13:44:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[sp_replicate_nxtdate]
@p_siteid varchar(100)
as
begin
declare @pmnum varchar(100)
declare @description varchar(400)
declare @assetnum varchar(100)
declare @assetname varchar(100)
declare @failurecode varchar(100)
declare @jpnum varchar(100)
declare @craft varchar(100)
declare @frequnit varchar(100)
declare @frequency int
declare @jpduration decimal(10,2)
declare @actual_hours decimal(10,2)
declare @key varchar(100)
declare @new_key varchar(100)
declare @siteid varchar(100)
declare @orgid varchar(100)
declare @version int
declare @changedate datetime
declare @change_user varchar(30)
declare @nextdate datetime
declare @extdate varchar(20)
declare @rpct_f char(1)
declare @currdate datetime
declare @v1_month int
declare @v2_week int
declare @v3_year int
declare @v4_day int
declare @vw int
declare @vm int
declare @vy int
declare @vd int
--cursor declaration--
declare cursor_mstr_extr CURSOR for
select * from dbo.PRVN_MNTE_MSTR_EXTR where version=1 and siteid=@p_siteid order by pmnum --and pmnum=33473
--open cursor--
--delete from replica where next_date is not null
set @v1_month=24
set @v2_week=104
set @v3_year=2
set @v4_day=730
open cursor_mstr_extr
fetch next from cursor_mstr_extr into @pmnum,@description,@assetnum,@assetname,@failurecode,@jpnum,
@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,@rpct_f
if @pmnum is not null and @frequnit is not null
and @frequency is not null and @nextdate is not null
begin
---declare @currdate datetime---
WHILE @@FETCH_STATUS = 0
begin
set @vm=1
set @vw=1
set @vy=1
set @vd=1
select @currdate=min(nextdate) from PRVN_MNTE_MSTR_EXTR where pmnum=@pmnum
if @frequnit like 'MONTH%'
begin
while @vm<=@v1_month/@frequency and @nextdate<dateadd(yyyy,2,@currdate)
begin
set @nextdate=(dateadd(mm,@frequency,@nextdate))
insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,
@jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )
--set @cdate=@next_date
set @vm=@vm+1
end
end
else
if @frequnit like 'WEEK%'
begin
while @vw<=@v2_week/@frequency and @nextdate<dateadd(yyyy,2,@currdate)
begin
set @nextdate=(dateadd(wk,@frequency,@nextdate))
insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,
@jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )
--set @cdate=@next_date
set @vw=@vw+1
end
end
else
if @frequnit like 'YEAR%'
begin
while @vy<=@v3_year/@frequency and @nextdate<dateadd(yyyy,2,@currdate)
begin
set @nextdate=(dateadd(yyyy,@frequency,@nextdate))
insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,
@jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )
--set @cdate=@next_date
set @vy=@vy+1
end
end
else
if @frequnit like 'DAY%'
begin
while @vd<=@v4_day/@frequency and @nextdate<dateadd(yyyy,2,@currdate)
begin
set @nextdate=(dateadd(dd,@frequency,@nextdate))
insert into PRVN_MNTE_MSTR_EXTR values(@pmnum,@description,@assetnum,@assetname,@failurecode,
@jpnum,@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,'R' )
--set @cdate=@next_date
set @vd=@vd+1
end
end
else break
---fetch next row---
fetch next from cursor_mstr_extr into @pmnum,@description,@assetnum,@assetname,@failurecode,@jpnum,
@craft,@frequnit,@frequency,@jpduration,@actual_hours,@key,@new_key,@siteid,
@orgid,@version,@changedate,@change_user,@nextdate,@extdate,@rpct_f
end --end of WHILE
end --end of if
close cursor_mstr_extr
DEALLOCATE cursor_mstr_extr
end
GO
Post #1398200
rhythm.varshney
rhythm.varshney
Posted Wednesday, December 19, 2012 1:59 AM
SSC Journeyman
Group: General Forum Members
Last Login: Thursday, March 21, 2013 2:14 AM
Points: 77,
Visits: 184
Please refer below URL an article from Jeff Moden where he mentioned how to use Tally Table.
http://www.sqlservercentral.com/articles/T-SQL/62867/
Also just a suggestion don't use 'sp' as prefix for your user defined stored procs.
Post #1398237
anthony.green
anthony.green
Posted Wednesday, December 19, 2012 2:02 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
Welcome to SCC
Can you please follow the links in my signature to posting, code and data for the best help and also the how to post performance problems so that we can help you.
But at a quick qlance, get rid of the cursor if you can.
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1398239
Sean Lange
Sean Lange
Posted Wednesday, December 19, 2012 8:34 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620,
Visits: 8,261
anthony.green (12/19/2012)
Welcome to SCC
Can you please follow the links in my signature to posting, code and data for the best help and also the how to post performance problems so that we can help you.
But at a quick qlance, get rid of the cursor if you can.
I agree with reading that article but I think the change needs to a bit more radical. You need a complete rewrite of this process. You have a cursor with at least four while loops for each iteration through the cursor. I would bet that once you post some ddl, sample data and desired output we can help you turn this monstrosity into a single insert statement.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1398456
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Wednesday, December 19, 2012 9:04 AM
SSC-Dedicated
Group: Administrators
Last Login: Today @ 1:14 AM
Points: 31,433,
Visits: 13,746
I would tend to agree with Sean. Likely this could be rewritten to function much better and quicker.
Follow me on Twitter:
@way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
Post #1398481
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.