我有一个模型任务
t.string "name"
t.hstore "actions"
Run Code Online (Sandbox Code Playgroud)
例:
#<Task id: 1, name: "first", actions: {"today"=>"9"},
#<Task id: 2, name: "second", actions: {"yesterday"=>"1"},
#<Task id: 3, name: "third", actions: nil,
#<Task id: 4, name: "four", actions: {"today"=>"11"},
Run Code Online (Sandbox Code Playgroud)
我需要找到行动的所有记录:nil,:今天<10和关键:今天不存在.这是1,2,3任务.
Task.where("actions -> 'today' < '10'") - it return only first task.
Task.where("actions -> 'today' < '10' OR actions IS NULL") - it return 1 and 3 records.
Run Code Online (Sandbox Code Playgroud)
我怎么能找到所有没有钥匙的记录:今天在行动?
我正在玩postgresql 9.3的hstore.我正在尝试使用和索引一个hstore列,就像文档状态一样.我的问题是索引似乎没有被使用.让我给你举个例子:
我创建了一个表'Person':
=# CREATE TABLE Person (Id BIGSERIAL PRIMARY KEY NOT NULL, Values hstore);
Run Code Online (Sandbox Code Playgroud)
并插入一个测试值:
=# INSERT INTO Person (Values, 'a=>1,b=>3');
Run Code Online (Sandbox Code Playgroud)
然后,如果我在"值"列上解析使用运算符"@>"的SELECT查询,我毫不奇怪地得到:
=# EXPLAIN SELECT P.* FROM Person AS P WHERE P.Values @> hstore('a', '1');
QUERY PLAN
----------------------------------------------------------
Seq Scan on person p (cost=0.00..24.50 rows=1 width=40)
Filter: ("values" @> '"a"=>"1"'::hstore)
Run Code Online (Sandbox Code Playgroud)
无索引< - >顺序扫描.说得通.无论如何,如果我创建GIN或GIST索引并不重要,解释会继续讨论顺序扫描:
=# CREATE INDEX IX_GIN_VALUES ON Person USING GIN (values);
CREATE INDEX
=# EXPLAIN SELECT P.* FROM Person P WHERE P.values @> hstore('a', '1');
QUERY …Run Code Online (Sandbox Code Playgroud) 我一直在尝试迁移我的数据库以使用HSTORE,但是当我想在其他模式中添加HSTORE列时,扩展仅适用于公共 SCHEMA,它不起作用
def up
# My hstore looks like this
execute "CREATE EXTENSION hstore SCHEMA public"
# I have also tried
# execute "CREATE EXTENSION hstore"
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的下一次迁移它只是不起作用,如果我去psql控制台和alter table我得到这个:
set search_path to public;
alter table accounts add column extras hstore; -- Works fine
set search_path to schema2;
alter table accounts add column extras hstore; -- Raises an error
Run Code Online (Sandbox Code Playgroud)
我正在使用rails 4谢谢.
有没有办法限制hstore可以保存哪些列?到目前为止,我已经有了以下代码:
store_accessor :widget_locations, :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2
Run Code Online (Sandbox Code Playgroud)
但这似乎仍然允许保存其他键名,即。middle_area123
另外我如何能够hstore像update_attributes或那样更新update?
我使用 hstore(Postgresql 9.3.4)来存储给定日期内每次发生事件的计数,更新如下。
days_count = days_count || hstore('x', (coalesce((days_count -> 'x')::integer, 0) + 1)::text)
Run Code Online (Sandbox Code Playgroud)
其中 x 是一年中的第几天。在运行生产预期行为的模拟之后,在分析和真空之后,我最终得到了一个150MB + 2GB Toast + 25-30MB 索引的表。
我现在将上面的列分成每个月的一列,如下所示
y_month_days_count = y_month_days_count || hstore('x', (coalesce((y_month_days_count -> 'x')::integer, 0) + 1)::text)
Run Code Online (Sandbox Code Playgroud)
其中 x 是该月中的第几天,y 是一年中的月份。 我现在仍在运行模拟,但到目前为止,已经完成了三分之一,我的内存为 60MB + 相当稳定的 20-30MB Toast + 25-30MB 索引。这意味着最终我应该得到大约180MB + 30-40MB 用于 Toast + 25MB-30MB 用于分析和真空之后的索引。
那么首先,Hstore 和 Toast 膨胀是否存在任何已知问题可以解释我的第一次设置问题?
其次,我当前的分解列的解决方案是否会因为一张表上的 hstore 列数量而导致未来的 hstore 和性能出现任何类型的问题?现在似乎很稳定,行数达到数十万,虽然我知道更多的列会使速度变慢,但我不确定 hstore 列的情况是否会更糟。
最后我确实发现了一些东西。我有一个 hstore 列,最终代表一天中的每一个小时,因此它有 24 个不同的键。当我仅针对此列运行模拟时,我最终几乎没有以 KB 为单位的 toast,但是当我运行整个模拟时,将天数分解为月份列,我最大的 hstore …
如何使用 Postgres 在 Rails 4 中重命名 hstore 键?
User.update_all('hstorename -> oldcolumname: newcolumnname') ???
Run Code Online (Sandbox Code Playgroud) 我设置我的Rails 3.2.x应用程序以使用PostgreSQL HStore,但我收到一个错误.看起来环境没有拾取hstore扩展.我已经重新启动了我的机器,检查了数据库扩展等.
当我尝试执行时:
User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test")
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:( @>无法识别,即hstore没有安装扩展程序?)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
我的Rails应用程序设置是:
Gemfile.rb:
gem 'activerecord-postgres-hstore'
Run Code Online (Sandbox Code Playgroud)
移民:
add_column :users, :settings, :hstore
execute "CREATE INDEX CONCURRENTLY users_gin_settings ON users USING GIN(settings)"
# can see the extenstion installed on my local dev psql database after this
Run Code Online (Sandbox Code Playgroud)
User 模型:
serialize :settings, ActiveRecord::Coders::Hstore
Run Code Online (Sandbox Code Playgroud)
动态User方法:
# metaprogramming: …Run Code Online (Sandbox Code Playgroud) 我需要将键值对保存在PostgreSQL数据库中,该数据库将具有有关记录的一些基本信息。
在对该主题进行了一些搜索之后,我发现hstore是选项之一。但是即使查阅了文档,我也无法弄清楚如何在带有hstore列的表中添加记录,以及如何在结果中返回它们以及如何解析它。
我对PostgreSQL完全陌生,因此任何代码参考都很棒。
考虑这个类:
class UsersMigration < ActiveRecord::Migration[5.2]
def change
create_table(:users) do |t|
t.hstore :settings
end
end
end
class User < ActiveRecord::Base
store_accessor :settings, :privileges, :colors
end
Run Code Online (Sandbox Code Playgroud)
执行此查询时:
User.where(privileges: 'Admin')
Run Code Online (Sandbox Code Playgroud)
它会导致此错误:
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column users.privileges does not exist)
并且是正确的,因为该列实际上是设置
为了进行查询,我们必须这样做:
User.where("settings->'privileges' = 'Admin'")
Run Code Online (Sandbox Code Playgroud)
如果模型已经显式序列化到 hstore 列,为什么第一个查询不会产生以下 SQL:
SELECT "user".* FROM "users" WHERE (settings->'privileges' = 'Admin')
Run Code Online (Sandbox Code Playgroud) 我正在部署一个使用PostgreSQL和HSTORE的Rails应用程序.
为了部署它,我正在使用橡胶.
一切正常,但HSTORE未正确启用除外.当包含execute("CREATE EXTENSION hstore")运行的迁移时,我收到以下错误:
** [out :: production.---]
** [out :: production.---] -- execute("CREATE EXTENSION hstore")
** [out :: production.---]
** [out :: production.---] rake aborted!
** [out :: production.---] An error has occurred, this and all later migrations canceled:
** [out :: production.---]
** [out :: production.---] PG::Error: ERROR: permission denied to create extension "hstore"
** [out :: production.---] HINT: Must be superuser to create this extension.
Run Code Online (Sandbox Code Playgroud)
创建postgres实例的脚本具有以下代码:
create_user_cmd = "CREATE USER #{env.db_user} WITH …Run Code Online (Sandbox Code Playgroud) 我想知道是否有人知道hstorePostgreSQL 9.2 中的列上的这个简单查询出了什么问题
查询在 pgAdmin 中运行
\n\nselect attributeValue->"CODE_MUN" from shapefile_feature\nRun Code Online (Sandbox Code Playgroud)\n\n返回:\xc2\xab 属性值\xc2\xbb 列不存在
\n\n当做:
\n\nselect * from shapefile_feature;\nRun Code Online (Sandbox Code Playgroud)\n\n返回所有列,包括 attributeValue、hstore 列
\n\n问题是什么?
\n假设我有一个具有此定义的表:
CREATE TABLE test (
values HSTORE NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
想象一下,我插入一些记录并得到以下结果:
values
-----------------------------
"a"=>"string1","b"=>"string2"
"b"=>"string2","c"=>"string3"
Run Code Online (Sandbox Code Playgroud)
有什么办法可以使聚合查询为我提供一个新的hstore,其中包含所有行的合并键(和值)。
伪查询:
SELECT hstore_sum(values) AS value_sum FROM test;
Run Code Online (Sandbox Code Playgroud)
所需结果:
value_sum
--------------------------------------------
"a"=>"string1","b"=>"string2","c"=>"string3"
Run Code Online (Sandbox Code Playgroud)
我知道每个键具有不同值的潜在冲突,但是在我的情况下,选择值的顺序/优先级并不重要(它甚至不必是确定性的,因为对于同一键,它们将是相同的) 。
这可能是开箱即用的,还是您必须使用某些特定的自制SQL函数或其他函数来完成?
hstore ×12
postgresql ×9
ruby ×4
sql ×2
activerecord ×1
aggregate ×1
amazon-ec2 ×1
heroku ×1
indexing ×1
rubber ×1