sql-server - 基于不在主键中的列对 SQL Server 表进行分区?
问题描述
假设我有一张这样的桌子:
create table test_partitions
(
pk_id int not null,
col1 nvarchar(20),
col2 nvarchar(100),
constraint pk_test_partitions primary key (pk_id, col1)
);
我想对该表进行分区以提高查询性能,这样我就不必每次需要时都查看整个表。所以我添加了一个计算列:
create table test_partitions
(
pk_id int not null,
partition_id as pk_id % 10 persisted not null,
col1 nvarchar(20),
col2 nvarchar(100),
constraint pk_test_partitions primary key (pk_id, col1)
);
所以每当我这样做时,我都select * from test_partitions where pk_id = 123
希望 SQL Server 只查看整个表的 1/10。我不想将partition_id
列添加到主键,因为它永远不会成为 where 子句的一部分。如何对我的表进行分区partition_id
?
解决方案
为了提高查询性能,您可以应用分页。也就是说,您可以使用fetch next query 来实现您的输出。请通过此了解更多详细信息。
SELECT column-names FROM table-name
ORDER BY column-names
OFFSET n ROWS
FETCH NEXT m ROWS ONLY
推荐阅读
- authentication - 会话基础和 jwt 身份验证有什么区别?
- python - 将多个单列 excel 文件与特定的嵌套列表/元组进行比较
- highcharts - 如何在变量饼图中移动右上角的数据标签
- python - 为什么通过函数传递字典后不修改字典?
- solidity - 在 Solidity 库中复制 SafeMath
- javascript - 如何在 localStorage 中更改和存储购物车数量值
- javascript - 如何在 React 中获取单击动态添加的组件的索引并将其删除?
- django - '问题'对象没有属性'choice_set'django
- python - dask 数据框是否有任何有效的方法来按一列分组然后加入该列?
- algorithm - N/3 重复次数线性解