Skip to content
Advertisement

MYSQL Index strategy when no WHERE Clause

I have this table

I need to run this query on it

It is dog slow and does a full file sort. Normally I know to add an index following the order of a where clause. I have no idea how to do it with this query and this table to avoid the filesort. Any suggestions would be terrific thankyou.

Advertisement

Answer

You can’t create an index on WLANTYPE as it is, because if you try to index a TEXT or BLOB, you get this error:

ERROR 1170 (42000): BLOB/TEXT column ‘wlantype’ used in key specification without a key length

I would question whether you need WLANTYPE to be TEXT. Perhaps a shorter VARCHAR would be just as good.

Then you can add a covering index:

Also get rid of the ORDER BY FileTime so you don’t have to sort the result. Sort the result after fetching the result in your application, if it isn’t already in the order you want.

The type: index in this explain report shows that it still has to scan the whole index, which is nearly as expensive as a table-scan. But that’s natural for your query, which needs to get counts from every row.

The advantage of making this an index scan may be that it has to examine fewer pages. One index, even on 6 columns, is smaller than the whole table.

Also getting rid of the filesort will help.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement