表详情 - >
CREATE TABLE `redeem_vouchers` (
`voucherid` int(11) NOT NULL,
`voucheramount` decimal(18,2) DEFAULT NULL,
`redeemedto` int(11) DEFAULT NULL,
`redeemedon` datetime DEFAULT NULL,
`transid` varchar(100) DEFAULT NULL,
`paymentconfirmed` tinyint(1) NOT NULL DEFAULT '0',
`confirmedby` int(11) DEFAULT NULL,
`confirmedon` datetime DEFAULT NULL,
`redeemedby` int(11) DEFAULT '0',
PRIMARY KEY (`voucherid`),
KEY `NewIndex2` (`redeemedto`),
KEY `NewIndex3` (`voucheramount`),
FULLTEXT KEY `NewIndex1` (`transid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
查询 - >
SELECT DISTINCT(transid),
(SELECT (COUNT(voucheramount)*16) FROM redeem_vouchers WHERE transid = r.transid) AS amount,
(SELECT redeemedto FROM redeem_vouchers WHERE transid = r.transid LIMIT 0,1) AS 'Name'
FROM
redeem_vouchers r
WHERE
confirmedon
BETWEEN ('2013-07-23 19:00:00') AND ('2013-07-24 19:00:00')
表只包含4909行,查询返回结果需要35秒。 我添加了4个索引但没有找到速度增益。我知道我正在做一些危险但我是sql的新手。请帮帮我。
答案 0 :(得分:0)
我会尝试使用GROUP BY的查询:
SELECT
transid,
COUNT(voucheramount)*16 AS Amount,
redeemedto As `Name`
FROM
redeem_vouchers
WHERE
confirmedon BETWEEN '2013-07-23 19:00:00' AND '2013-07-24 19:00:00'
GROUP BY
transid
redeemedto是一个非聚合列,其值将不确定,但在原始查询中也未确定,因为您使用LIMIT获取第一条记录,但您没有使用ORDER BY。
如果所有兑换的值都相同,那就没问题 - 或者您可以使用
MAX(redeemedto) As `Name`
使查询成为SQL标准。