Insus. NET in some time ago, once shared an article previous and next navigation http://www.bkjia.com/kf/201203/123721.html is to use the user control implementation, and pull into the article display page. In this way, it is shown that the most basic information of an article needs to be read from the database three times, one is to get the article information, and the other two are used in the user control, is to obtain the title and ID of the previous and next articles. To improve this performance, this article is generated:
Remove the user control and obtain the ID and title of the previous article together with the ID and title of the next article directly when reading the article. This acquisition saves 60% of the energy.
Refer to the stored procedure for getting an article:
Usp_Article_GetByPrimaryKey
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ===================================================== ======
-- Author: Insus. NET
-- Create date: 2012-03-05
-- Update date: 2012-03-17; 2012-03-23
-- Description: Get an article by article primary key.
-- ===================================================== ======
Create procedure [dbo]. [usp_Article_GetByPrimaryKey]
(
@ ArticleId INT
)
AS
WITH a
(
SELECT [ArticleId], [ArticleTypeId], [ArticleTypeName], [Subject], [PrevArticleId], -- previous ArticleId
[NextArticleId] -- next ArticleId
FROM [dbo]. [udf_Article] () -- table function
WHERE [ArticleId] = @ ArticleId
)
SELECT a. [ArticleId], a. [ArticleTypeId], [ArticleTypeName], a. [Subject],
[PrevArticleId], a1. [Subject] AS [PrevSubject], which lists the titles of the previous article.
[NextArticleId], a2. [Subject] AS [NextSubject] -- list the title of the next article
FROM
Left join [dbo]. [Article] AS a1 ON (a. [PrevArticleId] = a1. [ArticleId])
Left join [dbo]. [Article] AS a2 ON (a. [NextArticleId] = a2. [ArticleId]);
Copy code
In the code above, there is a table function:
Udf_Article
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ===================================================== ======
-- Author: Insus. NET
-- Create date: 2012-03-05
-- Update date: 2012-03-17
-- Description: Article details.
-- ===================================================== ======
Create function [dbo]. [udf_Article]
()
RETURNS TABLE
AS
RETURN
(
SELECT [ArticleId], at. [ArticleTypeId], [ArticleTypeName], [Subject],
(Select max ([ArticleId]) FROM [dbo]. [Article] AS a1 WHERE a1. [ArticleId] <a. [ArticleId]) AS [PrevArticleId],
(Select min ([ArticleId]) FROM [dbo]. [Article] AS a1 WHERE a1. [ArticleId]> a. [ArticleId]) AS [NextArticleId]
FROM [dbo]. [Article] AS
Left join [dbo]. [ArticleType] AS at ON (a. [ArticleTypeId] = at. [ArticleTypeId])
)
The ArticleView. aspx and Repeater controls have OnItemDataBound events. :
View Code
<Asp: Repeater ID = "RepeaterArticleView" runat = "server" OnItemDataBound = "RepeaterArticleView_ItemDataBound">
<ItemTemplate>
<! -- Display other information fields -->
Previous Article: <asp: HyperLink ID = "HyperLinkPrev" runat = "server" Target = "_ blank"> </asp: HyperLink>
<Br/>
Next article: <asp: HyperLink ID = "HyperLinkNext" runat = "server" Target = "_ blank"> </asp: HyperLink>
</ItemTemplate>
</Asp: Repeater>
ArticleView. aspx. vb:
View Code
Protected Sub RepeaterArticleView_ItemDataBound (sender As Object, e As RepeaterItemEventArgs)
Dim drv As DataRowView = DirectCast (e. Item. DataItem, DataRowView)
If e. Item. ItemType = ListItemType. Item OrElse e. Item. ItemType = ListItemType. AlternatingItem Then
If e. Item. FindControl ("HyperLinkPrev") IsNot Nothing Then
Dim prevLink As HyperLink = DirectCast (e. Item. FindControl ("HyperLinkPrev"), HyperLink)
If String. IsNullOrEmpty (drv ("PrevArticleId"). ToString () Then
PrevLink. Text = "NONE"
Else
PrevLink. Text = drv ("PrevSubject ")
PrevLink. NavigateUrl = "~ /ArticleView. aspx? ID = "& drv (" PrevArticleId ")
End If
End If
If e. Item. FindControl ("HyperLinkNext") IsNot Nothing Then
Dim nextLink As HyperLink = DirectCast (e. Item. FindControl ("HyperLinkNext"), HyperLink)
If String. IsNullOrEmpty (drv ("NextArticleId"). ToString () Then
NextLink. Text = "NONE"
Else
NextLink. Text = drv ("NextSubject ")
NextLink. NavigateUrl = "~ /ArticleView. aspx? ID = "& drv (" NextArticleId ")
End If
End If
End If
End Sub
From Insus. NET