below query taking more than 2.5 seconds running from 20lacs records;
SELECT lid,nid ,sum(nview) as view_cnt FROM nv WHERE lid = 1 GROUP BY nid ORDER BY view_cnt desc LIMIT 20;
i updated the possible key as indexes, Please help me to optimize this query
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE nv ref lid lid 4 const 406282 "Using where; Using temporary; Using filesort"
SHOW CREATE TABLE view_cnt
andEXPLAIN SELECT ..(yourquery)..
and include the results in your question. – KaiiORDER BY
clause also seems suspicious, you probably want toORDER BY view_cnt
instead? – Romainnid
are there for alid
on average? TheORDER BY
clause requires thefilesort
operation, which is super-slow, if you have too many rows to sort. – Romain