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隐式转换造成索引失效的事故总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于bat中使用rar压缩命令
- 下一篇: linux cmake编译源码,linu