Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Preventing
SP Time Out
Delay
Deadlocking
More DiskReads
By: Nix
Parameter
Sniffing
- This option causes the compilation process to ignore the value for the specified variable and use the specified value instead.
- Unlike the other workaround (Option(Recompile)), this option avoid the overhead of recompiling the query every time.
(
The workaround is to declare the input params@in_SetupId int
again with different variable name and use the)
As
new variable through out the SP.
begin
Declare @local_SetupId int =@in_SetupId
In the Example @local_SetupId
Select UserId from UserDetail where SetupId=@local_SetupI
end
Use NON
Clustered Index
Once you have created a temp table, set
index on the primary key of the table on
which you will be performing the join
operation.
In the example we have read the Product
Hierarchy
table into a temp table and we have
created a non-clustered
index onto the primary key i.e ProductId
Join On the
InputParameter
Instead Of
SELECT * from trans.Promotion P
inner join masters.Product PP with(NOLOCK) on
PP.SetupId=P.SetupId
inner join masters.Customer CC with(NOLOCK) on
CC.SetupId=P.SetupId
Use EXISTS
Use Local
Prevent calling the string functions or date
Variables to Store functions
Over and over again, instead store em in local
the FunctionCall variables
if you are going to reuse the value.
return
Declare @sampleString varchar(max) = 'nevermind the
promotions'
Declare @sizeOfString int
set @sizeOfString= len(@sampleString)
SELECT @sizeOfString
BEGIN TRY
-- Logic / Query here
END TRY
BEGIN CATCH
----------------------------------------------------------------------DECLARE
@ErrMsg VARCHAR(255) -- Error Message
,@ErrNo INT -- Error Number
,@ErrSeverity INT -- Error Severity
,@ErrProc VARCHAR(255) -- Error Procedure
,@ErrLine INT -- Error Line
SELECT
@ErrMsg = ERROR_MESSAGE()
,@ErrNo = ERROR_NUMBER()
,@ErrSeverity = 17
,@ErrProc = ERROR_PROCEDURE()
,@ErrLine = ERROR_LINE()
RAISERROR (
@ErrMsg
,@ErrSeverity
,1
,@ErrNo
,@ErrLine
,@ErrProc
)
----------------------------------------------------------------------END CATCH
END
SET NOCOUNT ON
Select PromotionId from Promotion
Prevent Usage of
DDL Statements
Do not try to use DDL statements inside a stored procedure that will
reduces the chance to reuse the execution plan.
DDL statements like CREATE,ALTER,DROP,TRUNCATE etc.
Use Alias
--Wrong Statement
SELECT PromotionId , P.VersionedPromotionId, Name, PIE.InvestmentTypeId from
Promotion P
Inner join PromotionInvestment PIE on
PIE.VersionedPromotionId=P.VersionedPromotionId
where P.Name='Blah'
--Correct Statement
SELECT P.PromotionId , P.VersionedPromotionId, P.Name, PIE.InvestmentTypeId
from Promotion P
Inner join PromotionInvestment PIE on
PIE.VersionedPromotionId=P.VersionedPromotionId
where P.Name='Blah'
Don't
useUPDATEinstead
ofCASE
Take this scenario, for instance: You're inserting data into a temp table and
need it to display a certain value if another value exists. Maybe you're pulling
from the Customer table and you want anyone with more than $100,000 in
orders to be labeled as "Preferred." Thus, you insert the data into the table
and run an UPDATE statement to set the CustomerRank column to
"Preferred" for anyone who has more than $100,000 in orders. The problem is
that the UPDATE statement is logged, which means it has to write twice for
every single write to the table. The way around this, of course, is to use an
inline CASE statement in the SQL query itself. This tests every row for the
order amount condition and sets the "Preferred" label before it's written to
the table.
select *
from Promotion
where YEAR(StartDate) = 2015
and MONTH(StartDate) = 6
Select *
From Promotion
Where StartDate between '6/1/2015'
and '6/30/2015'
WITH ( Index(IdxPromotionId))
WHERE Name = 'blah'
and Setupid=1099