indexing - Please Help me to optimise mysql query containing subqueries for just a few hundred records -
table details-->
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;
query-->
select distinct(transid), (select (count(voucheramount)*16) redeem_vouchers transid = r.transid) amount, (select redeemedto redeem_vouchers transid = r.transid limit 0,1) 'name' redeem_vouchers r confirmedon between ('2013-07-23 19:00:00') , ('2013-07-24 19:00:00')
table contains 4909 rows , query taking 35 seconds return result. have added 4 indexes found no speed gain. know doing dangerous new sql. please me.
i try query, uses group by:
select transid, count(voucheramount)*16 amount, redeemedto `name` redeem_vouchers confirmedon between '2013-07-23 19:00:00' , '2013-07-24 19:00:00' group transid
redeemedto non-aggregated column, , value undetermined, undetermined in original query since using limit first record, not using order by.
if redeemedto values same, fine - or can use
max(redeemedto) `name`
to make query sql standard.
Comments
Post a Comment