ALTER TABLE增加INDEX LENGTH

时间:2014-07-18 09:05:27

标签: mysql indexing

我有一个名为t_media_items的mysql表。我有3个cols索引(parent_id,type,weight)。索引大小为2.52MB。

mysql> show indexes from t_media_items;

+---------------+------------+--------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table         | Non_unique | Key_name     | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+--------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t_media_items |          0 | PRIMARY      |            1 | id              | A         |      113779 |     NULL | NULL   |      | BTREE      |         |               |
| t_media_items |          1 | idx_ptw      |            1 | parent_id       | A         |       16254 |     NULL | NULL   |      | BTREE      |         |               |
| t_media_items |          1 | idx_ptw      |            2 | type            | A         |       16254 |     NULL | NULL   |      | BTREE      |         |               |
| t_media_items |          1 | idx_ptw      |            3 | weight          | A         |      113779 |     NULL | NULL   |      | BTREE      |         |               |
+---------------+------------+--------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.01 sec)


mysql> SELECT table_name AS "Tables",  round(((index_length) / 1024 / 1024), 2) SIB  
        FROM information_schema.TABLES  
        WHERE table_schema = "XXXX" and table_name='t_media_items'  
        ORDER BY (index_length ) DESC;

+---------------+------+
| Tables        | SIB  |
+---------------+------+
| t_media_items | 2.52 |
+---------------+------+
1 row in set (0.00 sec)

我试图改变另一列的长度,命名为“rand_key”。这个奇怪的问题是在圆柱体变化后,INDEX尺寸突然增加到5.52MB,即使“rand_key”也不是指数的一部分。

mysql> ALTER TABLE `t_media_items` CHANGE `rand_key` `rand_key` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
Query OK, 108503 rows affected (7.24 sec)
Records: 108503  Duplicates: 0  Warnings: 0

ALTER后面是INDEX_LENGTH

mysql> SELECT table_name AS "Tables",  round(((index_length) / 1024 / 1024), 2) SIB  
FROM information_schema.TABLES  
WHERE table_schema = "tallcat" and table_name='t_media_items'  
ORDER BY (index_length ) DESC;
+---------------+------+
| Tables        | SIB  |
+---------------+------+
| t_media_items | 5.52 |
+---------------+------+
1 row in set (0.00 sec)

有人可以帮我解释一下这个问题吗?谢谢

1 个答案:

答案 0 :(得分:0)

ALTER TABLE执行表重组,该表重组为表,应用alter并将数据复制到表。索引将作为此过程的副产品进行重建。

但是当它在用数据填充表格的同时逐步构建索引时,它并没有利用fast index creation。索引的存储不那么紧凑。

我无法判断这是否解释了尺寸增加超过2倍,但这是可能的。

我假设您使用的是MySQL 5.5或更高版本,或者使用InnoDB插件的5.1。早期版本不支持快速索引创建。