我在一个非常简单的查询时遇到了困难。我已经尝试过使用解释,但我从未发现它非常有用,或者我不太了解它以尝试优化我的查询。下面的配置文件显示发送数据需要4秒,但我发誓查询执行时间总计为10-15秒。如果我将连接取出到设施表,那么查询就可以了。设施表中只有10行。
我的查询
SELECT
bill_payDate AS Date,
facilityName
FROM bill_pay
JOIN bill_invoice ON bill_invID = bill_pay_bill_invID
JOIN facility ON facilityID = bill_inv_facilityID
WHERE
bill_pay.bill_payDate BETWEEN '2014-02-16 00:00:00' AND '2014-02-16 23:59:59'
说明:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE facility index PRIMARY PRIMARY 2 NULL 30 Using index
1 SIMPLE bill_invoice ref PRIMARY,bill_inv_facilityID bill_inv_facilityID 4 sam_arenasports.facility.facilityID 179 Using where
1 SIMPLE bill_pay ref bill_pay_bill_invID,bill_payDate,payinv_date bill_pay_bill_invID 4 sam_arenasports.bill_invoice.bill_invID 1 Using where
资料:
Status Time
starting 0.000050
Waiting for query cache lock 0.000011
checking query cache for query 0.000054
checking permissions 0.000012
checking permissions 0.000010
checking permissions 0.000011
Opening tables 0.000034
System lock 0.000016
Waiting for query cache lock 0.000033
init 0.000031
optimizing 0.000021
statistics 0.000108
preparing 0.000026
executing 0.000011
Sending data 3.852001
end 0.000056
query end 0.000011
closing tables 0.000023
freeing items 0.000018
Waiting for query cache lock 0.000010
freeing items 0.000052
Waiting for query cache lock 0.000011
freeing items 0.000009
storing result in query cache 0.000010
logging slow query 0.000010
logging slow query 0.000073
cleaning up 0.000013
在终端中执行查询....
| 2014-02-16 22:39:14 |
| 2014-02-16 22:39:24 |
| 2014-02-16 22:41:42 |
| 2014-02-16 22:42:16 |
| 2014-02-16 22:50:09 |
| 2014-02-16 22:56:54 |
+---------------------+
1265 rows in set (26.47 sec)
mysql>
设施表,30行
-
facility
CREATE TABLE IF NOT EXISTS `facility` (
`facilityID` smallint(6) NOT NULL AUTO_INCREMENT,
`facilityName` varchar(50) NOT NULL DEFAULT '',
`facilityPhone` varchar(15) NOT NULL DEFAULT '',
`facilityEmail` varchar(50) NOT NULL DEFAULT '',
`facilitywww` varchar(60) DEFAULT NULL,
`facilityNick` varchar(10) NOT NULL DEFAULT '',
`facilityEmailBcastBounce` varchar(40) NOT NULL DEFAULT '',
`facilityAddress1` varchar(50) NOT NULL DEFAULT '',
`facilityAddress2` varchar(50) NOT NULL DEFAULT '',
`facilityCity` varchar(30) NOT NULL DEFAULT '',
`facilityState` char(2) NOT NULL DEFAULT '',
`facilityZip` varchar(10) NOT NULL DEFAULT '',
`facilityCCbypass` char(1) NOT NULL DEFAULT '',
`facility_ccPartner` varchar(32) DEFAULT NULL,
`facility_ccVendor` varchar(64) DEFAULT NULL,
`facility_ccUser` varchar(32) DEFAULT NULL,
`facility_ccPass` varchar(32) DEFAULT NULL,
`facility_ccAcceptedTypes` varchar(100) DEFAULT 'visa,mastercard',
`facilityTZ` varchar(60) DEFAULT 'PST8PDT',
`facilityTaxID` varchar(50) DEFAULT NULL,
`facilityActive` tinyint(4) NOT NULL DEFAULT '1',
`facilityMySAMVisible` tinyint(4) NOT NULL DEFAULT '1',
`facilityMySAMSupport` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`facilityID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 PACK_KEYS=1 AUTO_INCREMENT=31 ;
bill_pay表,140万行:
-
bill_pay
CREATE TABLE IF NOT EXISTS `bill_pay` (
`bill_payID` int(11) NOT NULL AUTO_INCREMENT,
`bill_pay_customerID` int(11) NOT NULL DEFAULT '0',
`bill_pay_bill_invID` int(11) NOT NULL DEFAULT '0',
`bill_payMethod` char(1) NOT NULL DEFAULT '',
`bill_payDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`bill_payAmount` decimal(10,2) NOT NULL DEFAULT '0.00',
`bill_payMethodInfo` varchar(80) DEFAULT NULL,
`bill_paySwipe` tinyint(4) NOT NULL DEFAULT '0',
`bill_payCVV` varchar(2) DEFAULT NULL COMMENT 'Credit card security code response',
`bill_payModU` varchar(8) NOT NULL DEFAULT '',
`bill_payPNRef` varchar(15) NOT NULL DEFAULT '',
`bill_payAuth` varchar(8) NOT NULL DEFAULT '0',
`bill_payCCexp` varchar(4) DEFAULT NULL,
`bill_payExplain` tinytext,
`bill_payCheckType` char(1) DEFAULT NULL,
`bill_payCCType` char(1) DEFAULT NULL,
PRIMARY KEY (`bill_payID`),
KEY `bill_pay_customerID` (`bill_pay_customerID`),
KEY `bill_pay_bill_invID` (`bill_pay_bill_invID`),
KEY `bill_payMethod` (`bill_payMethod`),
KEY `bill_payDate` (`bill_payDate`),
KEY `bill_payPNRef` (`bill_payPNRef`),
KEY `bill_payModU` (`bill_payModU`),
KEY `payinv_date` (`bill_pay_bill_invID`,`bill_payDate`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 PACK_KEYS=1 AUTO_INCREMENT=1390369 ;