SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help on this SP Urgent


Need help on this SP Urgent

Author
Message
Sana4u
Sana4u
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 68
I have been told to optimize this stored procedure w00t . Please view the attached database schema.

Please let me know if you have seen any thing wrong with this stored procedure.


Your URGENT HELP IS HIGHLY appreciated.




/****** Object: StoredProcedure [dbo].[spProfitNLossWithPreviousMonthYear] Script Date: 06/26/2014 21:44:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[spProfitNLossWithPreviousMonthYear]
@StartDate datetime = null,
@EndDate datetime = null,
@IsClosingEntry tinyint = 0
AS
BEGIN
declare @MonthStartDate as datetime
declare @MonthEndDate as datetime
declare @YearStartDate as datetime
declare @YearEndDate as datetime

declare @PreviousYearMonthStartDate as datetime
declare @PreviousYearMonthEndDate as datetime
declare @PreviousYearStartDate as datetime
declare @PreviousYearEndDate as datetime
if isdate(@StartDate) = 0
begin
set @YearStartDate = '1900-01-01 00:00:00.000'
set @PreviousYearStartDate = DATEADD(yy, -1, '1900-01-01 00:00:00.000')
end
else
begin
set @YearStartDate = @StartDate
set @PreviousYearStartDate = DATEADD(yy, -1, @StartDate)
end
if isdate(@EndDate) = 0
begin
set @YearEndDate = getdate()
set @PreviousYearEndDate = DATEADD(yy, -1, getdate())
end
else
begin
set @YearEndDate = @EndDate
set @PreviousYearEndDate = DATEADD(yy, -1, @EndDate)
end

----First Day of Month
set @MonthStartDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@YearEndDate),0)) + 1
----Last Day of Month
set @MonthEndDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@YearEndDate)+1,0))

----First Day of Month in Previous Year
set @PreviousYearMonthStartDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@PreviousYearEndDate),0)) + 1
----Last Day of Month in Previous Year
set @PreviousYearMonthEndDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@PreviousYearEndDate)+1,0))

--print (cast(@MonthStartDate as varchar))
--print (cast(@MonthEndDate as varchar))
--print (cast(@PreviousYearMonthStartDate as varchar))
--print (cast(@PreviousYearMonthEndDate as varchar))

--If the user selects “Select Fiscal Date” from the date selector combobox, then the calculation for YearBalance
--should include all transactions (even Closing Entry and Closing Entry Revision transactions)
if @IsClosingEntry = 1
begin
select GLAccountsID, GLAccountTypeID,GLAccountTypesDescription,GLNumber,GLAccountsDescription, AccountLevel, isnull(ParentGLAccountID,0) ParentGLAccountID,
vwGLAccountsWithLevels.EnumKey GLAccountTypesEnumKey,
--isnull(MonthBalance,0.00) MonthBalance, isnull(YearBalance,0.00) YearBalance,
--isnull(PreviousYearMonthBalance,0.00) PreviousYearMonthBalance, isnull(PreviousYearBalance,0.00) PreviousYearBalance
MonthBalance, YearBalance, PreviousYearMonthBalance, PreviousYearBalance
from vwGLAccountsWithLevels

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) YearBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@YearStartDate,110) as datetime) and cast(convert(varchar(10),@YearEndDate,110) as datetime)
group by GLAccountID) YearlyBalance
on vwGLAccountsWithLevels.GLAccountsID = YearlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) MonthBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@MonthStartDate,110) as datetime) and cast(convert(varchar(10),@MonthEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) MonthlyBalance
on vwGLAccountsWithLevels.GLAccountsID = MonthlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) PreviousYearBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@PreviousYearStartDate,110) as datetime) and cast(convert(varchar(10),@PreviousYearEndDate,110) as datetime)
group by GLAccountID) PreviousYearlyBalance
on vwGLAccountsWithLevels.GLAccountsID = PreviousYearlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) PreviousYearMonthBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@PreviousYearMonthStartDate,110) as datetime) and cast(convert(varchar(10),@PreviousYearMonthEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) PreviousYearMonthlyBalance
on vwGLAccountsWithLevels.GLAccountsID = PreviousYearMonthlyBalance.GLAccountID and IsProfitLoss = 1

--where (MonthBalance is not null OR YearBalance is not null OR PreviousYearBalance is not null OR PreviousYearMonthBalance is not null)
where GLAccountTypesDescription in ('Income','Cost of Sales','Selling & Operating Expense','General & Admin. Expense','Other Income','Other Expense')
order by GLAccountTypeID,cast(GLNumber as varchar)
end
else
begin
select GLAccountsID, GLAccountTypeID,GLAccountTypesDescription,GLNumber,GLAccountsDescription, AccountLevel, isnull(ParentGLAccountID,0) ParentGLAccountID,
vwGLAccountsWithLevels.EnumKey GLAccountTypesEnumKey,
--isnull(MonthBalance,0.00) MonthBalance, isnull(YearBalance,0.00) YearBalance,
--isnull(PreviousYearMonthBalance,0.00) PreviousYearMonthBalance, isnull(PreviousYearBalance,0.00) PreviousYearBalance
MonthBalance, YearBalance, PreviousYearMonthBalance, PreviousYearBalance
from vwGLAccountsWithLevels

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) YearBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@YearStartDate,110) as datetime) and cast(convert(varchar(10),@YearEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) YearlyBalance
on vwGLAccountsWithLevels.GLAccountsID = YearlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) MonthBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@MonthStartDate,110) as datetime) and cast(convert(varchar(10),@YearEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) MonthlyBalance
on vwGLAccountsWithLevels.GLAccountsID = MonthlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) PreviousYearBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@PreviousYearStartDate,110) as datetime) and cast(convert(varchar(10),@PreviousYearEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) PreviousYearlyBalance
on vwGLAccountsWithLevels.GLAccountsID = PreviousYearlyBalance.GLAccountID and IsProfitLoss = 1

left outer join
(select GLAccountID, sum(DebitAmount - CreditAmount) PreviousYearMonthBalance from vwTransactionDetails
where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@PreviousYearMonthStartDate,110) as datetime) and cast(convert(varchar(10),@PreviousYearMonthEndDate,110) as datetime)
and EnumKey != 'ClosingEntry' and EnumKey != 'ClosingEntryRevision'
group by GLAccountID) PreviousYearMonthlyBalance
on vwGLAccountsWithLevels.GLAccountsID = PreviousYearMonthlyBalance.GLAccountID and IsProfitLoss = 1

--where (MonthBalance is not null OR YearBalance is not null OR PreviousYearBalance is not null OR PreviousYearMonthBalance is not null)
where GLAccountTypesDescription in ('Income','Cost of Sales','Selling & Operating Expense','General & Admin. Expense','Other Income','Other Expense')
order by GLAccountTypeID,cast(GLNumber as varchar)
end
END
Attachments
DB with Schema.zip (10 views, 39.00 KB)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)

Group: General Forum Members
Points: 219263 Visits: 46279
Plenty wrong. For starters, this

where cast(convert(varchar(10),PostingDate,110) as datetime) between cast(convert(varchar(10),@YearStartDate,110) as datetime) and cast(convert(varchar(10),@YearEndDate,110) as datetime)

Change that so that you have no functions of any form on the columns. Hint, just drop the casts and converts.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61189 Visits: 17954
To add to what Gail already pointed out:

Multiple execution paths.
Repeatedly hitting the same view with nonSARGable predicates (EnumKey != )

Tuning this is far more than an online forum is capable of. First of all your sql script is almost 10k lines of t-sql. To truly optimize is several days. I am not willing to put that much effort into a forum post. My recommendation would be to find somebody local who is good at optimizing procedures and hire them as a consultant.

_______________________________________________________________

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 Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sana4u
Sana4u
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 68
HI-Please let me know what do you suggest how I Can change that I have to change the datetime in varchar
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search