tags:

views:

17

answers:

0

Database structure (MySQL):

A (model)
  * id

B (model)
  * id

C (content table)
  * id
  * b_id
  * date

A_B
  * a_id
  * b_id
  • a habtm b, and
  • b has_many c

The goal is given A, get all C's in descending order by date.

Indexes:

  • on C for b_id, date
  • on A_B for a_id

I tried the straight up B join C join A_B, but it doesn't seem to use the index correctly. What ended up being the fastest was to do a B join C where B.id IN ([B.id's]), where B.id's is entered in as the raw numbers. Doing a subquery results in a full scan of C. This is pretty surprising to me, as I'd expect MySQL to optimize. I also think this is a pretty common setup. Any advice would be greatly appreciated

Edit: Engine is InnoDB