mysql隐形转换造成索引失效的事故总结

   隐式转换导致索引失效.这一点应当引起重视.也是开发中经常会犯的错误. 由于表的字段tu_mdn定义为varchar2(20),但在查询时把该字段作为number类型以where条件传给mysql,这样会导致索引失效. 错误的例子:select * from test where tu_mdn=13333333333; 正确的例子:select * from test where tu_mdn=‘13333333333‘;

   看一下下面的案例,这个案例是开发过程中经常犯的一个错误,这种索引在大表的查询中是很致命的,直接能把数据库拖死:

mysql> show create table shipping\G;

*************************** 1. row ***************************

      Table: shipping

Create Table: CREATE TABLE `shipping` (

 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

 `shipping_no` int(11) NOT NULL,

 PRIMARY KEY (`id`),

 UNIQUE KEY `shipping_no` (`shipping_no`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

mysql> explain SELECT  shipping_no FROM `shipping`  WHERE  `shipping_no`  IN (62487941,62653594,62952180,63556576,63684186,99097538006,100433005006,100433006006);

+----+-------------+----------------------+-------+---------------+-------------+---------+------+------+-------------+

| id | select_type | table                | type  | possible_keys | key         | key_len | ref  | rows | Extra       |

+----+-------------+----------------------+-------+---------------+-------------+---------+------+------+-------------+

|  1 | SIMPLE      | shipping             | range | shipping_no   | shipping_no | 4       | NULL |    6 | Using where |

+----+-------------+----------------------+-------+---------------+-------------+---------+------+------+-------------+

1 row in set (0.00 sec)


mysql> explain SELECT  shipping_no  FROM `shipping`  WHERE  (`shipping_no`  IN (‘62487941‘,‘62653594‘,‘62952180‘,‘63556576‘,‘63684186‘,‘99097538006‘,‘100433005006‘,‘100433006006‘));

+----+-------------+----------------------+------+---------------+------+---------+------+----------+-------------+

| id | select_type | table                | type | possible_keys | key  | key_len | ref  | rows     | Extra       |

+----+-------------+----------------------+------+---------------+------+---------+------+----------+-------------+

|  1 | SIMPLE      | shipping             | ALL  | shipping_no   | NULL | NULL    | NULL | 12803696 | Using where |

+----+-------------+----------------------+------+---------------+------+---------+------+----------+-------------+

1 row in set (0.00 sec)

   很蛋疼的东西,希望开发者在开发的时候注意字段不要越界,最主要的是不要使用隐形转换,有些是转换不了的,DBA的同行们注意这种隐形转换带来的危害,一定要给开发者提供规范。

mysql隐形转换造成索引失效的事故总结,布布扣,bubuko.com

mysql隐形转换造成索引失效的事故总结

上一篇:mysql的权限有哪些


下一篇:Windows下JAVA开发环境配置