MySQL隐式类型转换导致索引失效
今天发现一个问题,where条件的列上明明有索引,但是执行计划还是走全表扫描
创新互联科技有限公司专业互联网基础服务商,为您提供绵阳主机托管,高防主机,成都IDC机房托管,成都主机托管等互联网服务。
MySQL> explain select task_id FROM mostop_xiaodai_collection_call_auto WHERE task_id = 433423882127424 AND task_data_id = 27739131157286912; +----+-------------+-------------------------------------+------------+------+------------------------------+------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------------------------------------+------------+------+------------------------------+------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | mostop_xiaodai_collection_call_auto | NULL | ALL | IDX_task_id,IDX_task_data_id | NULL | NULL | NULL | 3101134 | 1.00 | Using where | +----+-------------+-------------------------------------+------------+------+------------------------------+------+---------+------+---------+----------+-------------+ 1 row in set, 5 warnings (0.00 sec)
查看表结构
mysql> show create table mostop_xiaodai_collection_call_auto\G *************************** 1. row *************************** Table: mostop_xiaodai_collection_call_auto Create Table: CREATE TABLE `mostop_xiaodai_collection_call_auto` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号', `agentid` int(10) unsigned NOT NULL COMMENT '渠道 ID', `loan_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '借款 ID', `user_id` bigint(20) unsigned NOT NULL COMMENT '借款人 ID', `call_mobile` varchar(20) NOT NULL COMMENT '手机号', `call_name` varchar(20) NOT NULL COMMENT '名称', `call_sex` varchar(20) NOT NULL COMMENT '性别', `call_due_date` date NOT NULL COMMENT '应还日期', `call_overdue_day` int(10) NOT NULL COMMENT '逾期天数(负数为贷前催告的天数)', `call_talking_type` varchar(50) NOT NULL COMMENT '话术 (A:贷前7天的提醒话术 B:贷前3天的话术 C:贷前3天的话术 D:贷前1天的话术 Ps:若话术为空,则使用的是大唐的话术,其余类似due_one之类的均已废弃)', `call_platform_name` varchar(100) NOT NULL COMMENT '平台名称', `third_party_system` tinyint(3) unsigned NOT NULL COMMENT '第三方IVR系统,1 = 百可录(暂停使用),2 = 大唐,3 = 云电帮(暂停使用), 4 = 科大讯飞, 5 = 基立讯,99 = 决策引擎测试', `send_time` datetime DEFAULT NULL COMMENT '发送时间', `send_status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '发送状态,0 = 未发送,1 = 发送成功,2 = 发送失败', `send_result` tinyint(3) DEFAULT '0' COMMENT 'IVR结果( 0 = 代表无效接听,或者未接听,1 = 有效接听,已通知用户)', `talking_time` int(10) NOT NULL DEFAULT '0' COMMENT '通话时长', `task_id` varchar(32) NOT NULL DEFAULT '0' COMMENT '外呼任务ID (只为科大讯飞服务)', `task_data_id` varchar(32) NOT NULL DEFAULT '0' COMMENT '外呼任务二级ID (只为科大讯飞服务)', `resend_num` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '重发次数', `receive_time` datetime DEFAULT NULL COMMENT '接收时间', `receive_content` text COMMENT '接收内容', `unique_tab` varchar(100) DEFAULT NULL COMMENT '唯一标识', `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`), KEY `loan_id` (`agentid`,`loan_id`), KEY `send_result` (`call_mobile`,`send_time`), KEY `receive_time` (`receive_time`), KEY `IDX_task_id` (`task_id`), KEY `IDX_task_data_id` (`task_data_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5292233 DEFAULT CHARSET=utf8 COMMENT='催收自动通话表' 1 row in set (0.00 sec)
查看warnings
因为这两列为字符串类型 ,但是SQL中并没有加引号!!
`task_id` varchar(32) `task_data_id` varchar(32)
加上引号后查看执行计划
mysql> explain select task_id FROM mostop_xiaodai_collection_call_auto WHERE task_id = '433423882127424' AND task_data_id =' 27739131157286912'; +----+-------------+-------------------------------------+------------+------+------------------------------+------------------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------------------------------------+------------+------+------------------------------+------------------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | mostop_xiaodai_collection_call_auto | NULL | ref | IDX_task_id,IDX_task_data_id | IDX_task_data_id | 98 | const | 1 | 5.00 | Using where | +----+-------------+-------------------------------------+------------+------+------------------------------+------------------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
新闻名称:MySQL隐式类型转换导致索引失效
文章出自:http://azwzsj.com/article/ihpsji.html