4

让我们以一篇博客文章为例,其中从文章的标题生成了一个独特的 slug:sample_blog_post。假设您将 slug 存储在 _id 中,而不是将 mongo ObjectId 存储为 _id。除了标题更改时 slug 可能会更改的明显情况之外,使用字符串而不是数字 _id 在性能方面是否存在主要缺点?如果帖子数量变得非常大,例如超过一百万,这可能会成为问题。但如果帖子数量相对较少,比如 2000 个,会有很大的不同吗?到目前为止,我认为我会利用的关于 ObjectId 的唯一一件事是 created_on 日期是免费的。

总而言之,将 slug 存储为 _id 而不使用 ObjectId 是否值得?似乎有关于如何将替代值存储为 _id 的讨论,但没有讨论它的性能优势/劣势。

4

1 回答 1

3

So in summation, is it worth it to store the slug as the _id and not use an ObjectId?

In my opinion, no. The performance difference will be negligible for most scenarios (except paging), but

  • The old discussion of surrogate primary keys comes up. A "slug" is not a very natural key. Yes, it must be unique, but as you already pointed out, changing the slug shouldn't be impossible. This alone would keep me from bothering...
  • Having a monotonic _id key can save you from a number of headaches, most importantly to avoid expensive paging via skip and take (use $lt/$gt on the _id instead).
  • There's a limit on the maximum index length in mongodb of less than 1024 bytes. While not pretty, URLs are allowed to be a lot longer. If someone entered a longer slug, it wouldn't be found because it's silently dropped from the index.
  • It's a good idea to have a consistent interface, i.e. to use the same type of _id on all, or at least, most of your objects. In my code, I have a single exception where I'm using a special hash as id because the value can't change, the collection has extremely high write rates and it's large.
  • Let's say you want to link to the article in your management interface (not the public site), which link would you use? Normally the id, but now the id and the slug are equivalent. Now a simple bug (such as allowing an empty slug) would be hard to recover from, because the user couldn't even go to the management interface anymore.
  • You'll be dealing with charset issues. I'd suggest to not even use the slug for looking up the article, but the slug's hash.

Essentially, you'd end up with a schema like

{ "_id" : ObjectId("a237b45..."), // PK
  "slug" : "mongodb-is-fun", // not indexed
  "hash" : "5af87c62da34" } // indexed, unique
于 2013-10-24T18:19:54.310 回答