Telling MySQL Which Index to Use
**Disclamer: ** I'm sure MySQL database administrators will tell me that this is SUPER wrong because I should always let the database engine pick what indexes to use but this feature exists for a reason. That being said I'm NOT a database administrator I'm just a programmer that plays one on TV.
The other day we ran into an issue where a page would take about 60 seconds to rendering and the problem was tracked back to a single query. Lets say it looked like something like this:
SELECT *
FROM Table1
INNER JOIN Table2 ON Table2.id = Table1.Table2Id
WHERE Table2.column = 'true';
When I ran explain
on the query I got the following:
I've never seen it use intersection before and I had a index set for all the columns I was using. In MySQL you can force it to use an index:
SELECT *
FROM column
INNER JOIN Table2 USE INDEX (column_idx) ON Table2.id = Table1.Table2Id
WHERE Table2.column = 'true';
This simple change took the query from 60 seconds to 0.5.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows