Oracle 租户不支持 INSERT IGNORE 语法。
问题现象
使用 INSERT IGNORE 语句插入时报错。
obclient [gtx_test]> insert ignore into gtx_tb1 values(3,3,'3');
ERROR 1235 (0A000): ignore with global index not supported
问题原因
MySQL 租户支持使用 INSERT IGNORE 来忽略 INSERT 过程发生的错误(比如主角冲突等),让 INSERT 语句可以继续执行,并返回一个"正确"的返回码(ret_code)。比如,在一次性插入多条记录时,可以使用 INSERT IGNORE 来忽略其中某些记录的插入失败,让其他记录得以插入成功。 但是,当表中存在 Global 索引时,INSERT IGNORE 功能不被支持。
测试示例如下。
创建测试表 gtx_tb1,设置主键为 c1,并插入2 条记录。
obclient [gtx_test]> create table gtx_tb1 (c1 integer primary key, c2 integer, c3 varchar(100)) partition by hash(c1) partitions 9; Query OK, 0 rows affected (0.067 sec)obclient [gtx_test]> insert into gtx_tb1 values(1,1,'1'),(2,2,'2'); Query OK, 1 row affected (0.008 sec)查询测试表内容。
obclient [gtx_test]> select * from gtx_tb1;输出结果如下。
+----+------+------+ | c1 | c2 | c3 | +----+------+------+ | 1 | 1 | 1 | | 2 | 2 | 2 | +----+------+------+ 2 rows in set (0.019 sec)再插入 2 条记录:(2,2,'x'),(3,3,'3')。其中,(2,2,'x') 会导致主键冲突,不论是否使用 IGNORE, 都不会插入成功。
obclient [gtx_test]> insert into gtx_tb1 values(2,2,'x'),(3,3,'3'); ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'obclient [gtx_test]> insert ignore into gtx_tb1 values(2,2,'x'),(3,3,'3'); Query OK, 1 row affected (0.005 sec)再次查询测试表内容。
obclient [gtx_test]> select * from gtx_tb1;输出结果如下。
+----+------+------+ | c1 | c2 | c3 | +----+------+------+ | 2 | 2 | 2 | | 1 | 1 | 1 | | 3 | 3 | 3 | +----+------+------+ 3 rows in set (0.003 sec)查看 INSERT 语句的返回码,INSERT IGNORE 忽略了主键冲突的报错。
obclient [gtx_test]> select query_sql,ret_code from oceanbase.gv$sql_audit where query_sql like 'insert%gtx_tb1%' order by request_time desc limit 10;输出结果如下。
+-------------------------------------------------------+----------+ | query_sql | ret_code | +-------------------------------------------------------+----------+ | insert ignore into gtx_tb1 values(2,3,'x'),(3,3,'3') | 0 | | insert into gtx_tb1 values(2,2,'x'),(3,3,'3') | -5024 | | insert into gtx_tb1 values(1,1,'1'),(2,2,'2') | 0 | +-------------------------------------------------------+----------+创建 Global 索引,再次使用 INSERT IGNORE 时报错。
obclient [gtx_test]> create index gtx_tb1_ix1 on gtx_tb1(c2) global; Query OK, 0 rows affected (0.872 sec)obclient [gtx_test]> insert ignore into gtx_tb1 values(2,3,'x'),(3,3,'3'); ERROR 1235 (0A000): ignore with global index not supportedobclient [gtx_test]> insert ignore into gtx_tb1 values(4,4,'4'); ERROR 1235 (0A000): ignore with global index not supported
适用版本
OceanBase 数据库所有版本。
解决方式
当表中存在 Global 索引时,INSERT IGNORE 语句不被支持。需要应用程序来保证 INSERT 语句正确,避免出现错误。