MySQL 需要帮助定义 SQL 以删除不需要的行
所以该项目正在用 PHP 制作一个简单的 CMS。您有帖子、类别和标签。它将在一个数据库中处理数百万个帖子、数百万个标签和类别。
问题: 最佳情况下,您希望能够选择类别 5 和标签 1 和 2 下的 30 个帖子。您希望以尽可能少的查询完成此操作...
termRelations 包含帖子 ID 和术语ID,用 termTypeId 区分 cats 和标签表。
cats 包含术语 ID 和类别信息(名称、鼻涕虫等)
标签包含术语 ID 和标签信息(名称、slug 等),
猫和标签是单独的表,以便加快生成类别列表/更单独地定义它们。
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
在本例中,它为一篇文章返回 3 行,前两行添加了标签字段,最后一行添加了类别字段。
我不需要使用这些信息(因为当涉及到一行中的多个标签或类别时,这似乎是不可能的。也许不是?),我只需要抓取这三个术语下的帖子。不过,如果我可以通过一个查询获取类别和标签信息,那将是最佳的。
谢谢。这简直是在搞乱我的大脑。如果我做错了什么,并且您知道更有效的方法来做到这一点,那么我很乐意重新构建数据库。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DISTINCT 适用于 SELECT 中的所有列,因此如果您选择所有内容,它将返回每个不同的行,而不仅仅是不同的帖子。为了解决这个问题,您可以从帖子表中选择数据,然后区分它,即
,但您也说过,如果可能的话,您也希望获得帖子和猫信息。执行此操作并为每个帖子保留一行的一种方法是使用 GROUP_CONCAT 所以你的查询可能会像这样结束。
我对原始查询进行了一些其他更改,例如将第一个联接更改为 INNER JOIN 并将 cats/tags 过滤器添加到相关表的联接条件中。
ps,当您说您有单独的猫表和标签表来加速生成列表时,您可能会发现正确索引的一张表也会同样快,并且还会简化您的代码。
DISTINCT works on all columns in the SELECT so has you are SELECTing everything it will return each distinct row and not just the distinct posts. To get round this you could just SELECT the data from the posts table and then DISTINCT it, i.e.
But you've also said you would like the posts and cats info as well if possible. One way to do this and keep one row per post is to use GROUP_CONCAT so your query might end up something like this.
I've made a couple of other changes to your original query like changing the first join to an INNER JOIN and adding the cats/tags filters to the JOIN conditions for the relevant tables.
ps when you say you have separate tables for cats and tags to speed up generating lists, you may find that one table that is correctly indexed would be just as fast and would also simplify your code.