Scalable way of doing self join with many to many table

Posted by johnathan on Stack Overflow See other posts from Stack Overflow or by johnathan
Published on 2010-04-20T04:55:51Z Indexed on 2010/04/20 5:03 UTC
Read the original article Hit count: 234

I have a table structure like the following:

user
  id
  name

profile_stat
  id
  name

profile_stat_value
  id
  name

user_profile
  user_id
  profile_stat_id
  profile_stat_value_id

My question is:

How do I evaluate a query where I want to find all users with profile_stat_id and profile_stat_value_id for many stats?

I've tried doing an inner self join, but that quickly gets crazy when searching for many stats. I've also tried doing a count on the actual user_profile table, and that's much better, but still slow.

Is there some magic I'm missing? I have about 10 million rows in the user_profile table and want the query to take no longer than a few seconds. Is that possible?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about self-join