Using indexes on/through a MySQL view

Posted by Peeja on Stack Overflow See other posts from Stack Overflow or by Peeja
Published on 2010-05-25T21:24:01Z Indexed on 2010/05/25 21:31 UTC
Read the original article Hit count: 249

Filed under:

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?

© Stack Overflow or respective owner

Related posts about mysql