【发布时间】:2020-07-29 22:19:55
【问题描述】:
例如,我想检索publish_date 和published=True 的下一个/上一个帖子。我应该创建publish_date, published 的索引还是只创建publish_date?
表是
id (int)
title (char)
description (text)
publish_date (datetime)
published (bool)
查询是:
select *
from table
where publish_date > current_post.publish_date
and published = True
order by publish_date
limit 1
哪个索引会更好?
-
(publish_time, published)上的多列索引
或
- 仅在
(publish_time)上建立单列索引
【问题讨论】:
-
如果你使用多列索引,你需要一个逗号,而不是“and”。多久发布一次是真的?如果几乎总是这样,那么没有多大意义。如果几乎从不,那么您可能需要过滤索引,而不是多列。
-
80% 已发布=true
-
publish_date>current_post.publish_date-- 嗯?你为什么提到publish_date两次?current_post没有被提及。 -
我将它与 Web 应用程序一起使用。只是想知道哪种索引更适合这种情况。 @rickJames
标签: mysql database postgresql indexing