Using indexes on/through a MySQL view
- by Peeja
We've got a MySQL table in which rows are never updated, but instead new rows are added and the old ones marked obsolete. Think Rails' acts_as_paranoid, but for every update.
To make working with Rails sane, we've got a view which selects only the rows which are "current". That makes a much better "table" for our ActiveRecord model.
The snag: our indexes aren't being used anymore.
Queries on the view don't use the underlying tables' indexes. You can't add an index to a view. Without indexes, the app is unbearably slow.
The only solution we've come up with is to build a materialized view, but that's a pain in MySQL because they're not natively supported.
Is there a better way to do this?