gpt4 book ai didi

database - PostgreSQL 的 Ltree 模块是否适合线程注释?

转载 作者:太空狗 更新时间:2023-10-30 01:44:49 26 4
gpt4 key购买 nike

我正在考虑使用 PostgreSQL 的 Ltree module在我的应用程序中帮助线程评论。我一直在关注它以用于线程评论。我认为它会帮助您需要更新节点及其子节点的情况,例如当您想要隐藏评论及其回复时。

我在想 ltree(或类似的东西)如果它与传统的邻接列表(“comment_id”/“parent_comment_id”)相结合会很有用。

在开始使用 ltree 之前,我想知道一些事情:

  1. 您是否或曾经使用过 ltree?这就是所谓的“生产就绪”吗?
  2. 如果是,您用它解决了哪些问题?它做得好吗?
  3. 您认为它适合线程评论系统?
    1. 如果您使用它,您将什么用于路径的“文本”部分?您是否设置了诸如他们使用“Top.Astronomy.Cosmology”的 DMOZ 示例或基于主键“1.403.29.5”之类的东西?
    2. 有更好的方法吗?我对使用嵌套列表方法有点紧张——我读过的所有内容都表明更新或插入并不是所有的问题(你不需要重新排序整个东西吗?)。我也不是 CS 专业的,那种数据结构是我将来可能会忘记的东西。有人使用嵌套列表来评论或类似的东西吗?

如果有任何帮助,这是我正在考虑的模式:

CREATE TABLE comments (
comment_id SERIAL PRIMARY KEY,
parent_comment_id int REFERENCES comments(comment_id) ON UPDATE CASCADE ON DELETE CASCADE,
thread_id int NOT NULL REFERENCES threads(thread_id) ON UPDATE CASCADE ON DELETE CASCADE,
path ltree NOT NULL,
comment_body text NOT NULL,
hide boolean not null default false
);

ltree 使用的“路径”列看起来像这样:

<thread_id>.<parent_comment_id_#1>.<parent_comment_id_#2>.<my_comment_id>

在路径中使用主键有什么问题吗?我应该在路径中包含节点自己的主键吗?如果我这样做了,在其上放置一个唯一索引作为约束是否有意义?

最佳答案

  1. 是的,是的;
  2. 知识库中各个部分的层次结构(实现之一);
  3. 是的;

其中一个表的定义:

                                                   Table "knowledgebase.section"
Column | Type | Modifiers
----------------------------+--------------------------+-----------------------------------------------------------------------------
section_sid | integer | not null default nextval('knowledgebase.section_section_sid_seq'::regclass)
section | character varying | not null
description | character varying |
path | ltree | not null
is_active | boolean | not null default true
role_sid | integer | not null
last_modified_by | integer | not null
creation_datetime | timestamp with time zone | not null default now()
last_modification_datetime | timestamp with time zone | not null default now()
is_expanded | boolean | not null default false
section_idx | tsvector |
Indexes:
"section_sid_pkey" PRIMARY KEY, btree (section_sid)
"section_section_key" UNIQUE, btree (section)
"idxsection_idx" gist (section_idx)
"path_gist_idx" gist (path)
Foreign-key constraints:
"last_modified_by_fkey" FOREIGN KEY (last_modified_by) REFERENCES "user"."role"(role_sid) ON UPDATE CASCADE ON DELETE RESTRICT
"role_sid_fkey" FOREIGN KEY (role_sid) REFERENCES "user"."role"(role_sid) ON UPDATE CASCADE ON DELETE RESTRICT
Triggers:
section_idx_update BEFORE INSERT OR UPDATE ON knowledgebase.section FOR EACH ROW EXECUTE PROCEDURE tsearch2('section_idx', 'section')

“路径”列使用主键作为标签。

该表当前内容的示例(关于主键和“路径”列):

  section_sid | path
-------------+-------
53 | 34.53
56 | 56
55 | 29.55
35 | 35
54 | 34.54
37 | 30.37
... | ...

关于database - PostgreSQL 的 Ltree 模块是否适合线程注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/603894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com