mysql> desc posts_posts;
+------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| body | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| is_deleted | tinyint(1) | NO | | 0 | |
+------------+------------+------+-----+---------+----------------+
6 rows in set (0.06 sec)
mysql> desc posts_personas_assc;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| post_id | int(11) | NO | MUL | NULL | |
| persona_id | int(11) | NO | MUL | NULL | |
+------------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
Basically, user creates posts. Each post can have "personas" attached to them. In other words, it's a many-to-many relationship. (There's another persona table which is not shown here)
Suppose I have 50 million documents or so. I want to run this query:
SELECT pp.id FROM posts_posts pp
INNER JOIN posts_persona_assc ppa ON pp.id = ppa.post_id
WHERE ppa.persona_id IN(id1, id2, id3)
AND start_time > pp.created_at
ORDER BY created_at DESC LIMIT num;
- I'll have ppa.persona_id and pp.created_at indexed, of course
- the "IN" portion will never have more than 3 IDS.
Will it be efficient enough for me to run this query? Will it be fast enough?
This query wil be ran every time someone hits a user profile page.