我有一个存储过程,它使用一些逻辑之间带回大块数据。我声明中的 PostFamilyTags 表有大约 150 万行。下面的 sql 语句运行很慢。
SELECT TOP(100)* FROM
(SELECT ROW_NUMBER()
OVER(ORDER BY p.date DESC) as NUM,
m.postfamilymediaID,
m.postfamilyID,
p.blogID,
p.userID,
p.BlogPostID,
m.postfamilymediatypeID as Type,
p.Title,
m.Address,
m.AddressEncoded,
m.ThumbNailAddress,
p.Date,
p.Summary,
p.Url,
m.ThumbNailIndex,
m.ThumbNailHeight,
m.ThumbNailWidth,
m.ThumbNailHeightAlt,
m.ThumbNailWidthAlt,
m.ItemName,
m.id3Title,
m.id3SubTitle,
m.id3ContributingArtists,
m.id3AlbumArtist,
m.id3Album,
m.id3Year,
m.id3Genre,
m.id3Length,
m.IsPublic
FROM
PostFamilyMedia as m
inner join
PostFamily as p on m.postfamilyID = p.postfamilyID
inner join
PostFamilyTags as pt on p.postfamilyID = pt.postfamilyID
inner join --Tags
Tags as t on pt.tagID = t.tagID
Where t.TagLevel = 1 and t.Tag = 'Electronic'
) AS a WHERE NUM >= (100 + 1) AND NUM <= (100 + 100)
但是当我去掉中间逻辑时,它的效果很好。
SELECT TOP(100)
m.postfamilymediaID,
m.postfamilyID,
p.blogID,
p.userID,
p.BlogPostID,
m.postfamilymediatypeID as Type,
p.Title,
m.Address,
m.AddressEncoded,
m.ThumbNailAddress,
p.Date,
p.Summary,
p.Url,
m.ThumbNailIndex,
m.ThumbNailHeight,
m.ThumbNailWidth,
m.ThumbNailHeightAlt,
m.ThumbNailWidthAlt,
m.ItemName,
m.id3Title,
m.id3SubTitle,
m.id3ContributingArtists,
m.id3AlbumArtist,
m.id3Album,
m.id3Year,
m.id3Genre,
m.id3Length,
m.IsPublic
FROM
PostFamilyMedia as m
inner join
PostFamily as p on m.postfamilyID = p.postfamilyID
inner join
PostFamilyTags as pt on p.postfamilyID = pt.postfamilyID
inner join --Tags
Tags as t on pt.tagID = t.tagID
Where t.TagLevel = 1 and t.Tag = 'Electronic'
谁能帮我让我的第一个 sql 语句运行得更快?