数据库触发器/引用完整性和内存缓存

发布于 2024-08-25 18:44:35 字数 254 浏览 11 评论 0原文

您是否看到数据库触发器/参照完整性规则的使用方式会更改数据库中的实际数据(更改表 x 中的行 w 会导致表 z 中的行 y 发生更改)?

如果是,这与内存缓存(memcache 和朋友)的日益普及有何关系?毕竟,这些操作发生在数据库内部,但缓存系统必须了解它们才能反映正确的状态(或至少使可能更改的状态无效)。我发现很难相信回调是针对这种情况实现的。

有没有人有这样的设置的实际经验/考虑这样的设置并放弃它的实际经验(你走了哪条路?如果缓存,你如何强制完整性?)

Do you see database triggers / referential integrity rules being used in a way that changes actual data in the database (changing row w in table x causes a change in row y in table z)?

If yes, How does this tie-in with the increasing popularity of in-memory caching (memcache and friends)? After all, these actions occur inside the database but the caching system must be aware of them in order to reflect to correct state (or at least invalidate the possibly changed state). I find it hard to believe that callbacks are implemented for such cases.

Does anyone have real-world experience with such a setup / real-world experience with considering such a setup and abandoning it (which way did you go? if caching, how do you enforce integrity?)

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

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

发布评论

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

评论(1

紫南 2024-09-01 18:44:35

简单答案:

  • 引用完整性是必须具备
  • 缓存是合格必须具备
  • 触发器最好拥有

<强>更长的答案

自 1993 年以来,我一直在关系数据库上开发应用程序(自从你问以来,Dec RDB,在那之前就在平面文件系统上),触发器从未受到许多开发人员的欢迎,因为它们可以“删除那些你不想删除'。引用完整性也常常被开发人员所不赞成,因为具有适当引用完整性的第三范式的数据库很难在几分钟内整合在一起。

缓存通常也被认为很难正确执行,尽管我不确定为什么。

虽然许多系统可以在没有触发器的情况下运行,但我想说没有应用程序数据库可以在没有引用完整性的情况下轻松生存。看看这个问题上的标签,该网站背后的数据库将有一个标签(可能称为“标签”)和问题(可能称为“问题”)的表格。 “问题”将有一个指向标签表上主键的外键,但由于问题可以有很多标签,而标签可以有很多问题,我猜这种关系是这样的:

   Question
   (TagId)         1 | Database triggers / referential integrity and in-memory caching
      |  
    -----
    | | |
  QuestionTag
 (QuestionId)       1 | 1  ... 1 | 2  ... 1 | 3 ...
    (TagId)
    | | |
    -----
      |
     Tag            1 | database ... 2 | referential-integrity ... 3 | triggers ...
   (TagId)

这种引用完整性是任何可靠的应用程序,都是没有商量余地的。您可以看到它如何增加应用程序设计的可信度和对其寿命的信心。

SO 上的缓存可能会针对标签之类的东西打开(尽管不能保证),因此假设标签已缓存在内存中,并且您有足够的信誉可以向 SO 添加标签。您添加标签,它很可能会立即保存到数据库中 - 但缓存会更新吗?

你所拥有的是一个权衡。在不知道您的新标签的情况下,该网站能否生存?如果会持续多久?从根本上来说,标签的生命周期是怎样的?从用户添加到数据库中、可供其他用户使用、被其他用户使用?缓存将根据开发团队制定的规则重建 - 该规则本质上是一种权衡,以便任何新标签都可以足够快地可用,而不会减慢应用程序的速度。

触发器可以强制引用完整性,假设您添加的标签是“垃圾”,但当管理员看到它时,三个问题都被标记为“垃圾”。然后,管理员决定删除“垃圾”标签 - 但是用它标记的问题又如何呢?如果“标签”表上有一个在删除时触发的触发器,那么它可以围绕“问题”表运行并删除所有对“垃圾”的引用。这种方法有很多替代方案 - 其中许多是程序化的解决方法 - 但有没有更干净的替代方案?

在过去的 20 年里,我在很多网站上工作过,好的网站都使用引用完整性并越来越多地使用缓存。匿名更改数据的触发器(它们本质上都是事件驱动的存储过程)并不流行,而且越来越被误解,但仍然发挥着作用。

缓存和引用完整性不能被视为非此即彼——开发团队必须设计应用程序,以便将两者结合起来。

Simple Answer:

  • Referential integrity is a must have
  • Caching is a qualified must have
  • Triggers are a nice to have

Longer Answer

I've been developing applications on relational databases since 1993 (Dec RDB since you ask, and on flat file systems before then) and triggers have never been popular with many developers because they can 'delete stuff that you don't want deleting'. Referential integrity is also often frowned on by developers because a database in third normal form with proper referential integrity is difficult to bodge together in a few minutes.

Caching is also often considered quite 'hard' to do right, although I'm not sure why.

Whilst many systems can live without triggers, I'd say that no application database can comfortably survive without referential integrity. Look at the tags on this question, the database behind this site will have a table for the tags (probably called 'Tag') and questions (probably called 'Question'). 'Question' will have a foreign key to the primary key on the Tag table, but as questions can have many tags and tags can have many questions I'd guess that the relationship is like this:

   Question
   (TagId)         1 | Database triggers / referential integrity and in-memory caching
      |  
    -----
    | | |
  QuestionTag
 (QuestionId)       1 | 1  ... 1 | 2  ... 1 | 3 ...
    (TagId)
    | | |
    -----
      |
     Tag            1 | database ... 2 | referential-integrity ... 3 | triggers ...
   (TagId)

This kind of referential integrity is the bedrock of any reliable application and isn't negotiable. You can see how it adds credibility to the application design and confidence in its longevity.

The caching on SO may be turned on for such a thing as tags (although it isn't guaranteed) so assume that the tags are cached in memory and that you have enough reputation to be allowed to add a tag to SO. You add your tag and it may well be persisted to the database instantly - but is the cache then updated?

What you have is a trade-off. Can the site survive without knowing about your new tag? And if so for how long? Fundamentally what is the lifecycle of a tag, as it progresses from being added by a user to being in the database, available to other users, used by other users? The cache will be rebuilt according to the rules set down by the development team - and that rule will be a trade-off essentially so that any new tag is available quickly enough without slowing the application down.

Triggers can enforce referential integrity, say the tag you add is 'rubbish', but by the time the admins see it three questions are tagged 'rubbish'. The admins then decide to delete the 'rubbish' tag - but what about the questions that are tagged with it? If there is a trigger on the 'tag' table that is fired on the delete, it could then run round the 'question' table and remove all references to 'rubbish'. There are lots of alternatives to this approach - many of which are programmatic workrounds - but is there a cleaner alternative?

I've worked on lots of sites in the last 20 years, the good ones use referential integrity and increasingly caching. Triggers that change data anonymously (all they fundamentally are are event driven stored procedures) are not popular and increasingly misunderstood but still have a role.

Caching and referential integrity cannot be considered an either-or - development teams must design applications so that both can be incorporated.

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