如何在没有任何渐进字段的情况下对 table 进行分区?
how to partition a table without any progressive fields?
我需要对 table 进行分区以获取值范围内的记录。
我在table中没有渐进字段。
select [_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
where ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C]) between 500 and 1000;
编写这段代码时,它 returns 我出现了以下错误:
Windowed functions can only appear in the SELECT or ORDER BY clauses.
问题是 ROW_NUMBER()
语句。
我可以这样写查询,但问题是我必须尽可能地限制对 table 的访问,因为它真的很慢。
select *
from (
select ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id]
,[_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
) t
where t.id between 500 and 1000;
谁能给我一个达到相同目标的替代方案?
试试这个:
;with MyTable as
(
select
ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id]
,[_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
)
select *
from MyTable
where id between 500 and 1000
;
我找到了解决方案。
select ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id], [_Load_Timestamp], [COL_A], [COL_B], [COL_C]
into #tmp_key
from dbo.table
create index IX_01 on #tmp_key on ([id])
select _b.*
from #tmp_key _a
join dbo.table _b on _a.id between 500 and 1000
and _a.[_Load_Timestamp] = _b.[_Load_Timestamp]
and _a.[COL_A] = _b.[COL_A]
and _a...
数字 500 和 1000 将根据为我填充 table 的循环动态设置。
无论如何,我的问题都解决了。
我需要对 table 进行分区以获取值范围内的记录。
我在table中没有渐进字段。
select [_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
where ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C]) between 500 and 1000;
编写这段代码时,它 returns 我出现了以下错误:
Windowed functions can only appear in the SELECT or ORDER BY clauses.
问题是 ROW_NUMBER()
语句。
我可以这样写查询,但问题是我必须尽可能地限制对 table 的访问,因为它真的很慢。
select *
from (
select ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id]
,[_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
) t
where t.id between 500 and 1000;
谁能给我一个达到相同目标的替代方案?
试试这个:
;with MyTable as
(
select
ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id]
,[_Load_Timestamp], [COL_A], [COL_B], [COL_C], [COL_D], [COL_E], [COL_F]
from dbo.table
)
select *
from MyTable
where id between 500 and 1000
;
我找到了解决方案。
select ROW_NUMBER() OVER (order by [_Load_Timestamp], [COL_A], [COL_B], [COL_C])) [id], [_Load_Timestamp], [COL_A], [COL_B], [COL_C]
into #tmp_key
from dbo.table
create index IX_01 on #tmp_key on ([id])
select _b.*
from #tmp_key _a
join dbo.table _b on _a.id between 500 and 1000
and _a.[_Load_Timestamp] = _b.[_Load_Timestamp]
and _a.[COL_A] = _b.[COL_A]
and _a...
数字 500 和 1000 将根据为我填充 table 的循环动态设置。
无论如何,我的问题都解决了。