亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 821 | 回復(fù): 0
打印 上一主題 下一主題

mysql 索引優(yōu)化一例 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-12-21 08:42 |只看該作者 |倒序?yàn)g覽
在實(shí)際工作中遇到的問題。smw_namespace沒有索引,但是對(duì)它做了表分區(qū)。
索引信息如下:
  1. mysql> show index from smw_ids;
  2. +---------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
  3. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
  4. +---------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
  5. | smw_ids | 0 | PRIMARY | 1 | smw_id | A | 1284866 | NULL | NULL | | BTREE | |
  6. | smw_ids | 0 | PRIMARY | 2 | smw_namespace | A | 1284866 | NULL | NULL | | BTREE | |
  7. | smw_ids | 1 | smw_title | 1 | smw_title | A | 1284866 | 50 | NULL | | BTREE | |
  8. | smw_ids | 1 | idx_smw_sortkey | 1 | smw_sortkey | A | 1284866 | NULL | NULL | | BTREE | |
  9. +---------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
  10. 4 rows in set (0.57 sec)
查詢?nèi)缦拢?br>
  1. mysql> select /* twGetTermTotalCount 127.0.0.1 */ count(*) AS amount from smw_ids where smw_namespace between 1000 and 2000 and smw_namespace mod 2=0;
  2.     +---------+
  3.     | amount |
  4.     +---------+
  5.     | 1271240 |
  6.     +---------+
  7.     1 row in set (1.94 sec)
EXPLAIN信息如下:
  1. mysql> explain select /* twGetTermTotalCount 127.0.0.1 */ count(*) AS amount from smw_ids where smw_namespace between 1000 and 2000 and smw_namespace mod 2=0;
  2. +----+-------------+---------+-------+---------------+-----------------+---------+------+---------+--------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+---------+-------+---------------+-----------------+---------+------+---------+--------------------------+
  5. | 1 | SIMPLE | smw_ids | index | NULL | idx_smw_sortkey | 257 | NULL | 1274034 | Using where; Using index |
  6. +----+-------------+---------+-------+---------------+-----------------+---------+------+---------+--------------------------+
  7. 1 row in set (0.00 sec)
莫名其妙,為什么會(huì)用到idx_smw_sortkey這個(gè)索引??忽略掉這個(gè)索引看看。
  1. mysql> explain select /* twGetTermTotalCount 127.0.0.1 */ count(*) AS amount from smw_ids IGNORE INDEX(idx_smw_sortkey) where smw_namespace between 1000 and 2000 and smw_namespace mod 2=0;
  2. +----+-------------+---------+-------+---------------+---------+---------+------+---------+--------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+---------+-------+---------------+---------+---------+------+---------+--------------------------+
  5. | 1 | SIMPLE | smw_ids | index | NULL | PRIMARY | 8 | NULL | 1274034 | Using where; Using index |
  6. +----+-------------+---------+-------+---------------+---------+---------+------+---------+--------------------------+
  7. 1 row in set (0.00 sec)
這時(shí)候用到了主索引。因?yàn)閷?duì)smw_namespace做了分區(qū),所以它包含在主索引中。
查詢效率提高不少
  1. mysql> select /* twGetTermTotalCount 127.0.0.1 */ count(*) AS amount from smw_ids IGNORE INDEX(idx_smw_sortkey) where smw_namespace between 1000 and 2000 and smw_namespace mod 2=0;
  2. +---------+
  3. | amount |
  4. +---------+
  5. | 1271208 |
  6. +---------+
  7. 1 row in set (0.77 sec)
mysql 自己的優(yōu)化器某些時(shí)候還是不那么“聰明的”,優(yōu)化的時(shí)候要多思考。








您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP