SQL联表分页读取效率
/// <summary>
/// 读取标签列表(多表:Plus、Users)
/// </summary>
///
public DataSet GetList_TagUnion(string strWhere,int PageSize,int Page)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT top " + PageSize + " Plus.*,Users.* FROM Plus INNER JOIN Users ON Plus.Users_ID =Users.Users_ID ");
strSql.Append("and " + strWhere);
strSql.Append("where Plus.Plus_ID not in (select top (" + (Page - 1) * PageSize + ") Plus.Plus_ID FROM Plus INNER JOIN Users ON Plus.Users_ID =Users.Users_ID and " + strWhere + " order by Plus.Plus_ID desc) ");
strSql.Append(" order by Plus.Plus_ID desc");
return Common.DbHelperSQL.Query(strSql.ToString());
}
declare @page_size int;
declare @page_num int;
--比如:每页10条记录
set @page_size = 10;
--比如:先取第1页
set @page_num = 1;
select *
from
(
select *,
--这里按照@@servername来排序,
--你可以根据需要按照id,sid,sname等字段来排序
(row_number() over(order by @@servername) - 1) / @page_size as pagenum
from sys.objects
)t
where pagenum = @page_num - 1