So the project is making a simple CMS in PHP. You have posts, categories and tags. It will be handling millions of posts in one database, millions more tags and categories.
The problem: Optimally, you want to be able to select 30 posts that must be under categories 5 and tags 1 and 2. You want it done in as little queries as possible...
termRelations contains the post ID and term ID, with termTypeId distinguishing between the cats and tags tables.
cats contains the term ID and category info (Name, slug etc.)
tags contains the term ID and tag info (Name, slug etc.)
cats and tags are seperate tables so to speed up generating a category list/ to define them more seperately.
SELECT DISTINCT *
FROM posts
LEFT JOIN termRelations ON ( posts.id = termRelations.postId )
LEFT JOIN cats ON ( termRelations.termId = cats.id AND termRelations.termTypeId = 1 )
LEFT JOIN tags ON ( termRelations.termId = tags.id AND termRelations.termTypeId = 0 )
WHERE cats.id =5
OR tags.id =2
OR tags.id =1
LIMIT 0 , 30
In this case it returns 3 rows for one post, the first two with tag fields added, the last with the category fields.
I do not need this information for use (As that seems impossible when it comes to multiple tags or categories in one row. Maybe not?), I merely need to grab posts under those three terms. Though, if I could get category and tag info with one query that would be optimal.
Thanks. This is screwing with my brain. If I am doing something wrong and you know a more efficient way to do this then I would be happy to re structure the database.