标签:myisam btree 索引优化 sql优化 mysql
tbl_direct_pos_201506 表有 190 万数据,DDL:CREATE TABLE `tbl_direct_pos_201506` ( `acq_ins_code` char(13) NOT NULL DEFAULT ‘‘ COMMENT ‘机构代码‘, `trace_num` char(6) NOT NULL DEFAULT ‘‘ COMMENT ‘跟踪号‘, `trans_datetime` char(10) NOT NULL DEFAULT ‘‘ COMMENT ‘交易时间‘, `process_flag` char(1) DEFAULT NULL COMMENT ‘处理标识‘, `rev_flag` char(1) DEFAULT NULL COMMENT ‘接收标识‘, `before_trans_code` char(3) DEFAULT NULL COMMENT ‘交易类型‘, `trans_amt` decimal(15,3) DEFAULT NULL COMMENT ‘交易金额‘, `acct_num` char(21) DEFAULT NULL COMMENT ‘卡号‘, `mer_type` char(4) DEFAULT NULL COMMENT ‘商户类型‘, `recv_ins_code` char(13) DEFAULT NULL COMMENT ‘发卡行代码‘, `retrivl_ref_num` char(12) DEFAULT NULL COMMENT ‘检索参考号‘, `resp_auth_code` char(6) DEFAULT NULL COMMENT ‘授权码‘, `resp_code` char(2) DEFAULT NULL COMMENT ‘应答码‘, `term_id` char(8) DEFAULT NULL COMMENT ‘终端代码‘, `mer_code` char(15) DEFAULT NULL COMMENT ‘商户代码‘, `mer_addr_name` char(40) DEFAULT NULL COMMENT ‘商户名称和地址,前 25 字节是名称,后面是地址‘, `self_define` varchar(300) DEFAULT NULL COMMENT ‘第 259 字节是卡片类型‘, `sys_date` char(8) NOT NULL DEFAULT ‘‘ COMMENT ‘交易日期‘, `sa_sav2` varchar(300) DEFAULT NULL COMMENT ‘第 243 字节是 DCC 标识‘, `rec_create_time` datetime DEFAULT NULL COMMENT ‘联机入库时间‘, `rec_update_time` datetime DEFAULT NULL COMMENT ‘最后修改时间‘, PRIMARY KEY (`sys_date`,`trans_datetime`,`acq_ins_code`,`trace_num`), KEY `idx_direct_pos_create_time` (`rec_create_time`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=‘交易月表模板‘;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | t | range | PRIMARY | PRIMARY | 24 | 299392 | Using index condition; Using filesort | |
1 | SIMPLE | r | eq_ref | PRIMARY | PRIMARY | 41 | msp.t.recv_ins_code | 1 | Using where |
1 | SIMPLE | tt | eq_ref | PRIMARY | PRIMARY | 14 | msp.t.before_trans_code | 1 | Using where |
select substr(t.acq_ins_code, 3) merAcqInsCode, t.mer_code, t.term_id, substr(t.mer_addr_name, 1, 12) merName, tt.trans_desc, t.rev_flag, t.trans_amt, concat(substr(t.sys_date, 1, 4), t.trans_datetime) transTime,t.before_trans_code, t.acct_num, t.retrivl_ref_num, t.resp_code, t.resp_auth_code, r.recv_ins_name,t.acq_ins_code,t.trace_num,t.trans_datetime, case substr(t.sa_sav2,259,1) when 1 then ‘借记卡‘ when 2 then ‘贷记卡‘ when 3 then ‘准贷记卡‘ when 4 then ‘私有预付卡‘ else ‘‘ end cardType, case when locate(‘VIS‘,t.sa_sav2) > 0 then ‘VISA‘ when locate(‘JCB‘,t.sa_sav2) > 0 then ‘JCB‘ when locate(‘DNC‘,t.sa_sav2) > 0 then ‘大莱卡‘ when locate(‘CUP‘,t.sa_sav2) > 0 then ‘银联境内卡‘ when locate(‘UPI‘,t.sa_sav2) > 0 then ‘银联境外卡‘ else ‘‘ end cardBrand from tbl_direct_pos_201506 t left join trans_recv_ins r on r.recv_ins_code = t.recv_ins_code left join tbl_trans_type tt on tt.trans_code = t.before_trans_code where t.sys_date between ‘20150622‘ and ‘20150628‘ order by t.sys_date, t.trans_datetime, t.acq_ins_code, t.trace_num limit 0, 20;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | t | range | PRIMARY | PRIMARY | 24 | 299392 | Using index condition | |
1 | SIMPLE | r | eq_ref | PRIMARY | PRIMARY | 41 | msp.t.recv_ins_code | 1 | Using where |
1 | SIMPLE | tt | eq_ref | PRIMARY | PRIMARY | 14 | msp.t.before_trans_code | 1 | Using where |
select substr(t.acq_ins_code, 3) merAcqInsCode, t.mer_code, t.term_id, substr(t.mer_addr_name, 1, 12) merName, tt.trans_desc, t.rev_flag, t.trans_amt, concat(substr(t.sys_date, 1, 4), t.trans_datetime) transTime,t.before_trans_code, t.acct_num, t.retrivl_ref_num, t.resp_code, t.resp_auth_code, r.recv_ins_name,t.acq_ins_code,t.trace_num,t.trans_datetime, case substr(t.sa_sav2,259,1) when 1 then ‘借记卡‘ when 2 then ‘贷记卡‘ when 3 then ‘准贷记卡‘ when 4 then ‘私有预付卡‘ else ‘‘ end cardType, case when locate(‘VIS‘,t.sa_sav2) > 0 then ‘VISA‘ when locate(‘JCB‘,t.sa_sav2) > 0 then ‘JCB‘ when locate(‘DNC‘,t.sa_sav2) > 0 then ‘大莱卡‘ when locate(‘CUP‘,t.sa_sav2) > 0 then ‘银联境内卡‘ when locate(‘UPI‘,t.sa_sav2) > 0 then ‘银联境外卡‘ else ‘‘ end cardBrand from tbl_direct_pos_201506 t left join trans_recv_ins r on r.recv_ins_code = t.recv_ins_code left join tbl_trans_type tt on tt.trans_code = t.before_trans_code where t.sys_date between ‘20150622‘ and ‘20150628‘ order by t.sys_date desc, t.trans_datetime desc, t.acq_ins_code desc, t.trace_num desc limit 0, 20;
执行之,0.029 s,搞定。
版权声明:本文为博主原创文章,未经博主允许不得转载。
注意使用 BTREE 复合索引各字段的 ASC/DESC 以优化 order by 查询效率
标签:myisam btree 索引优化 sql优化 mysql
原文地址:http://blog.csdn.net/defonds/article/details/46727265