I have the following InnoDB table:
+-----------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| doc_id | char(32) | NO | | NULL | |
| staff | char(18) | NO | | NULL | |
| timestamp | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
+-----------+-----------+------+-----+-------------------+----------------+
With these keys:
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| staff_online | 0 | PRIMARY | 1 | id | A | 277350 | NULL | NULL | | BTREE | |
| staff_online | 1 | timestamp | 1 | timestamp | A | 277350 | NULL | NULL | | BTREE | |
| staff_online | 1 | staff_timestamp | 1 | timestamp | A | 277350 | NULL | NULL | | BTREE | |
| staff_online | 1 | staff_timestamp | 2 | staff | A | 277350 | NULL | NULL | | BTREE | |
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
I just noticed that in mysql-slow.log
I sometimes have an INSERT query on this table which takes more than 1 second
INSERT INTO `staff_online` (`doc_id`, `staff`, `timestamp`) VALUES ('150b60a0ab8c5888bdbbb80bd8b7f8a2', 'asia', '2011-01-29 16:52:54')
I'm really puzzled why it takes so long. How can I speed it up?
BTW: Each day there're ~80 slow INSERTS and 40 slow UPDATES like this.
timestamp
an another bytimestamp, staff
. The latter is sufficient to search bytimestamp
, you can drop the former.