I am updating the MySQL table (myisam mechanism), which, according to the profiler, spends an excessive amount of time in the "init" state:
mysql> show profile for query 2;
+----------------------+-----------+
| Status | Duration |
+----------------------+-----------+
| starting | 0.000057 |
| checking permissions | 0.000006 |
| Opening tables | 0.000020 |
| System lock | 0.000007 |
| Table lock | 0.000005 |
| init | 21.911657 |
| Updating | 0.002363 |
| end | 0.000009 |
| query end | 0.000004 |
| freeing items | 0.000051 |
| logging slow query | 0.000003 |
| logging slow query | 0.000002 |
| cleaning up | 0.000005 |
+----------------------+-----------+
The request is as follows:
mysql> update my_table
-> set rank =
-> greatest(
-> @rank := if(@score = score, @rank, @rank + 1),
-> least(0, @score := score)
-> )
-> where game=7 and zone=11 and ladder=2
-> order by score
-> limit 100;
Query OK, 100 rows affected (21.92 sec)
Rows matched: 100 Changed: 100 Warnings: 0
I have a composite index for all columns listed in the where and order clauses (see the index named zone_lad_score below):
mysql> show indexes from my_table;
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| my_table | 1 | indx_e | 1 | col_e | A | 2937401 | NULL | NULL | | BTREE | |
| my_table | 1 | zone_score | 1 | zone | A | 217 | NULL | NULL | | BTREE | |
| my_table | 1 | zone_score | 2 | score | A | 23499213 | NULL | NULL | YES | BTREE | |
| my_table | 1 | zone_d_score | 1 | zone | A | 217 | NULL | NULL | | BTREE | |
| my_table | 1 | zone_d_score | 2 | col_d | A | 123355 | NULL | NULL | YES | BTREE | |
| my_table | 1 | zone_d_score | 3 | score | A | 46998427 | NULL | NULL | YES | BTREE | |
| my_table | 1 | zone_lad_score | 1 | zone | A | 217 | NULL | NULL | | BTREE | |
| my_table | 1 | zone_lad_score | 2 | ladder | A | 868 | NULL | NULL | YES | BTREE | |
| my_table | 1 | zone_lad_score | 3 | score | A | 23499213 | NULL | NULL | YES | BTREE | |
+--------------------+------------+-----------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
I also have a table divided into a βgameβ, with a total of 10 sections. The table contains about 47 million records. The table definition is as follows:
my_table | CREATE TABLE `my_table` (
`col_e` bigint(20) NOT NULL,
`zone` bigint(20) NOT NULL,
`score` int(11) DEFAULT NULL,
`game` tinyint(4) DEFAULT NULL,
`ladder` tinyint(4) DEFAULT NULL,
`col_d` int(11) DEFAULT NULL,
`rank` int(11) DEFAULT NULL,
KEY `indx_e` (`col_e`),
KEY `zone_score` (`zone`,`score`),
KEY `zone_d_score` (`zone`,`col_d`,`score`),
KEY `zone_lad_score` (`zone`,`ladder`,`score`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
, MySQL (http://dev.mysql.com/doc/refman/5.0/en/general-thread-states.html), "init" " , InnoDB ." ... , InnoDB, , .
, , , , 100 , ? "init" ? ( * my_table, game = 7 zone = 11 ladder = 2 order by score limit 100), . ( zone_d_score index) . ?
: , , .
2: :
mysql> explain select * from my_table where game=7 and zone=11 and ladder=2 order by score limit 100;
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
| 1 | SIMPLE | my_table | ref | zone_score,zone_d_score,zone_lad_score | zone_lad_score | 10 | const,const | 53952 | Using where |
+----+-------------+--------------------+------+------------------------------------------------+-----------------+---------+-------------+-------+-------------+
1 row in set (0.00 sec)