I've been migrating one zabbix server installation and I've found a so big database (mysql).
First thing I've done was optimize tables, but big ones (history and history_uint) with something about 110465752 rows failed the process.
So I've created a similar table with autonumeric primary key (last column) and imported one dump
Not only was able to optimize the table, it is so fast compared with the one without primary key. So I want to propose to include primary keys on those tables.
MariaDB [zabbix]> desc history;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| itemid | bigint(20) unsigned | NO | MUL | NULL | |
| clock | int(11) | NO | | 0 | |
| value | double(16,4) | NO | | 0.0000 | |
| ns | int(11) | NO | | 0 | |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
+--------+---------------------+------+-----+---------+----------------+
5 rows in set (0.002 sec)
MariaDB [zabbix]> optimize table history;
+----------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+----------------+----------+----------+-------------------------------------------------------------------+
| zabbix.history | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| zabbix.history | optimize | status | OK |
+----------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (11 min 40.757 sec)
MariaDB [zabbix]> show index from history;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| history | 0 | PRIMARY | 1 | id | A | 110465752 | NULL | NULL | | BTREE | | |
| history | 1 | history_1 | 1 | itemid | A | 293791 | NULL | NULL | | BTREE | | |
| history | 1 | history_1 | 2 | clock | A | 110465752 | NULL | NULL | | BTREE | | |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.002 sec)
MariaDB [zabbix]> select count(*) from history;
+-----------+
| count(*) |
+-----------+
| 110804374 |
+-----------+
1 row in set (1 min 3.972 sec)
MariaDB [zabbix]> drop table history;
Query OK, 0 rows affected (0.038 sec)
MariaDB [zabbix]> rename table historybak to history;
Query OK, 0 rows affected (0.029 sec)
MariaDB [zabbix]> select count(*) from history;
+-----------+
| count(*) |
+-----------+
| 110804374 |
+-----------+
1 row in set (3 min 7.416 sec)
First thing I've done was optimize tables, but big ones (history and history_uint) with something about 110465752 rows failed the process.
So I've created a similar table with autonumeric primary key (last column) and imported one dump
Not only was able to optimize the table, it is so fast compared with the one without primary key. So I want to propose to include primary keys on those tables.
MariaDB [zabbix]> desc history;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| itemid | bigint(20) unsigned | NO | MUL | NULL | |
| clock | int(11) | NO | | 0 | |
| value | double(16,4) | NO | | 0.0000 | |
| ns | int(11) | NO | | 0 | |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
+--------+---------------------+------+-----+---------+----------------+
5 rows in set (0.002 sec)
MariaDB [zabbix]> optimize table history;
+----------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+----------------+----------+----------+-------------------------------------------------------------------+
| zabbix.history | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| zabbix.history | optimize | status | OK |
+----------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (11 min 40.757 sec)
MariaDB [zabbix]> show index from history;
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| history | 0 | PRIMARY | 1 | id | A | 110465752 | NULL | NULL | | BTREE | | |
| history | 1 | history_1 | 1 | itemid | A | 293791 | NULL | NULL | | BTREE | | |
| history | 1 | history_1 | 2 | clock | A | 110465752 | NULL | NULL | | BTREE | | |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.002 sec)
MariaDB [zabbix]> select count(*) from history;
+-----------+
| count(*) |
+-----------+
| 110804374 |
+-----------+
1 row in set (1 min 3.972 sec)
MariaDB [zabbix]> drop table history;
Query OK, 0 rows affected (0.038 sec)
MariaDB [zabbix]> rename table historybak to history;
Query OK, 0 rows affected (0.029 sec)
MariaDB [zabbix]> select count(*) from history;
+-----------+
| count(*) |
+-----------+
| 110804374 |
+-----------+
1 row in set (3 min 7.416 sec)
Comment