【问题标题】:Managing db indexes on heroku在heroku上管理数据库索引
【发布时间】:2011-09-06 08:05:11
【问题描述】:

如何在 Heroku 上管理我的数据库索引?我知道水龙头,但这似乎是用于推送/拉取数据。

如何查看、更新、删除我的索引?我的开发数据库是 sqlite3,而在 Heroku 上它是 postgres。

【问题讨论】:

  • 这是 15 美元/月的 postgres,20 个演出。我不确定它是共享的还是专用的。

标签: ruby-on-rails postgresql heroku


【解决方案1】:

您应该通过迁移来管理您的索引,以便它们在您的环境中保持同步。

【讨论】:

  • 这不让我查看我的索引,是吗?还是您在谈论查看 schema.rb?
  • 好吧,如果你愿意,你也可以在所有迁移中使用 grep add_index,但是 schema.rb 是可以查看的地方。我不知道有什么更好的方法,而且我认为做任何特定于 heroku 的事情都会让你以一种在未来很难看的方式对生产数据库模式进行更改。
【解决方案2】:

看起来您使用的是共享数据库而不是专用数据库,因此您必须努力做到这一点。如果您有一个专用数据库,那么您可以通过heroku pg:psql\di and assorted other psql commands 来获得您要查找的内容。

尽管总是有困难的方法,这涉及到内部目录表。您需要的 SQL 块很少,您可以将它们包装在 ActiveRecord::Base.connection.select_rows 调用中并从 Rails 控制台访问结果。

您可以通过以下方式获取表及其索引的列表:

select c2.relname as table, c2.oid as table_oid, c.relname as name, c.oid as index_oid
from pg_catalog.pg_class c
join pg_catalog.pg_index i on i.indexrelid = c.oid
join pg_catalog.pg_class c2 on i.indrelid = c2.oid
left join pg_catalog.pg_user u on u.usesysid = c.relowner
left join pg_catalog.pg_namespace n on n.oid = c.relnamespace
where c.relkind  = 'i'
  and n.nspname <> 'pg_catalog'
  and pg_catalog.pg_table_is_visible(c.oid)
order by c2.relname, c.relname

然后您可以使用index_oid 来获取有关索引的描述:

select c.relname, c.oid, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), c.reltablespace
from pg_catalog.pg_class c
join pg_catalog.pg_index i on c.oid = i.indexrelid
where c.oid = '#{index_oid}'

或者您可以使用table_oid 来获取该表的索引列表:

select ci.relname, ci.oid, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), ci.reltablespace
from pg_catalog.pg_index i 
join pg_catalog.pg_class ci on i.indexrelid = ci.oid
where i.indrelid = '#{table_oid}'
order by ci.relname  

您可能希望将所有这些东西包装在一个实用程序类中以便于访问:

class PGInfo
    def self.list_indexes
        data = ActiveRecord::Base.connection.select_rows(%Q{
            select c2.relname as table, c.relname as name, c.oid as oid
            ...
        })
        # do something pretty with the array of arrays that is in data
    end
    # etc.
end

我没有在 Heroku 的共享数据库中尝试过这些(抱歉,我只有一个专用数据库可以使用)。可能有更简单的方法,但这些应该可以完成工作,如果你将它们包装在 PGInfo 类中,它们将很容易使用。

所有这些都为您提供所需的索引信息,然后您可以使用正常的迁移来添加、删除或修改您的索引。

【讨论】:

  • @Brand:不客气,我实际上有时会使用答案来为自己做笔记:)
猜你喜欢
  • 2018-08-27
  • 2011-01-28
  • 2021-08-30
  • 2016-03-06
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
相关资源
最近更新 更多