使用 MyISAM 引擎在临时 table 上使用 MySQL 二级索引的结果不正确

Incorrect result with MySQL secondary index on temp table with MyISAM engine

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

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;

在最后一个 select,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();

我不确定那里发生了什么,但是 Aurora doesn't support MyISAM at all,根据官方文档。

再次,我从 AWS 技术支持那里得到了答案,我在这里分享它以防其他人遇到它(并且不支付额外支持费用)。这是 AWS Aurora 中的一个错误,据我所知 MySql 并没有那么多。

Doing further research, I was able to find a fix released for 2.03.3 "Fixed a wrong result issue with MyISAM temporary tables where only indexed columns are accessed." [1] In order to verify whether it is something related to this fix I have already reached to the internal team to take a further look at it.

我已经升级到 5.7.mysql_aurora.2.07.2,我可以确认问题已解决。最初的 Aurora 版本是 5.7.12。 (极光编号方案对我来说似乎完全是胡说八道 - 5.7.12 不是 << 5.7.mysql_aurora.1.07.2))