- 論壇徽章:
- 0
|
本帖最后由 wangjj20 于 2012-12-25 15:33 編輯
有一個表,有2276411條數(shù)據(jù)。索引如下:- root@phpcmsv9 10:25>SHOW INDEX FROM test;
- +-----------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
- | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
- +-----------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
- | test | 0 | PRIMARY | 1 | id | A | 2266380 | NULL | NULL | | BTREE | |
- | test | 0 | PRIMARY | 2 | downtime | A | 2266380 | NULL | NULL | | BTREE | |
- | test | 1 | userid | 1 | userid | A | 90655 | NULL | NULL | YES | BTREE | |
- | test | 1 | userid | 2 | catid | A | 755460 | NULL | NULL | YES | BTREE | |
- | test | 1 | userid | 3 | contentid | A | 2266380 | NULL | NULL | YES | BTREE | |
- | test | 1 | ucenteruid | 1 | ucenteruid | A | 125910 | NULL | NULL | | BTREE | |
- | test | 1 | idx_contentid_catid_time | 1 | contentid | A | 103017 | NULL | NULL | YES | BTREE | |
- | test | 1 | idx_contentid_catid_time | 2 | catid | A | 103017 | NULL | NULL | YES | BTREE | |
- | test | 1 | idx_contentid_catid_time | 3 | downtime | A | 2266380 | NULL | NULL | | BTREE | |
- | test | 1 | idx_downtime_contentid | 1 | downtime | A | 2266380 | NULL | NULL | | BTREE | |
- | test | 1 | idx_downtime_contentid | 2 | contentid | A | 2266380 | NULL | NULL | YES | BTREE | |
- +-----------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
復制代碼 執(zhí)行如下sql:- EXPLAIN
- SELECT SQL_NO_CACHE contentid,COUNT(contentid) AS downloadnum FROM test WHERE modelid=14 AND catid IN(248,250,251,330,331,249,252)
- AND downtime<=1355814270 AND downtime>=1355209470 GROUP BY contentid ORDER BY downloadnum DESC,downtime DESC LIMIT 0,5;
- +----+-------------+-----------------+-------+------------------------+--------------------------+---------+------+---------+----------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-----------------+-------+------------------------+--------------------------+---------+------+---------+----------------------------------------------+
- | 1 | SIMPLE | test | index | idx_downtime_contentid | idx_contentid_catid_time | 14 | NULL | 2266380 | Using where; Using temporary; Using filesort |
- +----+-------------+-----------------+-------+------------------------+--------------------------+---------+------+---------+----------------------------------------------+
復制代碼 這樣效率實在太低,執(zhí)行時間需要好幾秒。索引中的idx_downtime_contentid是為了優(yōu)化這條SQL加的。但是優(yōu)化器默認選擇了 idx_contentid_catid_time 這條索引。
force index(idx_downtime_contentid)這樣效率提高很多,但是有沒有辦法在不改動索引,不添加force index的情況下,能用到idx_downtime_contentid索引。- EXPLAIN
- SELECT SQL_NO_CACHE contentid,COUNT(contentid) AS downloadnum FROM test FORCE INDEX(idx_downtime_contentid) WHERE modelid=14 AND catid IN(248,250,251,330,331,249,252)
- AND downtime<=1355814270 AND downtime>=1355209470 GROUP BY contentid ORDER BY downloadnum DESC,downtime DESC LIMIT 0,5;
- +----+-------------+-----------------+-------+------------------------+------------------------+---------+------+--------+----------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-----------------+-------+------------------------+------------------------+---------+------+--------+----------------------------------------------+
- | 1 | SIMPLE | test | range | idx_downtime_contentid | idx_downtime_contentid | 4 | NULL | 186136 | Using where; Using temporary; Using filesort |
- +----+-------------+-----------------+-------+------------------------+------------------------+---------+------+--------+----------------------------------------------+
復制代碼 |
|