我有三张桌子
DGRS TABLE SPVS TABLE LOCS TABLE
------------------ ------------ ---------------------
dgr_date sid (PK) mloc (PK)
mc_loc (fk:locs:mloc) sname spid (fk:spvs:sid)
ws state
daily_gen
下面的mysql查询花了7分钟(在mysql终端上)返回结果并挂在phpmyadmin中:
select s.sname, sum(d.daily_gen)
from spvs as s, dgrs as d, locs as l
where s.sid=l.spid and l.mloc=d.mc_loc and d.dgr_date='2014-11-30'
group by s.sname;
解释上述查询结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s ALL PRIMARY NULL NULL NULL 20 Using temporary; Using filesort
1 SIMPLE l ref PRIMARY,fk_spid fk_spid 4 oms.s.sid 9 Using index
1 SIMPLE d ref fk_mcloc fk_mcloc 102 oms.l.mloc 485 Using where
如何优化查询? DGRS表有0.5万条记录。