MPTT 表的建议索引
我只是使用修改的前序树遍历 (MPTT) 构建一个表来存储分层数据 - 您知道这一点:每个节点存储左
和右
用于查找其后代的 ID。我使用的是 CakePHP 建议的模型,该模型与标准方式不同,在每行中包含 parent_id
。
以下是建议的表结构:
CREATE TABLE categories (
id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INTEGER(10) DEFAULT NULL,
lft INTEGER(10) DEFAULT NULL,
rght INTEGER(10) DEFAULT NULL,
name VARCHAR(255) DEFAULT '',
PRIMARY KEY (id)
);
以前从未使用过这种样式,并且不确切知道如何搜索它,我想知道应该对哪些字段建立索引?仅主键就足够了,还是应该包含 lft
和 rght
?
I'm just building a table to store hierarchical data using the Modified Pre-order Tree Traversal (MPTT) -- you know the one: each node stores the left
and right
IDs to find its descendants. I'm using a the CakePHP suggested model, which varies from the standard way by including the parent_id
with each row.
Here's the suggested table structure:
CREATE TABLE categories (
id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INTEGER(10) DEFAULT NULL,
lft INTEGER(10) DEFAULT NULL,
rght INTEGER(10) DEFAULT NULL,
name VARCHAR(255) DEFAULT '',
PRIMARY KEY (id)
);
Having never used this style before, and not knowing exactly how it gets searched, I'm wondering which fields I should be indexing? Is just the primary key sufficient, or should I be including lft
and rght
too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将始终使用左列,但我经常需要找到所有叶节点。
所以我通常只是用 lft, rgt 对创建一个索引。
You will always be using the left column, however I often need to find all leaf nodes.
So I usually just create a index with the pair lft, rgt.
我通常只索引左列。我通常使用 mysql,它在执行计划中只允许每个表有一个索引用户,左边的索引可以帮助我为 MPTT 表编写的每个查询,而在该索引中包含右边的好处是最小的。
简而言之,根据我的经验,左侧的单个索引是几乎每个用例的插入/更新速度和选择速度之间的最佳平衡。
i generally index just the left column. i usually work with mysql, which only allows for one index user per table in an execution plan, and an index on left helps every query i've ever written for a MPTT table and the benfits of including right in that index are minimal.
in short, a single index on left, in my experience, is the optimum balance between insert/update speed and select speed for nearly every use case.