查找表:有更好的做法吗?

发布于 2024-10-24 02:18:29 字数 1485 浏览 3 评论 0原文

我想知道在设计查找表时是否有更好的实践或原则。

我打算设计一个可以服务于许多不同情况的抽象查找表。

例如,我将我的查找表称为masters and Slaves表,

CREATE TABLE IF NOT EXISTS `masters_slaves` (
  `mns_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `master_id` varchar(255) DEFAULT NULL COMMENT 'user id or page id',
  `slave_id` varchar(255) DEFAULT NULL COMMENT 'member id or user id or page id',
  `cat_id` varchar(255) DEFAULT NULL COMMENT 'category id',
  `mns_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `mns_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`mns_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

因此该查找表可以服务这些类型的关系,例如

Admins and Members
Admins and Pages
Admins and Posts
Post Categories and Posts
Page Parents and Pages
etc

masters和中的cat_id Slaves 表将描述和区分这些类别。例如,cat_id 1管理员和成员等等。

我将插入:

  1. admin id 到列中 master_id 和会员 id 写入 slave_id
  2. 父页面id插入列 master_id 和子页面 id 成 slave_id
  3. 将类别 id 发布到列中 master_id 和页面 id 到 slave_id

但我确定是否应该这样做:

  1. 这是一个很好的查找吗 桌子练习还是应该做更多创造更多 比针对不同的一个查找表 关系?
  2. 如果我可以像这样查找表 这只是一个查找表 对于所有人,我会产生什么后果 将来有吗?这鞋底会不会 查找表将被过度填充 当我的网站内容增长时?
  3. 我想到的另一件事是—— 标签系统不是一个查找表吗 解决方案也?

谢谢。

I wonder if any better practice or any principle when come to design a lookup table.

I intend to design an abstract lookup table which can serve many different situations.

For instance, I call my lookup table as a masters and slaves table,

CREATE TABLE IF NOT EXISTS `masters_slaves` (
  `mns_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `master_id` varchar(255) DEFAULT NULL COMMENT 'user id or page id',
  `slave_id` varchar(255) DEFAULT NULL COMMENT 'member id or user id or page id',
  `cat_id` varchar(255) DEFAULT NULL COMMENT 'category id',
  `mns_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `mns_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`mns_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

so this lookup table can server the relationship of these types like,

Admins and Members
Admins and Pages
Admins and Posts
Post Categories and Posts
Page Parents and Pages
etc

the cat_id in masters and slaves table will describe and differentiate these categories. for instance, cat_id 1 is Admins and Members and so on.

And I will insert:

  1. admin id into the column of
    master_id and member id into
    slave_id column
  2. parent page id into the column of
    master_id and child page id into
    slave_id column
  3. post categories id into the column
    of master_id and page id into
    slave_id column
  4. etc

But I am sure about it whether I should go for it or not:

  1. Is this a good lookup
    table practice or should do many create more
    than one lookup tables for different
    relationships?
  2. If I can do the lookup table like
    this, which is only one lookup table
    for all, what consequences I will
    have in the future? Will this sole
    lookup table will be over populated
    when my site content grows?
  3. Another thing come to mind is that -
    isn't the tag system a lookup table
    solution too?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-10-31 02:18:29

这听起来非常像反模式一个真实的查找表。谷歌一下!

以下是我在不到 1 分钟内想到的一系列不好的事情:

  • 您将无法强制执行引用完整性
  • 所有关系都必须是多对多或一对-很多
  • 你将无法处理属性(100 数量的article_id=1 在store_id=7 中出售)
  • 你将只能处理单列键
  • 表会更大(因此平均速度会更慢)
  • 索引也会变慢更大(因此平均速度更慢)
  • 它可能会导致不必要的争
  • 用 优化器统计数据将变得倾斜
  • 数据库维护变得更加困难

我可以很容易地想出更多,但我认为它并不真正需要;)

当一个人没有太多数据库经验时,重用东西并使用“通用”结构感觉像是一件好事,但数据库很少从中受益。

对您的关系使用单独的表。最终,无论如何你都会这么做。

It sounds an awful lot like the anti-pattern One True Lookup Table. Google it!

Here is a list of bad things I came up with in less than 1 minutes:

  • You won't be able to enforce referential integrity
  • All relation have to be either many-to-many or one-to-many
  • You won't be able to handle attributes (100 qty of article_id=1 sold in store_id=7)
  • You will only be able to deal with single column keys
  • The table will be bigger (thus slower on average)
  • The indexes will also be bigger (thus slower on average)
  • It may cause unnecessary contention
  • Optimizer statistics will become skewed
  • Database maintenance becomes harder

I could easily come up with more, but I don't think it's really needed ;)

When one doesn't have much database experience, it feels like a good thing to do, to reuse stuff and use "common" structures, but databases rarely benefit from it.

Use separate tables for your relationships. Eventually, you will do that anyway.

破晓 2024-10-31 02:18:29

根据我的经验,最好为每个类别创建一个查找表。
以下是我所看到的好处:

  1. 一个查找表可能会变得非常大,而一些较小的查找表可能会通过 mysql 的缓存机制加载到内存中,并且仅在您访问特殊类别时才从那里进行处理。
  2. 加载/重新加载/备份等一些较小的表更容易。
  3. 您可以稍后更改表架构,假设您希望仅为一种类型的类别添加另一个字段。您不需要将其添加到该全局表中的所有行。

我认为拥有几张小桌子比拥有一张大桌子更有利。

From my experience it is better to create a lookup table for each category.
Here are the benefits as i see them:

  1. One lookup table might became very large, while a few smaller lookup tables may be loaded by the caching mechanism of the mysql into memory and be handled only from there if you are accessing a special category.
  2. It is easier to load / reload / backup etc' a few smaller tables.
  3. You can later on change the table schema, let's say you wish to add another field only for one type of category. You will not need to add it to all the rows in this global table.

I think there are more pros for having a few smaller tables than one big one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文