database - MySQL query takes ages to return on a huge table -
i have big table , following code takes 990 sec. complete. bdate
, itype
indexed. else need optimize/change?
select s, count(*) total `mt_ex_15` bdate > '2014-10-01' , bdate < '2014-11-01' , itype = '3' group s order total desc
edit: here explain
id select_type table type possible_keys key key_len ref rows 1 simple mt_ex_15 ref itype,bdate,s itype 2 const 44157686 using where; using temporary; using filesort
edit: think need optimize db or my.cnf because following query took 40 secs.
select count(*) total `mt_ex_15` bdate > '2015-02-01' , bdate < '2015-03-01'
and here explain:
id select_type table type possible_keys key key_len ref rows 1 simple mt_ex_15 range bdate bdate 3 null 4494019 using where; using index
for query:
select s, count(*) total `mt_ex_15` bdate > '2014-10-01' , bdate < '2014-11-01' , itype = '3' group s order total desc
the best index mt_ex_15(itype, bdate, s)
. engine should able take full advantage of index where
clause. in addition, covering index original data not need touched query.
if had list of available "s" values, correlated subquery:
select s.*, (select count(*) mt_ex_15 m m.s = s.s , m.itype = 3 , m.bdate > '2014-10-01' , m.bdate < '2014-11-01' ) total s having total > 0 -- using convenient mysql extension order total desc;
the best index query mt_ex_15(s, itype, bdate)
.
note: if itype
integer, should remove quotes around constant. misleading.
Comments
Post a Comment