千万数据多表查询分页
我是一个学生,现在我要做一个千万数据多表查询的习题。
想了好久都没有想出什么好的解决方案。
哪位大哥能帮助下,大家互相学习。
表1[Table1]
字段:
[id] [int] IDENTITY(1,1) NOT NULL,
[k] [varchar](50) NOT NULL,
[d] [datetime] NOT NULL
表2[Table2]
字段:
[id] [int] IDENTITY(1,1) NOT NULL,
[t3_Id] [int] NOT NULL,
[t1_Id] [int] NOT NULL,
[date] [datetime] NOT NULL
表3[Table3]
[id] [int] IDENTITY(1,1) NOT NULL,
[title] [varchar](50) NOT NULL,
[conten] [varchar](50) NOT NULL,
[date] [datetime] NOT NULL
表1存放的是关键字,表3存放的是文章,表2存放的是文章中包含的关键字
根据关键字查询
declare @t1_Id int;
Select @t1_Id = id from Table1 where k = 'test1'
Select t3.title,t3.conten from Table2 t2,Table3 t3 where t2.t1_Id = @kt_Id and t3.id = t2.t3_Id
/*通用分页存储过程*/
USE HotelManagementSystem
GO
IF EXISTS(SELECT * FROM sys.objects WHERE NAME='cndoup_GetPageOfRecords')
DROP PROCEDURE cndoup_GetPageOfRecords
GO
--创建存储过程
CREATE PROCEDURE cndoup_GetPageOfRecords
@pageSize int = 20, --分页大小
@currentPage int , --第几页
@columns varchar(1000) = '*', --需要得到的字段
@tableName varchar(100), --需要查询的表
@condition varchar(1000) = '', --查询条件, 不用加where关键字
@ascColumn varchar(100) = '', --排序的字段名 (即 order by column asc/desc)
@bitOrderType bit = 0, --排序的类型 (0为升序,1为降序)
@pkColumn varchar(50) = '' --主键名称
AS
BEGIN --存储过程开始
DECLARE @strTemp varchar(300)
DECLARE @strSql varchar(5000) --该存储过程最后执行的语句
DECLARE @strOrderType varchar(1000) --排序类型语句 (order by column asc或者order by column desc)
BEGIN
IF @bitOrderType = 1 --降序
BEGIN
SET @strOrderType = ' ORDER BY '+@ascColumn+' DESC'
SET @strTemp = '<(SELECT min'
END
ELSE --升序
BEGIN
SET @strOrderType = ' ORDER BY '+@ascColumn+' ASC'
SET @strTemp = '>(SELECT max'
END
IF @currentPage = 1 --第一页
BEGIN
IF @condition != ''
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@condition+@strOrderType
ELSE
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+@strOrderType
END
ELSE -- 其他页
BEGIN
IF @condition !=''
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@condition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+
' '+@pkColumn+' FROM '+@tableName'where'+@condition+@strOrderType+') AS TabTemp)'+@strOrderType
ELSE
SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@columns+' FROM '+@tableName+
' WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currentPage-1)*@pageSize)+' '+@pkColumn+
' FROM '+@tableName+@strOrderType+') AS TabTemp)'+@strOrderType
END
END
EXEC (@strSql)
END