暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

国产GPT对比

原创 行之 2024-06-19
257

仅从一个答复就说一个大模型的好坏毕竟是片面的,此文权当图个乐子。
因为GPT答复每次都会有所不同,这里只取第一次的答复。

问题:

分区表如何创建索引,来保证数据的唯一性?

一、智谱清言

https://chatglm.cn/main/alltoolsdetail

在数据库管理系统中,分区表是一种将大表分成多个小块的技术,每个小块称为一个分区。创建索引是数据库优化的一种手段,可以加快查询速度。为了保证数据的唯一性,通常会在表上创建唯一性索引。
以下是创建分区表并保证数据唯一性的步骤:

  1. 创建分区表
    在创建分区表时,需要指定分区策略,如范围分区、列表分区、哈希分区等。假设我们有一个用户表,按照用户ID的范围来分区。
CREATE TABLE users ( user_id INT, username VARCHAR(50), email VARCHAR(100), ... ) PARTITION BY RANGE (user_id) ( PARTITION p0 VALUES LESS THAN (10000), PARTITION p1 VALUES LESS THAN (20000), ... );
  1. 创建唯一性索引
    为了保证数据唯一性,可以在表上创建唯一性索引。如果需要在整个表上保证唯一性,则创建普通的唯一性索引;如果需要保证每个分区内的唯一性,则可以在每个分区上创建唯一性索引。
    在整个表上创建唯一性索引:
CREATE UNIQUE INDEX idx_users_user_id ON users(user_id);

在每个分区上创建唯一性索引(如果数据库支持):

CREATE UNIQUE INDEX idx_users_p0_user_id ON users(user_id) PARTITION p0; CREATE UNIQUE INDEX idx_users_p1_user_id ON users(user_id) PARTITION p1; ...
  1. 维护索引
    当向表中插入数据时,数据库会自动维护这些索引。如果尝试插入重复的数据,数据库将阻止插入操作,并返回错误信息。
  2. 分区维护
    根据业务需求,可能需要定期对分区进行维护,如增加新的分区、删除旧的分区或者对分区进行合并和拆分。在维护分区时,需要确保相关的索引也得到正确的维护。
    请注意,不同的数据库管理系统(如MySQL、Oracle、PostgreSQL等)在分区表和索引的实现上可能会有差异,因此具体的SQL语句可能会有所不同。在实施分区和索引策略时,需要参考相应数据库的官方文档以获取准确的操作方法。

二、KIMI

https://kimi.moonshot.cn/

在数据库中,创建索引是一种提高查询效率、确保数据唯一性的重要手段。以下是创建分区表索引的一般步骤:

  1. 确定索引类型:首先,你需要确定要创建的索引类型,比如普通索引、唯一索引或主键索引。唯一索引可以确保表中的数据在索引列上的唯一性。

  2. 确定分区键:在分区表中,你需要确定一个或多个列作为分区键。分区键的选择会影响数据的分布和查询性能。

  3. 创建分区表:根据你的数据库系统,创建分区表。不同的数据库系统有不同的语法。

  4. 创建索引

  • 对于大多数数据库系统,创建索引的语法类似于:
CREATE INDEX index_name ON table_name (column_name);
  • 如果是唯一索引,可以添加 UNIQUE 关键字:
CREATE UNIQUE INDEX index_name ON table_name (column_name);
  • 如果表已经创建,并且需要添加索引,可以使用上述语法直接添加。
  1. 考虑分区索引:在某些数据库系统中,你可能需要特别创建分区索引。这通常涉及到指定索引的分区方法,以匹配表的分区策略。

  2. 测试索引:创建索引后,你应该测试其性能和正确性,确保它按预期工作。

  3. 维护索引:索引需要定期维护,以保持其性能。这可能包括重建索引、更新统计信息等。

请注意,具体的SQL语法和功能可能会根据你使用的数据库系统(如MySQL、PostgreSQL、Oracle等)有所不同。因此,你应该查阅你所使用的数据库系统的文档,以获取更具体的指导和示例。

三、通义千问

https://tongyi.aliyun.com/qianwen

在分区表上创建索引来保证数据唯一性,尤其是涉及到唯一约束时,需要特别注意索引应当包含分区键。不同数据库系统(如Oracle、SQL Server等)有其特定的语法和最佳实践,但基本原则相似。以下是创建索引以确保分区表数据唯一性的通用步骤和注意事项:

通用步骤:

  1. 确定分区策略:首先,根据数据的特性选择合适的分区方法,如范围分区、列表分区或哈希分区等。

  2. 包含分区键:在创建唯一索引时,确保索引中包含了分区键。这是因为唯一约束需要跨所有分区保持数据的唯一性,而包含分区键能帮助数据库引擎高效地定位和验证数据的唯一性。

示例(以Oracle为例):

假设有一个按日期范围分区的表orders,分区键为order_date,且希望保证(order_id, customer_id)的组合唯一。

CREATE TABLE orders ( order_id INT, customer_id INT, order_date DATE ) PARTITION BY RANGE (order_date) ( PARTITION orders_q1 VALUES LESS THAN (TO_DATE('2024-04-01', 'YYYY-MM-DD')), PARTITION orders_q2 VALUES LESS THAN (TO_DATE('2024-07-01', 'YYYY-MM-DD')), -- 更多分区... ); -- 创建包含分区键的局部唯一索引 CREATE UNIQUE INDEX idx_orders_unique ON orders(order_id, customer_id, order_date) LOCAL;

在这个例子中,order_date作为分区键被包含在唯一索引中,这样就可以确保每个(order_id, customer_id)组合在每个分区中都是唯一的,并且跨分区也是唯一的。

注意事项:

  • 局部索引 vs 全局索引:局部索引会随着表的分区而分区,每个分区的索引部分存储在相应的表分区所在的位置或指定位置,更利于管理和查询性能。全局索引则跨所有分区独立存在,适用于需要在整个表范围内快速查找的情况,但维护成本较高,特别是在大量数据插入或更新时。

  • 维护成本:在高并发写入场景下,维护唯一索引可能会影响性能,特别是全局唯一索引,因为它们需要跨所有分区维护索引结构的一致性。

  • 索引选择:根据查询模式选择合适的索引类型,如果大部分查询都涉及到分区键,那么局部索引通常是更好的选择。如果查询经常跨越多个分区,则可能需要考虑全局索引。

确保在实施前充分测试和分析,以确定最适合您应用场景的策略。

四、万知

https://www.wanzhi.com/
万知的文本复制不带换行,这里稍微排版。

在数据库中,创建索引来保证数据的唯一性通常是通过定义唯一索引(Unique Index)来实现的。对于分区表,这个过程与普通表类似,但需要考虑分区策略。 以下是在一些常见的数据库系统中为分区表创建唯一索引的基本步骤:

1. MySQL(使用InnoDB引擎)

在MySQL中,如果你使用的是InnoDB存储引擎,你可以为分区表创建唯一索引。但是,需要注意的是,全局唯一索引(跨所有分区的唯一索引)可能会影响分区表的数据分布和性能。

CREATE TABLE partitioned_table ( id INT NOT NULL, data VARCHAR(100), PRIMARY KEY (id), UNIQUE KEY unique_index (data) ) PARTITION BY HASH(id) PARTITIONS 4;

2. Oracle

在Oracle数据库中,也可以为分区表创建局部唯一索引(Local Unique Index)或全局唯一索引(Global Unique Index)。局部唯一索引是根据分区键创建的,每个分区都有自己的唯一索引。

CREATE TABLE partitioned_table ( id NUMBER NOT NULL, data VARCHAR2(100), CONSTRAINT partitioned_table_pk PRIMARY KEY (id) ) PARTITION BY HASH(id) PARTITIONS 4; -- 创建局部唯一索引 CREATE UNIQUE INDEX local_unique_index ON partitioned_table(data) LOCAL; -- 创建全局唯一索引 CREATE UNIQUE INDEX global_unique_index ON partitioned_table(data);

3. PostgreSQL

在PostgreSQL中,分区表的索引创建与普通表类似。你可以为分区表创建唯一索引来保证数据的唯一性。

CREATE TABLE partitioned_table ( id SERIAL PRIMARY KEY, data TEXT ) PARTITION BY RANGE(id); CREATE INDEX unique_index ON partitioned_table USING btree(data);

注意事项

  • 分区键选择:在创建分区表时,选择合适的分区键非常重要,因为它会影响数据的分布和查询性能。

  • 全局唯一性:如果你需要保证的是全局唯一性,那么你需要确保索引是全局的,而不是局部分区的。

  • 性能考虑:在分区表上创建全局唯一索引可能会影响DML操作的性能,因为数据库需要跨分区检查唯一性。

  • 维护成本:维护分区表和索引可能会比维护非分区表更复杂,需要考虑这一点。

在创建索引时,确保遵循你所使用的数据库系统的最佳实践,并考虑到你的具体需求和数据特性。

五、360智脑

https://chat.360.com/chat

在数据库中,创建索引的目的是为了加速查询过程,同时可以通过唯一索引来保证数据的唯一性。对于分区表,由于其数据分布在不同的分区中,因此在创建索引时需要考虑如何使得索引能够高效地支持分区表的查询和维护数据的唯一性。

在创建分区表的索引以保证数据唯一性时,可以按照以下步骤操作:

  1. 确定分区策略:首先,根据业务需求和数据特点选择合适的分区策略,例如范围分区(Range)、哈希分区(Hash)或是列表分区(List)等。

  2. 设计索引结构:针对分区表,可以选择创建全局索引(Global Index)或局部索引(Local Index)。全局索引类似于普通表的索引,它跨越所有分区。而局部索引仅在每个分区内部有效。对于保证数据唯一性来说,通常可以在分区表上创建唯一索引,这可以是全局索引或局部索引。

  3. 创建唯一索引:根据需要保证唯一性的列创建唯一索引。如果要确保每条记录在某个特定列或列组合上的唯一性,可以创建唯一索引。例如,在MySQL中,可以使用ALTER TABLE语句添加唯一索引。

ALTER TABLE table_name ADD UNIQUE INDEX index_name (column1, column2, ...);

其中,table_name是表名,index_name是索引名,column1, column2, … 是列名。

  1. 分区定义与索引结合:如果想要通过分区表达某些查询条件,可以在创建分区表时将这些条件作为分区函数的一部分。这样,当数据写入到各个分区时,数据库系统可以根据分区函数自动将数据分配到正确的分区中。

  2. 维护索引:对于分区表上的全局索引,需要定期对索引进行维护,比如重建、优化等操作,以保持索引的高效性能。

具体语法和操作可能会根据不同数据库管理系统(如MySQL、Oracle、SQL Server等)有所不同,请参照相应数据库的文档指南。总之,合理地创建和维护索引是保证数据唯一性和提升查询效率的关键所在。

六、Gemini 1.5

分区表创建索引以保证数据唯一性与普通表类似,但需要注意一些特殊情况:

1. 唯一索引的创建:

  • 和普通表一样,在分区表上创建唯一索引,可以使用 CREATE UNIQUE INDEX 语句:
CREATE UNIQUE INDEX idx_unique_column ON partitioned_table (column1, column2);
  • partitioned_table 是分区表的名称。

  • column1column2 是用作唯一索引的列。

  • 唯一索引将确保在每个分区中,指定列的组合都是唯一的,但并不保证整个表中数据的唯一性

  • 如果你需要在整个表中保证数据唯一性,你需要在所有分区上都创建相同的唯一索引。

2. 跨分区唯一性

  • 使用全局唯一索引:

  • 某些数据库支持全局唯一索引,例如 PostgreSQL。

  • 全局唯一索引在整个表范围内强制实施唯一性约束。

  • 使用触发器:

  • 你可以编写触发器,在数据插入时检查所有分区是否存在重复数据。

  • 这种方法需要额外的代码,但可以实现跨分区唯一性。

  • 使用应用程序逻辑:

  • 在应用程序层,你可以实现逻辑来确保插入数据之前,检查整个表中是否存在重复数据。

  • 这种方法需要应用程序代码参与,但可以实现灵活的唯一性控制。

3. 分区键的唯一性:

  • 分区键本身可以是唯一的。
  • 这意味着每个分区都有一个唯一的标识符。
  • 这可以用来保证跨分区的数据唯一性,但需要确保分区键的设计能够满足你的业务需求。

4. 注意事项:

  • 索引创建后,需要考虑索引维护的成本。
  • 选择合适的索引类型和字段,以提高查询性能和数据维护效率。

总结:

创建索引以保证分区表数据唯一性需要根据具体需求进行选择。如果需要在整个表中保证唯一性,可以考虑使用全局唯一索引,触发器或应用程序逻辑。如果只需要在每个分区内保证唯一性,可以使用普通的唯一索引。

希望以上信息对您有所帮助!

七、GTP-3.5 Turbo

在创建分区表上的索引来保证数据的唯一性时,可以按照以下步骤进行操作:

  1. 选择唯一性约束条件:确定需要保证唯一性的列或列的组合,这些列的取值组合必须在整个分区表中都是唯一的。

  2. 创建唯一性索引:在选择的列或列的组合上创建唯一性索引,可以使用 CREATE UNIQUE INDEX 语句。

CREATE UNIQUE INDEX idx_unique_column ON partitioned_table (column1, column2);
  • idx_unique_column 是索引的名称。
  • partitioned_table 是分区表的名称。
  • column1column2 是需要包含在索引中的列。
  1. 纳入分区键(可选):如果需要在整个分区表中确保唯一性,可以将分区键列也包含在唯一性索引中。这样可以确保在整个表中的每个分区都是唯一的。

  2. 验证唯一性:在创建完成唯一性索引后,最好对索引进行验证,确保数据唯一性的有效性。可以通过插入重复数据来测试索引的唯一性。

  3. 注意索引维护:唯一性索引将增加数据插入、删除和更新操作的成本,因为数据库必须确保新数据不会违反唯一性约束。因此,在使用唯一性索引时需要考虑索引维护的性能影响。

通过以上步骤,你可以在创建分区表上的索引来保证数据的唯一性。希望这些信息对你有所帮助!如果有任何疑问或需要更多帮助,请随时告诉我。

八、墨天轮-数据库智能助手

https://www.modb.pro/moai

在分区表上创建索引来保证数据的唯一性通常涉及以下几点:

  1. 主键和全局索引

    • 分区表的主键会自动创建一个全局(GLOBAL)索引,这个索引是基于主键列的,并确保整个表的主键列的唯一性。
    • 主键字段必须包含分区键,这意味着主键的值在每个分区中都是唯一的。
    • 示例:在TB1表中,TB1_PK是一个基于id列的全局唯一索引。
  2. 唯一性约束

    • 如果你希望在非主键列上创建唯一约束,这个约束必须是全局的,因为局部(LOCAL)索引仅适用于每个分区内的唯一性,而不是整个表。
    • 示例:在创建索引tb1_id_no_idx时,如果idno列联合起来是唯一的,这个全局索引可以确保整个表的这个组合的唯一性。
  3. 局部索引

    • 局部索引(LOCAL)是针对每个分区独立创建的,它们不保证跨分区的唯一性。
    • 示例:在TB1表中,tb1_no_idx是一个基于no列的局部索引,它仅在每个分区内部保证no列的唯一性。
  4. 创建唯一局部索引

    • 虽然局部索引不能独立保证整个表的唯一性,但在包含分区键的情况下,可以创建局部唯一索引,因为分区键的值已经确保了分区内的唯一性。
    • 示例:如果no列是分区键的一部分,创建一个基于no的局部索引可以确保每个分区内的no值唯一。
  5. 选择合适的索引类型

    • 除了B-Tree索引,还可以考虑其他类型的索引,如Bitmap索引,但这通常适用于特定查询场景,并且有特定的限制,例如Bitmap索引必须是局部的。

创建唯一性索引的SQL命令可能如下所示(假设no列是分区键的一部分):

CREATE UNIQUE INDEX idx_unique_no ON tb1 (no) LOCAL;

请注意,实际的SQL命令需要根据你的数据库管理系统(如Oracle、PostgreSQL等)和具体需求进行调整。在执行任何操作之前,你应该根据你的业务需求和表的大小来评估索引性能和存储成本。

墨天轮刚上线还有不少问题,复制粘贴时发现有这么一大段内容未被展示:

<references title="References" type="chat" references="[{&quot;name&quot;: &quot;/content/vldbg/partition-create-tables-indexes.html&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;4.1.15 Specifying Partitioning When Creating Index-Organized TablesFor index-organized tables, you can use the range, list, or hash partitioning method.The semantics for creating partitioned index-organized tables are similar to that for regular tables with these differences:When you create the table, you specify the ORGANIZATION INDEX clause, and INCLUDING and OVERFLOW clauses as necessary.The PARTITION clause can have OVERFLOW subclauses that allow you to specify attributes of the overflow segments at the partition level.Specifying an OVERFLOW clause results in the overflow data segments themselves being equipartitioned with the primary key index segments. Thus, for partitioned index-organized tables with overflow, each partition has an index segment and an overflow data segment.For index-organized tables, the set of partitioning columns must be a subset of the primary key columns. Because rows of an index-organized table are stored in the primary key index for the table, the partitioning criterion affects the availability. By choosing the partitioning key to be a subset of the primary key, an insert operation must only verify uniqueness of the primary key in a single partition, thereby maintaining partition independence.Support for secondary indexes on index-organized tables is similar to the support for regular tables. Because of the logical nature of the secondary indexes, global indexes on index-organized tables remain usable for certain operations where they would be marked UNUSABLE for regular tables.The following topics are discussed:Creating Range-Partitioned Index-Organized TablesCreating Hash Partitioned Index-Organized TablesCreating List-Partitioned Index-Organized TablesSee Also:Maintenance Operations for Partitioned Tables and Indexes for information about maintenance operations on index-organized tablesOracle Database Administrators Guide for more information about managing index-organized tablesOracle Database Concepts for more information about index-organized tables&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;/content/vldbg/partition-create-tables-indexes.html\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: -1.0, \&quot;rerank_score\&quot;: 0.61834}&quot;}]}, {&quot;name&quot;: &quot;/content/vldbg/partition-concepts.html&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;2.5.5 Miscellaneous Information about Creating Indexes on Partitioned TablesYou can create bitmap indexes on partitioned tables, with some restrictions.Bitmap indexes must be local to the partitioned table. They cannot be global indexes.Global indexes can be unique. Local indexes can only be unique if the partitioning key is a part of the index key.&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;/content/vldbg/partition-concepts.html\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: -1.0, \&quot;rerank_score\&quot;: 0.61427}&quot;}]}, {&quot;name&quot;: &quot;金仓数据库KingbaseES分区表 -- 声明式创建分区表&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;n index_name | index_type | table_name | table_type | uniqueness | compression n------------+------------+------------+------------+------------+-------------n TB1_PK     | BTREE      | TB1        | TABLE      | UNIQUE     | DISABLEDn1 row分区表添加主键:n添加主键的同时会创建主键列字段唯一索引但是有唯一索引的列不一定是主键。n主键字段不允许空值,添加主键过程中会自动添加not null非空约束,保证主键列值的唯一性。n分区表添加主键同时创建的索引索引有GLOBAL是全局索引。n分区表会在主键列创建一个全局global索引,默认为添加主键列的同时创建全局索引。n分区表唯一约束必须包含分区键。n1.3 对分区表创建索引n# 分区表创建索引ncreate index on tb1 no local;nCREATE INDEXncreate index on tb1 id,no global;nCREATE INDEX# 查看tb1表信息ntest=# d+ tb1n                                        Partitioned table public.tb1n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition key: RANGE pdatenRange interval: INTERVAL 0-1::pg_catalog.intervalnIndexes:n    tb1_pk PRIMARY KEY, btree id INCLUDE tableoid GLOBAL n    tb1_id_no_idx btree id, non    tb1_no_idx btree nonPartitions: tb1_tb1_p1 FOR VALUES FROM MINVALUE TO 2019-01-01 00:00:00,n            tb1_tb1_p2 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00,n            tb1_tb1_p3 FOR VALUES FROM 2019-02-01 00:00:00 TO 2019-03-01 00:00:00,n            tb1_tb1_p4 FOR VALUES FROM 2019-03-01 00:00:00 TO 2019-04-01 00:00:00n            n# 查看分区表tb1子表信息ntest=# d+ tb1_tb1_p2n                                          Table public.tb1_tb1_p2n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition of: tb1 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00nPartition constraint: pdate IS NULL OR pdate::timestamp without time zone &gt;= 2019-01-01 00:00:00::date AND pdate IS NOT NULL AND pdate::timestamp without time zone &lt; 2019-02-01 00:00:00::datenIndexes:n    tb1_tb1_p2_id_no_idx btree id, non    tb1_tb1_p2_no_idx btree nonAccess method: heap分区表创建索引:n在分区表创建本地索引,会自动在每个分区上创建一个本地索引。&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;金仓数据库KingbaseES分区表 -- 声明式创建分区表\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.76786, \&quot;rerank_score\&quot;: 0.60797}&quot;}, {&quot;id&quot;: 2, &quot;content&quot;: &quot;n在申明式创建的分区表上创建索引,会自动将索引应用于所有的子分区。n分区表惟一约束必须包括分区键。n不能创建包含所有子分区的排除约束,只能每个子分区单独创建。n在分区表创建索引时不可使用CONCURRENTLY,可使用on only在分区表创建标记失效的索引,避免大表创建索引耗时太久子分区不会自动应用该索引,然后在所有子分区单独创建索引可使用CONCURRENTLY,最后使用ALTER INDEX … ATTACH PARTITION附加到到父索引,所有子分区索引附加到父索引后会自动标记为有效。n# 分区表不支持使用CONCURRENTLY在父表创建索引ntest=# create index CONCURRENTLY on tb1info;nERROR:  cannot create index on partitioned table tb1 concurrentlynTime: 0.519 ms# 使用on only在分区表创建索引ntest=# create index on only tb1info;    nCREATE INDEXnTime: 1.845 ms# 查看分区表tb1信息,tb1_info_idx标记为无效invalidntest=# d+ tb1n                                        Partitioned table public.tb1n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition key: RANGE pdatenRange interval: INTERVAL 0-1::pg_catalog.intervalnIndexes:n    tb1_pk PRIMARY KEY, btree id INCLUDE tableoid GLOBAL n    tb1_id_no_idx btree id, non    tb1_info_idx btree info INVALIDn    tb1_no_idx btree nonPartitions: tb1_tb1_p1 FOR VALUES FROM MINVALUE TO 2019-01-01 00:00:00,n            tb1_tb1_p2 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00,n            tb1_tb1_p3 FOR VALUES FROM 2019-02-01 00:00:00 TO 2019-03-01 00:00:00,n            tb1_tb1_p4 FOR VALUES FROM 2019-03-01 00:00:00 TO 2019-04-01 00:00:00,n            tb1_tb1_p5 FOR VALUES FROM 2019-04-01 00:00:00 TO 2019-04-30 00:00:00# 单独创建所有子分区索引ntest=# create index tb1_tb1_p1_info_idx on tb1_tb1_p1info;&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;金仓数据库KingbaseES分区表 -- 声明式创建分区表\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.7964, \&quot;rerank_score\&quot;: 0.5976}&quot;}]}, {&quot;name&quot;: &quot;/content/vldbg/partition-oltp.html&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;Each partition of a partitioned index is considered an autonomous segment. To enforce the autonomy of these segments, you always have to include the partitioning key columns as a subset of the unique key definition.Global partitioned indexes must always be prefixed with at least the first leading column of the index column the partitioning column of the partitioned global index.Unique local indexes must have the partitioning key of the table as a subset of the unique key definition.Example 7-1 shows the creation of a unique index on the order_id column of the orders_oltp table. The order_id in the OLTP application is filled using a sequence number. The unique index uses hash partitioning to reduce contention for the monotonically increasing order_id values. The unique key is then used to create the primary key constraint.&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;small\&quot;, \&quot;source\&quot;: \&quot;/content/vldbg/partition-oltp.html\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.75343, \&quot;rerank_score\&quot;: 0.60764}&quot;}]}, {&quot;name&quot;: &quot;人大金仓KingbaseES分区表 -- 声明式创建分区表&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;n index_name | index_type | table_name | table_type | uniqueness | compression n------------+------------+------------+------------+------------+-------------n TB1_PK     | BTREE      | TB1        | TABLE      | UNIQUE     | DISABLEDn1 rown分区表添加主键:添加主键的同时会创建主键列字段唯一索引但是有唯一索引的列不一定是主键。主键字段不允许空值,添加主键过程中会自动添加not null非空约束,保证主键列值的唯一性。分区表添加主键同时创建的索引索引有GLOBAL是全局索引。分区表会在主键列创建一个全局global索引,默认为添加主键列的同时创建全局索引。分区表唯一约束必须包含分区键。1.3 对分区表创建索引# 分区表创建索引ncreate index on tb1 no local;nCREATE INDEXncreate index on tb1 id,no global;nCREATE INDEX# 查看tb1表信息ntest=# d+ tb1n                                        Partitioned table public.tb1n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition key: RANGE pdatenRange interval: INTERVAL 0-1::pg_catalog.intervalnIndexes:n    tb1_pk PRIMARY KEY, btree id INCLUDE tableoid GLOBAL n    tb1_id_no_idx btree id, non    tb1_no_idx btree nonPartitions: tb1_tb1_p1 FOR VALUES FROM MINVALUE TO 2019-01-01 00:00:00,n            tb1_tb1_p2 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00,n            tb1_tb1_p3 FOR VALUES FROM 2019-02-01 00:00:00 TO 2019-03-01 00:00:00,n            tb1_tb1_p4 FOR VALUES FROM 2019-03-01 00:00:00 TO 2019-04-01 00:00:00n            n# 查看分区表tb1子表信息ntest=# d+ tb1_tb1_p2n                                          Table public.tb1_tb1_p2n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition of: tb1 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00nPartition constraint: pdate IS NULL OR pdate::timestamp without time zone &gt;= 2019-01-01 00:00:00::date AND pdate IS NOT NULL AND pdate::timestamp without time zone &lt; 2019-02-01 00:00:00::datenIndexes:n    tb1_tb1_p2_id_no_idx btree id, non    tb1_tb1_p2_no_idx btree nonAccess method: heapn分区表创建索引:在分区表创建本地索引,会自动在每个分区上创建一个本地索引。&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;人大金仓KingbaseES分区表 -- 声明式创建分区表\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.77305, \&quot;rerank_score\&quot;: 0.60539}&quot;}]}, {&quot;name&quot;: &quot;/content/vldbg/index-partitioning.html&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;3.3.1 Local Partitioned IndexesIn a local index, all keys in a particular index partition refer only to rows stored in a single underlying table partition.A local index is created by specifying the LOCAL attribute. Oracle constructs the local index so that it is equipartitioned with the underlying table. Oracle partitions the index on the same columns as the underlying table, creates the same number of partitions or subpartitions, and gives them the same partition bounds as corresponding partitions of the underlying table.Oracle also maintains the index partitioning automatically when partitions in the underlying table are added, dropped, merged, or split, or when hash partitions or subpartitions are added or coalesced. This ensures that the index remains equipartitioned with the table.A local index can be created UNIQUE if the partitioning columns form a subset of the index columns. This restriction guarantees that rows with identical index keys always map into the same partition, where uniqueness violations can be detected.&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;small\&quot;, \&quot;source\&quot;: \&quot;/content/vldbg/index-partitioning.html\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.74532, \&quot;rerank_score\&quot;: 0.60086}&quot;}]}, {&quot;name&quot;: &quot;/content/vldbg/partition-warehouse.html&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot; In those cases, it can be faster to scan a single index than to scan many index partitions to find all matching rows.Unique indexes on columns other than the partitioning columns must be global because unique local nonprefixed indexes whose keys do not contain the partitioning keys are not supported. Unique keys are not always enforced in data warehouses due to the controlled data load processes and the performance cost of enforcing the unique constraint. Global indexes can grow very large on tables with billions of rows.The following example creates a global unique index on the sales table:CREATE UNIQUE INDEX sales_unique_ixON salescust_id, prod_id, promo_id, channel_id, time_idPARALLEL NOLOGGING;Very few queries benefit from this index. In systems with a very limited data load window, consider not creating and maintaining it.&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;small\&quot;, \&quot;source\&quot;: \&quot;/content/vldbg/partition-warehouse.html\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.73945, \&quot;rerank_score\&quot;: 0.59901}&quot;}]}, {&quot;name&quot;: &quot;人大金仓 KingbaseES 分区表 -- 声明式创建分区表&quot;, &quot;chunks&quot;: [{&quot;id&quot;: 1, &quot;content&quot;: &quot;n index_name | index_type | table_name | table_type | uniqueness | compression n------------+------------+------------+------------+------------+-------------n TB1_PK     | BTREE      | TB1        | TABLE      | UNIQUE     | DISABLEDn1 rown分区表添加主键:添加主键的同时会创建主键列字段唯一索引但是有唯一索引的列不一定是主键。主键字段不允许空值,添加主键过程中会自动添加 not null 非空约束,保证主键列值的唯一性。分区表添加主键同时创建的索引索引有 GLOBAL是全局索引。分区表会在主键列创建一个全局global索引,默认为添加主键列的同时创建全局索引。分区表唯一约束必须包含分区键。1.3 对分区表创建索引# 分区表创建索引ncreate index on tb1 no local;nCREATE INDEXncreate index on tb1 id,no global;nCREATE INDEX# 查看tb1表信息ntest=# d+ tb1n                                        Partitioned table public.tb1n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition key: RANGE pdatenRange interval: INTERVAL 0-1::pg_catalog.intervalnIndexes:n    tb1_pk PRIMARY KEY, btree id INCLUDE tableoid GLOBAL n    tb1_id_no_idx btree id, non    tb1_no_idx btree nonPartitions: tb1_tb1_p1 FOR VALUES FROM MINVALUE TO 2019-01-01 00:00:00,n            tb1_tb1_p2 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00,n            tb1_tb1_p3 FOR VALUES FROM 2019-02-01 00:00:00 TO 2019-03-01 00:00:00,n            tb1_tb1_p4 FOR VALUES FROM 2019-03-01 00:00:00 TO 2019-04-01 00:00:00n            n# 查看分区表tb1子表信息ntest=# d+ tb1_tb1_p2n                                          Table public.tb1_tb1_p2n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition of: tb1 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00nPartition constraint: pdate IS NULL OR pdate::timestamp without time zone &gt;= 2019-01-01 00:00:00::date AND pdate IS NOT NULL AND pdate::timestamp without time zone &lt; 2019-02-01 00:00:00::datenIndexes:n    tb1_tb1_p2_id_no_idx btree id, non    tb1_tb1_p2_no_idx btree nonAccess method: heapn分区表创建索引:在分区表创建本地索引,会自动在每个分区上创建一个本地索引。&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;人大金仓 KingbaseES 分区表 -- 声明式创建分区表\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.77008, \&quot;rerank_score\&quot;: 0.59879}&quot;}, {&quot;id&quot;: 2, &quot;content&quot;: &quot;在申明式创建的分区表上创建索引,会自动将索引应用于所有的子分区。分区表惟一约束必须包括分区键。不能创建包含所有子分区的排除约束,只能每个子分区单独创建。在分区表创建索引时不可使用 CONCURRENTLY,可使用 on only 在分区表创建标记失效的索引,避免大表创建索引耗时太久子分区不会自动应用该索引,然后在所有子分区单独创建索引可使用 CONCURRENTLY,最后使用 ALTER INDEX .. ATTACH PARTITION 附加到到父索引,所有子分区索引附加到父索引后会自动标记为有效。# 分区表不支持使用CONCURRENTLY在父表创建索引ntest=# create index CONCURRENTLY on tb1info;nERROR:  cannot create index on partitioned table tb1 concurrentlynTime: 0.519 ms# 使用on only在分区表创建索引ntest=# create index on only tb1info;    nCREATE INDEXnTime: 1.845 ms# 查看分区表tb1信息,tb1_info_idx标记为无效invalidntest=# d+ tb1n                                        Partitioned table public.tb1n Column |            Type            | Collation | Nullable | Default | Storage  | Stats target | Description n--------+----------------------------+-----------+----------+---------+----------+--------------+-------------n id     | bigint                     |           | not null |         | plain    |              | n stat   | date                       |           |          |         | plain    |              | n no     | bigint                     |           |          |         | plain    |              | n pdate  | date                       |           |          |         | plain    |              | n info   | character varying50 char |           |          |         | extended |              | nPartition key: RANGE pdatenRange interval: INTERVAL 0-1::pg_catalog.intervalnIndexes:n    tb1_pk PRIMARY KEY, btree id INCLUDE tableoid GLOBAL n    tb1_id_no_idx btree id, non    tb1_info_idx btree info INVALIDn    tb1_no_idx btree nonPartitions: tb1_tb1_p1 FOR VALUES FROM MINVALUE TO 2019-01-01 00:00:00,n            tb1_tb1_p2 FOR VALUES FROM 2019-01-01 00:00:00 TO 2019-02-01 00:00:00,n            tb1_tb1_p3 FOR VALUES FROM 2019-02-01 00:00:00 TO 2019-03-01 00:00:00,n            tb1_tb1_p4 FOR VALUES FROM 2019-03-01 00:00:00 TO 2019-04-01 00:00:00,n            tb1_tb1_p5 FOR VALUES FROM 2019-04-01 00:00:00 TO 2019-04-30 00:00:00# 单独创建所有子分区索引ntest=# create index tb1_tb1_p1_info_idx on tb1_tb1_p1info;&quot;, &quot;meta_info&quot;: &quot;{\&quot;chunk_type\&quot;: \&quot;large\&quot;, \&quot;source\&quot;: \&quot;人大金仓 KingbaseES 分区表 -- 声明式创建分区表\&quot;}&quot;, &quot;recall_score&quot;: &quot;{\&quot;similar_score:\&quot;: 0.79316, \&quot;rerank_score\&quot;: 0.59701}&quot;}]}]"></references>

您更喜欢哪个大模型? 欢迎留言讨论。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论