SQL 服务器中的分区或索引大 table
Partition or Index large table in SQL Server
我有一个很大的 table,由 40 亿多行和 50 列组成,其中大部分是 datetime
或 numeric
,少数 varchar
除外。
数据将每周插入 table(约 2000 万行)。
我希望在一些 datetime
列和一些 varchar
列上使用 where 子句进行查询。 table 中没有主键。
没有索引,table 也没有分区。我正在使用 SQL Server 2016。
我知道我需要对 table 进行分区或编制索引,但实际上我不确定采用哪种方法或两者都采用。
因为table很大,是先建索引还是先建分区?如果我确实创建了索引,然后创建了分区,我应该怎么做才能用每周进来的新数据来维护这些索引。
编辑:此外,预计 table
上的更新和删除最少
I understand that I need to partition or index the table
您需要了解分区的好处。 SQL 服务器根本不需要在大 table 上进行分区才能充分发挥作用。 SQL 服务器可扩展到任意 table 大小,没有任何固有问题。
分区的共同好处是:
- 恒定时间批量删除
- 旧分区的不同存储
- 不备份旧分区
有时在特殊情况下(例如列存储),分区可以作为一种加速查询的策略。通常,索引对此更好。
本质上,分区将 table 在物理上分成多个子 table。大多数情况下,这会对查询计划产生 负面影响 。索引完全能够限制需要接触的数据集。分区更糟糕。
Most of the queries will be filtering on the datetime columns and on some of the varchar columns. Like, get data for a certain daterange for a certain entity. With the indexes, it will be fragmented a lot because of new inserts and rebuilding/reorganising the indexes will also consume a lot of time. I can do it but again not sure which approach.
看来你最好通过索引来解决这个问题:
- 根据您期望的查询进行索引。
- 正确维护索引。这并不难。例如,在每周加载后重建它们。
Since the table is large, should I create the indexes first or should I create the partitions first?
首先设置分区对象。然后,在新的分区方案上创建或重建聚簇索引。如果可能,先删除其他索引,然后再重新创建它们(由于可用性限制可能无法工作)。
what should I do to maintain these with new data coming in weekly.
你有什么顾虑?新数据将自动存储在适当的分区中。确保在加载数据之前创建新分区。提前 2 周准备好分区。最新的分区必须始终为空以避免昂贵的拆分。
There is no primary key in the table.
通常这不是一个好的设计。大多数 table 应该有一个主键和一个聚集索引。如果没有自然键,请使用人工键,例如 bigint identity
.
您当然可以应用分区,但我的感觉是它不会给您带来您可能期望的效果。但它会迫使您承担额外的维护负担,可能会降低性能,并且存在犯下威胁可用性的错误的风险。简单很重要。
我有一个很大的 table,由 40 亿多行和 50 列组成,其中大部分是 datetime
或 numeric
,少数 varchar
除外。
数据将每周插入 table(约 2000 万行)。
我希望在一些 datetime
列和一些 varchar
列上使用 where 子句进行查询。 table 中没有主键。
没有索引,table 也没有分区。我正在使用 SQL Server 2016。
我知道我需要对 table 进行分区或编制索引,但实际上我不确定采用哪种方法或两者都采用。
因为table很大,是先建索引还是先建分区?如果我确实创建了索引,然后创建了分区,我应该怎么做才能用每周进来的新数据来维护这些索引。
编辑:此外,预计 table
上的更新和删除最少I understand that I need to partition or index the table
您需要了解分区的好处。 SQL 服务器根本不需要在大 table 上进行分区才能充分发挥作用。 SQL 服务器可扩展到任意 table 大小,没有任何固有问题。
分区的共同好处是:
- 恒定时间批量删除
- 旧分区的不同存储
- 不备份旧分区
有时在特殊情况下(例如列存储),分区可以作为一种加速查询的策略。通常,索引对此更好。
本质上,分区将 table 在物理上分成多个子 table。大多数情况下,这会对查询计划产生 负面影响 。索引完全能够限制需要接触的数据集。分区更糟糕。
Most of the queries will be filtering on the datetime columns and on some of the varchar columns. Like, get data for a certain daterange for a certain entity. With the indexes, it will be fragmented a lot because of new inserts and rebuilding/reorganising the indexes will also consume a lot of time. I can do it but again not sure which approach.
看来你最好通过索引来解决这个问题:
- 根据您期望的查询进行索引。
- 正确维护索引。这并不难。例如,在每周加载后重建它们。
Since the table is large, should I create the indexes first or should I create the partitions first?
首先设置分区对象。然后,在新的分区方案上创建或重建聚簇索引。如果可能,先删除其他索引,然后再重新创建它们(由于可用性限制可能无法工作)。
what should I do to maintain these with new data coming in weekly.
你有什么顾虑?新数据将自动存储在适当的分区中。确保在加载数据之前创建新分区。提前 2 周准备好分区。最新的分区必须始终为空以避免昂贵的拆分。
There is no primary key in the table.
通常这不是一个好的设计。大多数 table 应该有一个主键和一个聚集索引。如果没有自然键,请使用人工键,例如 bigint identity
.
您当然可以应用分区,但我的感觉是它不会给您带来您可能期望的效果。但它会迫使您承担额外的维护负担,可能会降低性能,并且存在犯下威胁可用性的错误的风险。简单很重要。