【发布时间】:2011-09-06 08:05:11
【问题描述】:
如何在 Heroku 上管理我的数据库索引?我知道水龙头,但这似乎是用于推送/拉取数据。
如何查看、更新、删除我的索引?我的开发数据库是 sqlite3,而在 Heroku 上它是 postgres。
【问题讨论】:
-
这是 15 美元/月的 postgres,20 个演出。我不确定它是共享的还是专用的。
标签: ruby-on-rails postgresql heroku
如何在 Heroku 上管理我的数据库索引?我知道水龙头,但这似乎是用于推送/拉取数据。
如何查看、更新、删除我的索引?我的开发数据库是 sqlite3,而在 Heroku 上它是 postgres。
【问题讨论】:
标签: ruby-on-rails postgresql heroku
您应该通过迁移来管理您的索引,以便它们在您的环境中保持同步。
【讨论】:
看起来您使用的是共享数据库而不是专用数据库,因此您必须努力做到这一点。如果您有一个专用数据库,那么您可以通过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 类中,它们将很容易使用。
所有这些都为您提供所需的索引信息,然后您可以使用正常的迁移来添加、删除或修改您的索引。
【讨论】: