在30萬條記下表結構的表查詢要花9秒時間
Id int no 4
memberId int no 4
secType int no 4
secTitle varchar no 100
secTitleColor varchar no 20
secMap varchar no 50
AreaID varchar no 50
Roadid int no 4
District varchar no 50
realtyRate varchar no 50
isParking bit no 1
partRate varchar no 50
Floors int no 4
totalFloors int no 4
houseType int no 4
houseArea numeric no 9
useArea int no 4
Rooms int no 4
Halls int no 4
Kitchens int no 4
Wei int no 4
yangTai int no 4
ownerShip varchar no 50
Price numeric no 9
Unit int no 4
secDegree datetime no 8
Matching varchar no 100
Renovation int no 4
Address nvarchar no 300
Traffic nvarchar no 300
memberIp varchar no 30
Duration datetime no 8
LookTime varchar no 50
Tags varchar no 100
Remarks ntext no 16
Hits int no 4
Flag bit no 1
isRecom bit no 1
RecomDate datetime no 8
isTop bit no 1
TopDate datetime no 8
isAction bit no 1
createDate datetime no 8
updateTime datetime no 8
roomface varchar no 50
housepic varchar no 1000
status int no 4
totalprice numeric no 9
isJingPing bit no 1
--開始過程
CREATE PROC test2_sp_PageView
@tbname sysname, --要分頁顯示的表名
@FieldKey nvarchar(1000), --用於定位記錄的主鍵(惟一鍵)欄位,可以是逗號分隔的多個欄位
@PageCurrent int=1, --要顯示的頁碼
@PageSize int=10, --每頁的大小(記錄數)
@FieldShow nvarchar(1000)='', --以逗號分隔的要顯示的欄位列表,如果不指定,則顯示所有欄位
@FieldOrder nvarchar(1000)='', --以逗號分隔的排序欄位列表,可以指定在欄位後面指定DESC/ASC
@Where nvarchar(1000)='', --查詢條件
@PageCount int OUTPUT --總頁數
AS
SET NOCOUNT ON
--檢查對象是否有效
IF OBJECT_ID(@tbname) IS NULL
BEGIN
RAISERROR(N'對象"%s"不存在',1,16,@tbname)
RETURN
END
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s"不是表、視圖或者資料表值函式',1,16,@tbname)
RETURN
END
--分分頁欄位檢查
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N'分頁處理需要主鍵(或者惟一鍵)',1,16)
RETURN
END
--其他參數檢查及規範
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@Where,N'')=N''
SET @Where=N''
ELSE
SET @Where=N'WHERE ('+@Where+N')'
--如果@PageCount為NULL值,則計算總頁數(這樣設計可以只在第一次計算總頁數,以後調用時,把總頁數傳回給預存程序,避免再次計算總頁數,對於不想計算總頁數的處理而言,可以給@PageCount賦值)
IF @PageCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
+N' '+@Where
EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
END
--計算分頁顯示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=(@PageCurrent-1)*@PageSize
--第一頁直接顯示
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder)
ELSE
BEGIN
--處理別名
IF @FieldShow=N'*'
SET @FieldShow=N'a.*'
--產生主鍵(惟一鍵)處理條件
DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
@s nvarchar(1000),@Field sysname
SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
WHILE CHARINDEX(N',',@s)>0
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
@Where=REPLACE(@Where,@Field,N'a.'+@Field),
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
SELECT @Where=REPLACE(@Where,@s,N'a.'+@s),
@FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
@FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
@Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
@Where2=CASE
WHEN @Where='' THEN N'WHERE ('
ELSE @Where+N' AND ('
END+N'b.'+@s+N' IS NULL'+@Where2+N')'
--執行查詢
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' a LEFT JOIN(SELECT TOP '+@TopN1
+N' '+@FieldKey
+N' FROM '+@tbname
+N' a '+@Where
+N' '+@FieldOrder
+N')b ON '+@Where1
+N' '+@Where2
+N' '+@FieldOrder)
END