私は店のSQLを持っています
ALTER procedure [dbo].[TNNews_User_SearchBasic]
@Title nvarchar(400),
@CategoryId int,
@IsInterested int,
@IsHot int,
@IsTopCategory int,
@IsPublish int,
@PageSize int,
@PageIndex int,
@OrderBy varchar(20),
@PortalId int,
@LanguageId varchar(6)
as
DECLARE @EndTime DATETIME
DECLARE @StartTime DATETIME
SET @StartTime = GETDATE()
declare @tbCategory table(Id int)
DECLARE @StartRowIndex INT
IF @PageSize=0 SELECT @PageSize=count(*) FROM TNNews
IF(@PageIndex<0) SET @PageIndex=0
SET @StartRowIndex = @PageSize*(@PageIndex-1)+1
;WITH tmpCategory(Id, Name,ParentId,Level)
AS (
SELECT
e.Id,
e.Name,
e.ParentId,
1
FROM dbo.TNCategory AS e
WHERE
Id = @CategoryId or (@CategoryId='' and ParentId<=0)
UNION ALL
SELECT
e.Id,
e.Name,
e.ParentId,
Level + 1
FROM dbo.TNCategory AS e
JOIN tmpCategory AS d ON e.ParentId = d.Id
)
insert @tbCategory select Id from tmpCategory
;WITH tmpNews as
(
SELECT
a.Id,a.Title,a.Subject
,ROW_NUMBER() OVER (ORDER BY (Publisheddate) desc) as ThuTuBanGhi
FROM dbo.TNNews a
where 1 = 1
--and ( Title like '%'+@Title+'%')
and (@CategoryId = -1 or exists (select 0 from @tbCategory b where b.Id = a.CategoryId))
and (@IsInterested = -1 or IsIntrested = @IsInterested )
and (@IsHot = -1 or IsHot = @IsHot )
and (@IsTopCategory = -1 or IsTopCategory = @IsTopCategory )
and (@IsPublish = -1 or IsPublished = @IsPublish)
and PortalId=@PortalId
and LanguageId = @LanguageId
)
select *, (select COUNT(Id) from tmpNews) as 'TongSoBanGhi' from tmpNews
WHERE
ThuTuBanGhi BETWEEN (@StartRowIndex) AND (@StartRowIndex + @PageSize-1)
SET @EndTime = GETDATE()
PRINT 'StartTime = ' + CONVERT(VARCHAR(30),@StartTime,121)
PRINT ' EndTime = ' + CONVERT(VARCHAR(30),@EndTime,121)
PRINT ' Duration = ' + STR(DATEDIFF(MILLISECOND,@StartTime,@EndTime)) + ' millisecond'
select STR(DATEDIFF(MILLISECOND,@StartTime,@EndTime))
このストアの実行後
EXEC [dbo].[TNNews_User_SearchBasic]
@Title='',
@CategoryId = '',
@IsInterested = -1,
@IsHot = -1,
@IsTopCategory = -1,
@IsPublish = -1,
@PageSize = 20,
@PageIndex = 1,
@OrderBy = '',
@PortalId = 0,
@LanguageId = N'vi-VN'
go
実行時間は約「200ms」です。そして、いくつかの変更を加えて、新しいストア「TNNews_User_SearchBasic1」を作成します。
.....
--,ROW_NUMBER() OVER (ORDER BY (Publisheddate) desc) as ThuTuBanGhi
,ROW_NUMBER() OVER (ORDER BY (case when @OrderBy='VIEW_COUNT' then ViewCount else PublishedDate end) desc) as ThuTuBanGhi
.....
そして今、この店を実行する時間
EXEC [dbo].[TNNews_User_SearchBasic1]
@Title='',
@CategoryId = '',
@IsInterested = -1,
@IsHot = -1,
@IsTopCategory = -1,
@IsPublish = -1,
@PageSize = 20,
@PageIndex = 1,
@OrderBy = '',
@PortalId = 0,
@LanguageId = N'vi-VN'
GO
約900ms。なぜ変化があるのか分かりません。これらの店舗の改善にご協力ください。
PS: サンプル データベースをhttp://anhquan22.tk/Portals/0/Videos/web.rarに置きました。