【发布时间】:2018-08-01 12:21:23
【问题描述】:
我在一个有 445 万行的表上运行下面的查询,并且查询需要 15-20 多分钟才能完成操作。我也尝试将引擎从 Innodb 更改为 MyISAM,但没有任何效果。我也尝试过添加多个类型为 normal 和 unique 的索引,但它仍然需要相同的时间。
这是我的查询:
SELECT
a.source, a.destination, a.forward_to, a.start_epoch, a.end_epoch, a.duration, a.billsec, a.outbound_billsec, a.pool_id, a.group_id, a.cost, a.outbound_cost, a.net, a.keep, a.payin, a.payout, a.campaign_id, a.buyer, a.hangup_cause, a.endpoint_disposition, a.uuid, a.agreement, a.agreement_type, a.contract, a.contract_type, a.sip_received_ip,a.termination_ip,
REPLACE(REPLACE(ifnull(b.line_type,''),'\n',' '),'\r',' ') AS line_type,
REPLACE(REPLACE(ifnull(b.ocn,''),'\n',' '),'\r',' ') AS ocn,
REPLACE(REPLACE(ifnull(b.spid_carrier_name,''),'\n',' '),'\r',' ') AS spid_carrier_name
INTO OUTFILE '/tmp/test-husnain01'
FIELDS TERMINATED BY ',' FROM inbound_022018 a
LEFT JOIN wireless_checks b ON (a.uuid = b.uuid)
WHERE date(a.start_epoch)='2018-02-19' AND
a.endpoint_disposition='ANSWER' AND
a.direction='inbound' AND
a.billed=1;
下面是我的表结构(inbound_022018):
CREATE TABLE `inbound_022018` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`destination` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`prefix` int(22) NOT NULL,
`forward_to` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`supplier` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`agreement` int(11) NOT NULL,
`agreement_type` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`payout` float(11,4) NOT NULL,
`pool_id` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`campaign_id` bigint(22) NOT NULL,
`lead` int(1) NOT NULL,
`cpl` float(11,4) NOT NULL,
`buyer` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`contract` int(11) NOT NULL,
`contract_type` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`payin` float(11,4) NOT NULL,
`gross` float(11,4) NOT NULL,
`cost` float(11,4) NOT NULL,
`outbound_cost` float(11,4) NOT NULL,
`net` float(11,4) NOT NULL,
`keep` float(11,4) NOT NULL,
`direction` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`session_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`uuid` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`sip_from_uri` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`sip_received_ip` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`domain_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`sip_req_uri` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`endpoint_disposition` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`hangup_cause` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`hangup_cause_q850` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`start_epoch` datetime DEFAULT NULL,
`answer_epoch` datetime DEFAULT NULL,
`bridge_epoch` datetime DEFAULT NULL,
`progress_epoch` datetime DEFAULT NULL,
`progress_media_epoch` datetime NOT NULL,
`end_epoch` datetime NOT NULL,
`digits_dialed` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_app` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_arg` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`duration` int(11) NOT NULL,
`g30` int(1) DEFAULT NULL,
`billsec` int(11) NOT NULL,
`outbound_duration` int(11) NOT NULL,
`outbound_billsec` int(11) NOT NULL,
`progresssec` int(11) NOT NULL,
`answersec` int(11) NOT NULL,
`waitsec` int(11) NOT NULL,
`progress_mediasec` int(11) NOT NULL,
`flow_billsec` int(11) NOT NULL,
`sip_hangup_disposition` int(11) NOT NULL,
`callForwarded` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`forwardUuid` varchar(40) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`call_type` enum('s','v') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 's',
`billed` int(1) NOT NULL,
`uc` int(1) NOT NULL,
`suc` int(1) NOT NULL,
`callinfo` varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`termination_ip` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`switchname` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`org_charges` float(11,4) NOT NULL,
`call_summary` text,
PRIMARY KEY (`id`),
UNIQUE KEY `index_inbound_0717` (`id`) USING BTREE,
UNIQUE KEY `index_uuid` (`uuid`) USING BTREE,
UNIQUE KEY `index_all` (`id`,`campaign_id`,`session_id`,`uuid`) USING BTREE,
KEY `index_source` (`source`) USING BTREE,
KEY `index_destination` (`destination`) USING BTREE,
KEY `index_endpoint` (`endpoint_disposition`) USING BTREE,
KEY `index_build` (`billed`) USING BTREE,
KEY `index_campainid` (`campaign_id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=4457485 DEFAULT CHARSET=latin1
这是第二个表(wireless_checks):
CREATE TABLE `wireless_checks` (
`id` int(22) NOT NULL AUTO_INCREMENT,
`date` varchar(10) NOT NULL,
`uuid` varchar(100) NOT NULL,
`tn` varchar(11) NOT NULL,
`lrn` varchar(11) NOT NULL,
`ported_status` varchar(2) NOT NULL,
`ported_date` varchar(11) NOT NULL,
`ocn` varchar(10) NOT NULL,
`line_type` int(1) NOT NULL,
`spid` varchar(10) NOT NULL,
`spid_carrier_name` varchar(100) NOT NULL,
`spid_carrier_type` varchar(10) NOT NULL,
`altspid_carrier_name` varchar(10) NOT NULL,
`altspid_carrier_type` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_uuid` (`uuid`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=36175 DEFAULT CHARSET=latin1
请指导我如何优化此查询以减少执行时间。如果有任何其他方法可以完成这项工作,我也愿意接受解决方法。任何帮助将不胜感激。
谢谢
侯赛因
【问题讨论】:
-
考虑了解和使用
EXPLAIN。
标签: mysql innodb myisam into-outfile