Table: articles
+--------+------+------------+
| id | title| created |
+--------+------+------------+
| 201 | AAA | 1482561011 |
| 202 | BBB | 1482561099 |
| 203 | CCC | 1482562188 |
+--------+------+------------+
Table: taggings
+-----------+------+
| articleid | tagid|
+-----------+------+
| 201 | 11 |
| 201 | 12 |
| 202 | 11 |
| 202 | 13 |
| 202 | 14 |
+-----------+------+
Now if given 3 tag ids, what is the best index design and query to select latest 10 articles that each article match the 3 tag ids at the same time?
I know there can be several ways to do it, but I'm concerning the performance, considering there maybe tens of thousands of articles in each tag
You need have an index on
articles.created
for sorting, and another unique index ontaggings(articleid, tagid)
for querying:Then just make a select query with three
taggings
table aliases: