使用slug而不是ID获取记录

and*_*dyk 6 database url performance select slug

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

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

http://stackoverflow.com/questions/tagged/language-agnostic
Run Code Online (Sandbox Code Playgroud)

通过slug获取记录,看起来比以下更好:

http://stackoverflow.com/questions/tag/789/language-agnostic
Run Code Online (Sandbox Code Playgroud)

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

现在,使用db模式:

posts    post_to_tags    tags
-----    ------------    ----
id       id              id
title    post_id         name
content  tag_id          slug
...                      ...
Run Code Online (Sandbox Code Playgroud)

我做得对吗?是否存在我需要知道的陷阱或最佳实践以避免性能问题?(例如,标签不应超过10,000条记录,或标签slug不应超过n个字符,或其他)

提前致谢.

Ton*_*ews 5

使用第一种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];
Run Code Online (Sandbox Code Playgroud)

只要tags.slug被索引,这应该是非常有效的,几乎没有任何不同

select ...
from   posts p
join   posts_to_tags pt on pt.post_id = p.post_id
where  pt.tag_id = [url tag ID];
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我们只是在谈论使用给定的Slug值对tags表进行索引读取以获取tag_id,所以这是花生。在Oracle上,我只是尝试了10,000种选择,并且获得了1.078和1.312秒的计时-即每个查询0.0234毫秒! (2认同)