首页 > 解决方案 > 使用 MyISAM 引擎在临时表上使用 MySQL 二级索引的结果不正确

问题描述

我在 Amazon RDS (Aurora) 中使用 MySql 5.7。问题在于临时表上的二级索引(用作存储过程的一部分):

create temporary table tmpDemo (
 c1 varchar(50) NOT NULL, PRIMARY KEY(c1), 
 c2 varchar(100) NOT NULL,...     
) Engine=MyIsam;
-- ... insert data into tmpDemo
  select c1,c2 from tmpDemo;      
  create index ix_secondary on tmpDemo(c2) USING BTREE;      
  select c1,c2 from tmpDemo;

在最后一次选择时,C1 列中的数据被复制。就好像二级索引去掉了主键的唯一性约束:

11237357    00
11237357    00
11237357    00

下面的小程序重现了这个问题。如果您想知道我为什么使用 MyISAM 而不是 InnoDB,那是因为这是 AWS Aurora DB 上的只读集群,所以我别无选择。我不知道独立的 MySQL 实例是否会发生这种情况。

drop procedure if exists showIxBug;
DELIMITER $$
create procedure showIxBug()
BEGIN
drop temporary table if exists tmpDemo;

create temporary table tmpDemo (
 c1 varchar(50) NOT NULL, PRIMARY KEY(c1), 
 c2 varchar(100) NOT NULL,
 c3 int,
 c4 datetime NOT NULL
) Engine=MyIsam;

set @i=0;
  WHILE @i < 10000 DO
    set @seed = FLOOR(RAND()*12121212);
    set @c1 = convert(@seed,char);
    set @c2 = convert(@seed % 100,char);
    INSERT INTO  tmpDemo VALUES (
    @c1,concat(@c2,@c2),@seed,
    utc_timestamp()
        ) on duplicate key update c3=100;
    SET @i = @i + 1;
  END WHILE;

  select c1,c2 from tmpDemo;

  create index ix_secondary on tmpDemo(c2) USING BTREE;

  select c1,c2 from tmpDemo;

END$$

DELIMITER ;
call showIxBug();

标签: mysqlamazon-aurora

解决方案


根据官方文档,我不确定那里发生了什么,但Aurora 根本不支持 MyISAM 。


推荐阅读