SQL Server - Reduce number of Reads [closed]
I have one general question about optimization at SQL Server: How I can reduce number of Reads in stored procedure?
I'm interested in good practices like: - creating indexes at physical and temp tables - using temp tables instead using same table couple times in procedure - DDL before DML - SET NOCOUNT ON at the beginning of stored procedure - ...
We have issue with disk space that is used because of huge number of reads that is caused by couple stored procedures and I need to optimize it.
Part of stored procedure that is 'the most expensive' is:
create table #stavke
(
Id_Br int identity(1, 1), IDStavke int,
HeaderID int, Currency varchar(3), GLAcct varchar(20), id varchar(20), Trnuid varchar(60),
ReferenceID varchar(20), DocumentID varchar(20),
DtAvail varchar(10), DtBooking varchar(10), DatePosted varchar(10),
Amount money, AmountLcl money,
Description varchar(250), Type varchar(10), DP int )
insert into #stavke
(
IDStavke, HeaderID, GLAcct, Currency, id , Trnuid ,
ReferenceID, DocumentID ,
DtAvail , DtBooking , DatePosted,
Amount , AmountLcl ,
Description , Type , DP
)
SELECT S.ID as IDStavke,
z.RB as HeaderID,
z.KONTO AS GLAcct,
z.OZNVAL AS Currency,
Si.BROJNALOGA as ID,
D.TRN as Trnuid,
case substring(SI.BROJNALOGA,1,4)
when '0746' then O.REFERENCA
when '1450' then D.REFERENCA
when '0743' then L.REFERENCA
when '2021' then N.REFERENCA
end ReferenceID,
case substring(SI.BROJNALOGA,1,4)
when '3000' then 'Kursna razlika'
when '2200' then 'PP-'+SI.BROJNALOGA
when '2201' then 'KDP-'+SI.BROJNALOGA
else SI.BROJNALOGA
end DocumentID,
dvalute as DtAvail,
si.dknizenja as DtBooking,
'' as DatePosted,
case si.teret
when 0 then si.korist
else si.teret
end Amount,
case SI.DINTERET
when 0 then si.dinkorist
else si.dinteret
end AmountLcl,
'' as Description,
case substring(SI.BROJNALOGA,1,4)
when '0746' then '0746'
when '1450' then '1450'
when '0743' then '0743'
when '2021' then 'Ostalo'
end Type,
case SI.DINTERET
when 0 then 1
else -1
end DP
FROM A I
inner join B st on i.transfer=st.transfer and i.partija=st.partija
INNER JOIN C SI ON st.RB=Si.RB
inner join D z on z.rb=st.rb
inner join E s on z.rb=s.rb AND s.BROJNALOGA = si.BROJNALOGA
LEFT JOIN F D ON D.BROJ=SI.BROJNALOGA
LEFT JOIN G L ON L.BROJ=SI.BROJNALOGA
LEFT JOIN H O ON O.BROJ=SI.BROJNALOGA
LEFT JOIN I N ON N.BROJ=SI.BROJNALOGA
WHERE I.novi_izvod=convert(int,@StatementNumber) AND i.PARTIJA=@Account
ORDER BY I.PARTIJA,z.RB,SI.id, z.KONTO,z.OZNVAL, SI.DKNIZENJA
Tables B, G, H and I (I changed real names of tables for this example to make it easier for reading) are very big, i.e. have many columns and big amount of data.
I hope you are talking of minimizing the disk activity by a procedure.
firstly, you can benchmark your current IO activity using the
set statistics IO on;
With that info at hand and getting the Execution Plan with SET SHOWPLAN_ALL or XML or you can make use of ssms to get the same ergonomically. You can make use of DTA for basic tuning.
Try executing the SP as a bunch of ad-hoc statements and see where the IO is heavy and concentrate on that segment. There are a lot of good practices which might suit your requirement.
精彩评论