13

在 Rails 中,如何在迁移到具有特定排序顺序的 Postgres 数据库中添加索引?

最终想要完成这个查询:

CREATE INDEX index_apps_kind_release_date_rating ON apps(kind, itunes_release_date DESC, rating_count DESC);

但是现在在我的迁移中,我有这个:

add_index :apps, [:kind, 'itunes_release_date desc', 'rating_count desc'], name: 'index_apps_kind_release_date_rating'

吐出这个:

CREATE INDEX "index_apps_kind_release_date_rating" ON "apps" ("kind", "itunes_release_date desc", "rating_count desc")

哪些错误:

PG::Error: ERROR:  column "itunes_release_date desc" does not exist
4

3 回答 3

25

Rails 现在支持在迁移中指定索引顺序:

def change
  add_index(:accounts, [:branch_id, :party_id, :surname], order: {branch_id: :desc, party_id: :asc})
end

来自http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index

于 2015-10-22T00:21:48.280 回答
3

您不需要DESC在索引中指定。它将为使用此特定排序的查询带来一点速度优势,但通常 - 索引可用于列的任何排序。

于 2013-06-03T14:51:43.823 回答
2

看起来 Rails 不支持有序索引。

我怀疑您可以安全地删除这两个desc, btw: kindis 在您的 where 子句中基于您之前的问题,因此 PG 应该愉快地以相反的顺序查找索引。

于 2013-06-03T14:41:10.580 回答