查询计划成本混乱

时间:2015-11-09 13:47:07

标签: mysql sql-execution-plan query-planner

以下具有不同限制计数的查询,制定不同的查询计划和非常不同的扫描行:

mysql> explain SELECT * FROM `funds_cash_apply` WHERE 1 and `id` > '0' and `channel` in ('70','81') and `state` = '1' and `state_pay` = '0' and `create_time` < '1447063200' order by id asc limit 50 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: funds_cash_apply
         type: range
possible_keys: PRIMARY,idx_state_state_e
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 373619
        Extra: Using where
1 row in set (0.00 sec)

mysql> explain SELECT * FROM `funds_cash_apply` WHERE 1 and `id` > '0' and `channel` in ('70','81') and `state` = '1' and `state_pay` = '0' and `create_time` < '1447063200' order by id asc limit 5000 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: funds_cash_apply
         type: ref
possible_keys: PRIMARY,idx_state_state_e
          key: idx_state_state_e
      key_len: 1
          ref: const
         rows: 6652
        Extra: Using index condition; Using where; Using filesort
1 row in set (0.00 sec)

包含索引的表:

  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_water_no` (`water_no`,`state`) USING BTREE,
  KEY `idx_kdt_id` (`kdt_id`,`state`,`create_time`) USING BTREE,
  KEY `idx_batch_id` (`batch_water_no`,`state`) USING BTREE,
  KEY `idx_cp_uid` (`cp_uid`,`state`,`create_time`),
  KEY `idx_acct_no` (`acct_no`,`state`,`create_time`),
  KEY `idx_state_state_e` (`state`,`state_exception`) USING BTREE

为什么不同的限制(50 vs 500)会产生不同的查询计划,扫描行有很大的变化?

0 个答案:

没有答案