简体   繁体   中英

Index using temporary and filesort

i have this query :

SELECT date(DATE_SAISIE) , count(DATE_SAISIE) as total 
FROM appel_offre 
GROUP BY date(DATE_SAISIE) 
ORDER BY DATE_SAISIE

I have index on DATE_SAISIE

create index mi_date on appel_offre(DATE_SAISIE)

and this is explain cmd :

+----+-------------+-------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref  | rows | Extra                                        |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
|  1 | SIMPLE      | appel_offre | index | mi_date       | mi_date | 6       | NULL |   25 | Using index; Using temporary; Using filesort |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+----------------------------------------------+

in the column extra there is Using temporary; Using filesort Using temporary; Using filesort so I think the query will be slow, How to avoid it ?

  • using temporary means that MySQL has to create a temporary table for the purpose of generating your results. This is because you're grouping by a function on a column. If you were simply outputting the result of a function on a column, MySQL would not have to remember that value, but because you want to group all records with that function result together, it has to store those results somewhere.

  • using filesort is an amazingly poorly-named indication that MySQL will need to sort some temporary storage in order to generate your results. This is because you're ordering by a function on a column that is in the index rather than by the column itself.

One way to get rid of the filesort, or at least to make it faster, would be to order by the same thing you're selecting:

SELECT date(DATE_SAISIE), count(*) as total 
FROM appel_offre 
GROUP BY date(DATE_SAISIE) 
ORDER BY date(DATE_SAISIE)

This will give exactly the same result as your query but might allow MySQL to not have to remember the DATE_SAISIE values themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM