使用 slug 而不是 ID 来获取记录

发布于 2024-07-12 23:07:37 字数 731 浏览 6 评论 0原文

我目前正在尝试在处理诸如获取标有特定标签、类别或类似内容的记录之类的情况时找到最佳方法(在可用性和性能方面)。

一个好方法(我想要做的方法)是使用标签/类别 slug 获取记录,因此 URL 看起来像:

http://stackoverflow.com/questions/tagged/language-agnostic

通过 slug 获取记录,这看起来比:

http://stackoverflow.com/questions/tag/789/language-agnostic

通过 ID 获取并在后面添加 slug 更好它对搜索引擎更友好。 这在性能方面更好,因为通过整数 ID 获取数据比字符串更快。 (cmiiw)

现在,使用像这样的数据库模式:

posts    post_to_tags    tags
-----    ------------    ----
id       id              id
title    post_id         name
content  tag_id          slug
...                      ...

我做得对吗? 为了避免性能问题,我需要了解哪些陷阱或最佳实践吗? (例如,标签不应超过 10,000 条记录,或标签段不应超过 n 个字符,或其他)

提前致谢。

I'm currently trying to find the best way (in term of usability and performance) when dealing with a situation like fetching records tagged with a specific tag, or category, or something like that.

A good way (the way I wanted to do), would be to fetch records with the tag/category slug, so the URL would look like :

http://stackoverflow.com/questions/tagged/language-agnostic

fetching records by slug, which looks better than :

http://stackoverflow.com/questions/tag/789/language-agnostic

fetching by ID and adding the slug behind so it's more search-engine friendly. This one is better performance-wise, because fetching data by an integer ID would be faster than a string. (cmiiw)

Now, with a db schema like :

posts    post_to_tags    tags
-----    ------------    ----
id       id              id
title    post_id         name
content  tag_id          slug
...                      ...

am I doing it right ? Is there pitfall or best-practices that I need to know to avoid performance problems ? (eg. tags should not exceed 10,000 records, or tag slug should not exceed n characters, or something else)

Thanks in advance.

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

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

发布评论

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

评论(2

¢蛋碎的人ぎ生 2024-07-19 23:07:38

使用第一个 URL 样式和当前的数据库设计,您可以执行以下操作:

select ...
from   posts p
join   posts_to_tags pt on pt.post_id = p.post_id
join   tags t on t.id = pt.tag_id
where  t.slug = [url slug value];

只要对 tag.slug 建立索引,这应该非常高效,几乎

select ...
from   posts p
join   posts_to_tags pt on pt.post_id = p.post_id
where  pt.tag_id = [url tag ID];

With the first URL style and your current db design, you can do this:

select ...
from   posts p
join   posts_to_tags pt on pt.post_id = p.post_id
join   tags t on t.id = pt.tag_id
where  t.slug = [url slug value];

As long as tags.slug is indexed, this should be very efficient, hardly any different from

select ...
from   posts p
join   posts_to_tags pt on pt.post_id = p.post_id
where  pt.tag_id = [url tag ID];
农村范ル 2024-07-19 23:07:38

第一个更好,但是可以改变子弹吗? 在这种情况下,您需要有一个重定向表(例如“some-article-about-dogs”现在是“article-about-dogs-and-cats”)。

The first one is better, but can the slugs possibly be changed? In that case you'd need to have a redirect table (e.g. "some-article-about-dogs" is now "article-about-dogs-and-cats").

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