Hacking with PHP has been updated for PHP 7 - click here! >>

Make sure your indexes are being used

When using EXPLAIN, MySQL will tell you what indexes were available to help it perform its query. As mentioned at the start of this chapter, optimisation is the process of improving your code, and may well not be perfect - the MySQL query optimiser tries its best to get the most optimised version of your query, but sometimes it does slip up and fail to use an index.

If this happens, you can provide a hint to MySQL by using the USE INDEX clause - you can specify which fields you would like MySQL to use, and it will honour that.

For example:

SELECT * FROM mytable USE INDEX (Name) WHERE Name = 'Joe' ;

Naturally MySQL would not fail to optimise that query properly as it is so simple, but you get the idea.


If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Normalisation >>

Previous chapter: Index your data

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.