使用slug而不是ID获取记录

时间:2009-01-27 08:29:01

标签: database performance url select slug

我正在尝试寻找最佳方式(在可用性和性能方面)处理诸如获取标记有特定标记或类别或类似内容的记录的情况。

一种好方法(我想做的方式)是获取带有标签/类别slug的记录,因此URL看起来像:

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

通过slug获取记录,看起来好于:

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

通过ID获取并添加slug,因此它更适合搜索引擎。这个性能更好,因为通过整数ID获取数据会比字符串更快。 (cmiiw)

现在,使用db架构,如:

posts    post_to_tags    tags
-----    ------------    ----
id       id              id
title    post_id         name
content  tag_id          slug
...                      ...
我正在做对吗?是否存在我需要知道的陷阱或最佳实践以避免性能问题? (例如,标签不应超过10,000条记录,或标签slug不应超过n个字符,或其他内容)

提前致谢。

2 个答案:

答案 0 :(得分:4)

使用第一个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];

答案 1 :(得分:0)

第一个更好,但可能会改变slu ??在这种情况下,你需要有一个重定向表(例如“some-article-about-dogs”现在是“关于狗和猫的文章”)。