InnoDB和MyISAM都是密集索引

InnoDB和MyISAM都是密集索引

1 以前从一些资料上看到InnoDB的索引是稀疏索引,而MyISAM的索引是密集索引,今天刻意测试了一下,发现竟然不是这样。

2 找时间研究下,mark一下先。

3 mysql> show create table uniq_id\G

4 *************************** 1. row ***************************

5 Table: uniq_id

6 Create Table: CREATE TABLE `uniq_id` (

7 `id` int(11) DEFAULT NULL,

8 KEY `id` (`id`)

9 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

10 1 row in set (0.00 sec)

11

12 mysql> show create table same_id\G

13 *************************** 1. row ***************************

14 Table: same_id

15 Create Table: CREATE TABLE `same_id` (

16 `id` int(11) DEFAULT NULL,

17 KEY `id` (`id`)

18 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

19 1 row in set (0.00 sec)

20

21 mysql> select count(*),count(distinct id) from same_id;

22 +----------+--------------------+

23 | count(*) | count(distinct id) |

24 +----------+--------------------+

25 | 1000000 | 1 |

26 +----------+--------------------+

27 1 row in set (0.21 sec)

28

29 mysql> alter table uniq_id add index(id);

30 Query OK, 1000000 rows affected (1.27 sec)

31 Records: 1000000 Duplicates: 0 Warnings: 0

32

33 mysql> alter table same_id add index(id);

34 Query OK, 1000000 rows affected (1.59 sec)

35 Records: 1000000 Duplicates: 0 Warnings: 0

36

37 -rw-rw---- 1 mysql mysql 11326464 May 26 10:54 same_id.MYI

38 -rw-rw---- 1 mysql mysql 11326464 May 26 10:54 uniq_id.MYI

39

40 mysql> alter table uniq_id engine=innodb;

41 Query OK, 1000000 rows affected (8.89 sec)

42 Records: 1000000 Duplicates: 0 Warnings: 0

43

44 mysql> alter table same_id engine=innodb;

45 Query OK, 1000000 rows affected (8.48 sec)

46 Records: 1000000 Duplicates: 0 Warnings: 0

47

48 -rw-rw---- 1 mysql mysql 58720256 May 26 10:56 same_id.ibd

49 -rw-rw---- 1 mysql mysql 58720256 May 26 10:56 uniq_id.ibd

50

51 mysql> show create table uniq_id2\G

52 *************************** 1. row ***************************

53 Table: uniq_id2

54 Create Table: CREATE TABLE `uniq_id2` (

55 `id` int(11) NOT NULL,

56 `v` int(11) DEFAULT NULL,

57 PRIMARY KEY (`id`)

58 ) ENGINE=MyISAM DEFAULT CHARSET=latin1

59 1 row in set (0.00 sec)

60

61 mysql> show create table same_id2\G

62 *************************** 1. row ***************************

63 Table: same_id2

64 Create Table: CREATE TABLE `same_id2` (

65 `id` int(11) NOT NULL,

66 `v` int(11) DEFAULT NULL,

67 PRIMARY KEY (`id`)

68 ) ENGINE=MyISAM DEFAULT CHARSET=latin1

69 1 row in set (0.00 sec)

70

71

72 -rw-rw---- 1 mysql mysql 10263552 May 26 11:00 same_id2.MYI

73 -rw-rw---- 1 mysql mysql 10263552 May 26 11:01 uniq_id2.MYI

74

75 mysql> alter table same_id2 add index(v);

76 Query OK, 1000000 rows affected (9.06 sec)

77 Records: 1000000 Duplicates: 0 Warnings: 0

78

79 mysql> alter table uniq_id2 add index(v);

80 Query OK, 1000000 rows affected (8.60 sec)

81 Records: 1000000 Duplicates: 0 Warnings: 0

82

83 -rw-rw---- 1 mysql mysql 21605376 May 26 11:02 same_id2.MYI

84 -rw-rw---- 1 mysql mysql 21605376 May 26 11:02 uniq_id2.MYI

85

86 mysql> alter table uniq_id2 engine=innodb;

87 Query OK, 1000000 rows affected (7.66 sec)

88 Records: 1000000 Duplicates: 0 Warnings: 0

89

90 mysql> alter table same_id2 engine=innodb;

91 Query OK, 1000000 rows affected (8.19 sec)

92 Records: 1000000 Duplicates: 0 Warnings: 0

93

94 -rw-rw---- 1 mysql mysql 54525952 May 26 11:05 same_id2.ibd

95 -rw-rw---- 1 mysql mysql 54525952 May 26 11:05 uniq_id2.ibd