Forcing a query to use an index
Often, we think we know better than the database optimizer. Most of the time, your expectations are wrong, and if you look carefully, you’ll see that. So, recheck everything and come back later.
It is a classic error to try to get the database optimizer to use indexes when the database has very little data in it. Put some genuine data in the database first, then worry about it. Better yet, load some data on a test server first, rather than doing this in production.
Sometimes, the optimizer gets it wrong. You feel elated—and possibly angry—that the database optimizer doesn’t see what you see. Please bear in mind that the data distributions within your database change over time, and this causes the optimizer to change its plans over time as well.
If you have found a case where the optimizer is wrong, this can sometimes change over time as the data changes. It might have been correct last week and will be correct...