0

我有一个简单的表,需要对信息进行半复杂的检索。表格是:

表 - 帖子

id        - bigint(20)
title     - varchar(250)
content   - longtext
parent_id - int(20)
type      - varchar(50)

目前,我在帖子表中输入了三个不同的类型值 - 组、讨论和回复。数据的存储方式如下:

  1. 回复有一个讨论 id 作为它的 parent_id。
  2. 讨论有一个组 id(或 0)作为它的 parent_id。

该站点的成员可以加入一个组,我想将该组的讨论和回复包含在他们的电子邮件摘要中。目前,我正在使用 LEFT JOINS 来完成此操作,但它变得很重,因为我必须有很多连接才能完成它,并且随着表的快速增长,性能正在离题。

有人对更好的实施有任何建议吗?我已经研究了嵌套集模型,但我不相信它在这里对我有帮助,因为检索是基于顶级组条目上的操作,因此检索到的数据需要与之相关联。

非常感谢您的想法和回应。

4

1 回答 1

1

If your data model is replies are in discussions are in groups, then you should have three different tables for these entities. This gives more information to the SQL engine for optimizing the queries.

It may be too late for that. In that case, you need to be sure that you have appropriate indexes. For instance, to get all discussions in a group, there is a query like this:

select *
from posts p
where p.parent_id = XXX and p.type = 'Discussion'

This suggests that you need an index on type, probably posts(type, id). To see what other indexes might be useful, you would need to modify your question to include some of the queries you are using.

于 2013-01-31T15:49:22.813 回答