sql server 如何高效读取表中xxx至yyy之间的记录?(没分了,请大家帮帮忙吧)
我目前采用的方法是select * from table1,记录集返回后,
if(!pRecordset->adoEOF)
{
pRecordset->MoveFirst();
pRecordset->Move((long)xxx,vtMissing);
}
int nCount=0;
while(!pRecordset->adoEOF)
{
读取记录的各个字段;
nCount++;
if(nCount==(yyy-xxx))
break;
pRecordset->MoveNext();
}
但这种方法效率很低,请大家多多指教。
SQL?Server 效率
[解决办法]
使用row_number,请看
http://social.msdn.microsoft.com/Forums/zh-CN/55d1917f-42cb-4e8a-995f-ba66b34df219/tsql
[解决办法]
SELECT * FROM (select (row_number() over (order by id)) as RowNumber ,* FROM table1) AS t WHERE t.RowNumber >=xxx AND t.RowNumber <yyy
--有聚集索引的话
SELECT *
FROM
(
SELECT rowid = ROW_NUMBER() OVER(ORDER BY id), *
FROM dbo.tablename
) T
WHERE rowid BETWEEN xxx AND yyy
--堆的话
SELECT *
FROM
(
SELECT rowid = ROW_NUMBER() OVER(ORDER BY (SELECT 1)), *
FROM dbo.tablename
) T
WHERE rowid BETWEEN xxx AND yyy