use*_*289 14 postgresql jsonb postgresql-9.4
我正在尝试postgres jsonb列类型,到目前为止一直很好.我正在使用的一个常见查询是这样的:
select count(*) from jsonbtest WHERE attributes @> '{"City":"Mesa"}';
Run Code Online (Sandbox Code Playgroud)
我该怎么扭转呢?是否有不同的运营商或仅仅用作
select count(*) from jsonbtest WHERE NOT attributes @> '{"City":"Mesa"}';
Run Code Online (Sandbox Code Playgroud)
小智 10
两种方法,您可以测试任何json(b)值
->>运营商获取价值为文本。但是这个操作很慢,如果只使用值测试@>操作者的测试任何JSON(b)中含有任何JSON(b)中。这是一种快速的方法,但是您没有经过NOT选项的测试。简单快捷的方法:
NOT (attribute @> '{"City":"Mesa"}'::jsonb)
Run Code Online (Sandbox Code Playgroud)
我已更改attribute->>'City' <> 'Mesa'为NOT (attribute @> '{"City":"Mesa"}'::jsonb),我的〜2.000.000行查询结果时间从45秒更改为25秒。
这可以通过几种条件来实现。它不是很优雅,但是我没有找到另一种方法。
因此,首先,获取每个简单的不具有“城市”属性的行,然后添加“或”条件以检查正确的字段值。
select count(*) from jsonbtest where
NOT(attributes ? 'City')
OR (attributes ? 'City') is NULL -- this is required if attributes can be null
OR (attributes->>'City' != 'Mesa'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5675 次 |
| 最近记录: |