kundb unique索引数据重复问题

  其他常见问题
内容纲要

概要描述


客户反馈,kundb3.2.3版本,一张表中设置了唯一索引(unique key),出现重复数据。

先说结论:普通的唯一索引与主键索引仅能保证 shard 内的唯一性

为保证字段的全局唯一性,可采用两种方式:

a)以该主键/唯一索引字段为分片键;
b)对字段创建一个 全局唯一索引 (GLOBAL UNIQUE INDEX),全局唯一索引能够保证所有shard中字段的唯一性。

详细说明


问题说明

建表语句中配置了 UNIQUE KEY ,但是查询数据出现2条重复数据。

直连3个shard底层数据,1条数据出现在shard1,1条出现在shard2。这两条数据不在同一个shard上面。

解决方案:

方案一:以该主键/唯一索引字段为分片键;
--该列即是 unique key,也是分片键
drop table if exists t4;
create table t4(
id1 int ,
id2 int ,
name varchar(32))
distributed by hash(id1);

alter table t4 add unique(id1);

insert into t4 values (2,22,'aa');
insert into t4 values (2,33,'aab');

执行报错:1105 - unknown error: Duplicate entry '2' for key 't4.id1' (errno 1062) (sqlstate 23000) during query: /*DRDS /kv2:16015/0/1// */INSERT INTO t4(id1, id2, name) VALUES (2,33,'aab')

方案二:对字段创建一个 GLOBAL UNIQUE INDEX
drop table if exists t3;
create table t3(
id1 int ,
id2 int ,
name varchar(32))
distributed by hash(id2);

create global unique  index idx_t3 on t3(id1);

insert into t3 values (2,22,'aa');
insert into t3 values (2,33,'aab');

执行报错:1105 - unknown error: Duplicate entry '1' for key 'idx_t3_t3_4kundb_admin.id1' (errno 1062) (sqlstate 23000)

这篇文章对您有帮助吗?

平均评分 0 / 5. 次数: 0

尚无评价,您可以第一个评哦!

非常抱歉,这篇文章对您没有帮助.

烦请您告诉我们您的建议与意见,以便我们改进,谢谢您。