改善以下存储过程的执行时间 [英] improve execution time of the following stored procedure
问题描述
CREATE PROCEDURE [dbo].[SP_RPT_DefectList]
(
@WhereClause NVARCHAR(2000) = NULL
)
AS
BEGIN
DECLARE @SelectStatement NVARCHAR(2000)
DECLARE @FullStatement NVARCHAR(4000)
SET @SelectStatement = ''SELECT DefectListId,
CONVERT(Varchar(12),DateOfEntry,101)DateOfEntry,
U.UserName as EnteredBy,
dbo.getDefectlistfromName(RefBy) RefBy,
dbo.GuiId(EquipmentGuid)EquipmentGuid,
DefectDescription,
SparesStoresRequiredRef,
ShoreAssistanceRequired,
DockingRequired,
Remarks,
Status,
CONVERT(Varchar(12),TargetDate,101)TargetDate
FROM dbo.PMS_TBL_DEFECT_LIST D
left outer join dbo.PMS_TBL_USERS U
on U.UserId=D.EnteredBy
LEFT OUTER JOIN dbo.PMS_TBL_SUB_EQUIPMENT_MASTER S
ON S.UniqueId=D.EquipmentGuid
LEFT OUTER JOIN dbo.PMS_TBL_EQUIPMENT_MASTER E
ON E.UniqueId=D.EquipmentGuid OR E.EquipmentId=S.EquipmentId
LEFT OUTER JOIN dbo.PMS_TBL_SUB_FUNCTIONAL_BLOCK_MASTER SF
ON SF.UniqueId=D.EquipmentGuid
Or SF.SubFunctionalBlockId=E.SFBId
LEFT OUTER JOIN dbo.PMS_TBL_FUNCTIONAL_BLOCK_MASTER F
ON F.FunctionalBlockId=SF.FBId ''
SET @FullStatement = @SelectStatement + ISNULL(@WhereClause,'''')
PRINT @FullStatement
EXECUTE sp_executesql @FullStatement
END
任何人都建议我改进上述存储过程的执行时间.....
Can any one suggest me to improve the execution time of the above stored procedure.....
推荐答案
首先确保你有适当的索引在表格上 - 见上一篇文章以下是我的存储过程可以任何人建议我改进执行时间 [^ ]
下一步 - 你为什么要在日期进行转换
Firstly make sure you have appropriate indexes on the tables - see your previous post the following is my stored procedure can any one suggest me the improvement of execution time[^]
Next - why are you doing the conversions on dates
CONVERT(Varchar(12),DateOfEntry,101)DateOfEntry
...
CONVERT(Varchar(12),TargetDate,101)TargetDate
不要在数据检索中进行转换 - 只在演示文稿中进行转换呃。
您加入了很多表并在ON子句中使用OR - 有什么办法可以减少输入这些表的记录数量加入 - 例如使用CTE获取带有WHERE子句的关键记录,然后加入以获取其他信息。
您还调用两个函数
Don't do the conversion in the data retrieval - only do that in the presentation layer.
You're joining across a lot of tables and using OR in the ON clauses - is there any way you can reduce the number of records that are being fed into these joins - e.g. use a CTE to get the key records with the WHERE clause then join to get the additional information.
You're also calling two functions
dbo.getDefectlistfromName(RefBy) RefBy,
dbo.GuiId(EquipmentGuid)EquipmentGuid,
它们是否有效(您还没有共享该代码)?他们是否真的需要在那里或者可以在结果出现之前立即使用
Are they efficient (you haven't shared that code)? Do they actually need to be there or can they be used immediately prior to presentation of the results
这篇关于改善以下存储过程的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!