取最新一条SQL优化
一开始给同事的sql是第二条,但关联查询执行起来慢,同事修改了一些第一条效率明显提升!
最终优化SQL
CREATE VIEW [dbo].[View_Item_IsLarge]
AS
SELECT ItemID, DLMasterChildType
FROM dbo.Item_IsLarge AS A
WHERE A.Gid IN ( SELECT TOP ( 1 )
Gid
FROM dbo.Item_IsLarge
WHERE ( ItemID = A.ItemID )
ORDER BY rowModifyDate DESC )
GO
AS
SELECT ItemID, DLMasterChildType
FROM dbo.Item_IsLarge AS A
WHERE A.Gid IN ( SELECT TOP ( 1 )
Gid
FROM dbo.Item_IsLarge
WHERE ( ItemID = A.ItemID )
ORDER BY rowModifyDate DESC )
GO
一开始优化的SQL
create view [dbo].[View_Item_IsLarge2]
as
select DLMasterChildType,ItemID from (
select DLMasterChildType,ItemID,ROW_NUMBER() over(partition by ItemID order by rowModifyDate desc )rn
from dbo.Item_IsLarge ) t
where rn=1
GO
as
select DLMasterChildType,ItemID from (
select DLMasterChildType,ItemID,ROW_NUMBER() over(partition by ItemID order by rowModifyDate desc )rn
from dbo.Item_IsLarge ) t
where rn=1
GO