Mysql的强制索引(Force Index)都为我们做了哪些优化?_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2282 | 回复: 1   主题: Mysql的强制索引(Force Index)都为我们做了哪些优化?        下一篇 
landy
注册用户
等级:新兵
经验:61
发帖:72
精华:0
注册:2011-12-31
状态:离线
发送短消息息给landy 加好友    发送短消息息给landy 发消息
发表于: IP:您无权察看 2015-12-18 16:41:26 | [全部帖] [楼主帖] 楼主

原本只是想验证一下选择不同索引对innodb count(*)查询速度的影响。

测试过程中没想到同样的一条sql语句仅仅是增加了force index后查询速度几乎快了一倍。

select count(*) from http_log_3 force index(time) where time >= 000000    //1 row in set (11 min 19.35 sec)
select count(*) from http_log_3 where time >= 000000    //1 row in set (20 min 5.86 sec)


但实际上通过explain分析可知其实这两条sql语句使用的都是time索引,完全一样!

在这个特例当中使用force index(time)后影响的并不是索引key的选择(优化器默认也使用time索引),而是type及rows.

很想知道这是为什么,rows是如何被估算出来的,可有公式?

测试环境:

数据库 mysql 5.1.34,innodb引擎,使用innodb_file_per_table选项。

使用表分区方式创建数据表(按日分区共十个),表中一共有5000万数据,即每个分区各500万。

测试输出:

--------------
explain partitions select count(*) from http_log_3 force index(time) where time >= 000000
--------------+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
| id | select_type | table      | partitions                        | type  | possible_keys | key  | key_len | ref  | rows     | Extra                    |
+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
|  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | range | time          | time | 3       | NULL | 25000141 | Using where; Using index |
 +----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
 1 row in set (0.01 sec)
 --------------
 explain partitions select count(*) from http_log_3 where time >= 000000
 --------------+----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
 | id | select_type | table      | partitions                        | type  | possible_keys            | key  | key_len | ref  | rows     | Extra                    |
 +----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
 |  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | index | time_ip,time_domain,time | time | 3       | NULL | 50000291 | Using where; Using index |
  +----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
  1 row in set (0.01 sec)
  --------------
  explain partitions select count(*) from http_log_3
  --------------+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
  | id | select_type | table      | partitions                        | type  | possible_keys | key  | key_len | ref  | rows     | Extra       |+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
  |  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | index | NULL          | time | 3       | NULL | 50000291 | Using index | 
  +----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
  1 row in set (0.00 sec)
  --------------
  select count(*) from http_log_3 force index(time) where time >= 000000
  --------------+----------+
  | count(*) |
  +----------+
  | 50000000 |
   +----------+
   1 row in set (11 min 19.35 sec)
   --------------
   select count(*) from http_log_3 where time >= 000000
   --------------+----------+
   | count(*) |
   +----------+
   | 50000000 |
    +----------+
    1 row in set (20 min 5.86 sec)
    --------------
    select count(*) from http_log_3--------------+----------+
    | count(*) |
    +----------+
    | 50000000 | 
    +----------+
    1 row in set (20 min 6.32 sec)













赞(0)    操作        顶端 
水云间山水
注册用户
等级:中士
经验:223
发帖:2
精华:0
注册:2016-2-17
状态:离线
发送短消息息给水云间山水 加好友    发送短消息息给水云间山水 发消息
发表于: IP:您无权察看 2016-3-24 17:51:59 | [全部帖] [楼主帖] 2  楼

学习到了



赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论