I have the following SQL query:
select expr1, operator, expr2, count(*) as c
from log_keyword_fulltext
group by expr1, operator, expr2
order by c desc limit 2000;
Problem: The count(*)
as part of my order by is killing my application, probably because it don't use index. I would like to know if there is any way to make it faster, like for example a select
inside of another select
, or something like that.
My SELECT
explained:
+----+-------------+----------------------+-------+---------------+-------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+-------+---------------+-------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | log_keyword_fulltext | index | NULL | expr1 | 208 | NULL | 110000 | Using index; Using temporary; Using filesort |
+----+-------------+----------------------+-------+---------------+-------+---------+------+--------+----------------------------------------------+
UPDATED:
I tried to do a subquery like that
select * from (select b.expr1,b.operator,b.expr2,count(*) as c
from log_keyword_fulltext b group by b.expr1,b.operator,b.expr2) x
order by x.c desc limit 2000;
its working but not faster, following is the explain:
+----+-------------+------------+-------+---------------+-------+---------+------+--------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+-------+---------+------+--------+----------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 38398 | Using filesort |
| 2 | DERIVED | b | index | NULL | expr1 | 208 | NULL | 110000 | Using index |
+----+-------------+------------+-------+---------------+-------+---------+------+--------+----------------+
You can check that now, its not using temporary anymore, but it still with the same performance. any recommendation ?