I have created an sql table where I have indexed files on disk.
There are over 1 million records on the table.
I have added indexes for ext and size, but it still takes over a minute to execute this query which tells me the amount of space used by ext.
How can I improve performance on this select?
select ext,
ROUND((sum(size) / (1073741824))) as TotalSizeGB,
count(*) as Count
from fileindex
group by ext
order by TotalSizeGB desc;
Explain output:
|| *id* || *select_type* || *table* || *type* || *possible_keys* || *key* || *key_len* || *ref* || *rows* || *Extra* ||
|| 1 || SIMPLE || fileindex || index || _NULL_ || ext || 27 || _NULL_ || 1892234 || Using index; Using temporary; Using filesort ||
The query as written is always going to hit every row in the table – so there really is a limit to how quickly it can perform. If you really want this result to be something returned quickly, you might want to add another table to keep the total size of each ext, and update it with triggers whenever an operation takes place on your main table.