Mysql innodb: Choosing index type for 'greater than >=' select
I'm optimizing an update query:
UPDATE projects SET rgt = rgt + 2 WHERE rgt >= @superRgt;
The table has a lot of rows (~63000), and the query runs slow. When I
explain SELECT * FROM projects WHERE rgt >= @superRgt;
I get this result:
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | projects | ALL | rgt | NULL | NULL | NULL | 69971 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+-------+-------------+
The rgt key is defined as:
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| projects | 1 | rgt | 1 | rgt | A | 72770 | NULL | NULL | | BTREE | 开发者_JAVA百科 |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
I'm wondering if I could optimize the UPDATE query by choosing an index type that can be used with WHERE rgt >= @superRgt
?
How much rows are affected by UPDATE projects SET rgt = rgt + 2 WHERE rgt >= @superRgt
?
If it affects a lot of rows, MySQL will ignore the index defined. These links might be related:
MySQL is not using an index
MySQL datetime index is not working
Try to delete all the indexes at all. Because I'm not actually sure when this index is oing to be updated. During each SET, or after all the chages in the table. And if the answer is - after updating each line, then it is really slow.
I think the only way to speed this up is to check your innodb buffer sizes. Both the disk and ram caching.
A small value may cause the innodb to go to disk to frequently.
精彩评论