我一生都无法弄清楚如何在我的 json 列的整数属性上创建索引。
我以这种方式尝试过(以及其他数十种方式)
CREATE INDEX user_reputation_idx ON users(("user"->>'reputation')::int)
Run Code Online (Sandbox Code Playgroud)
它在查询中工作得很好(例如ORDER BY ("user"->>'reputation')::int
)
我错过了什么?
更新
我收到一个简单的语法错误,但是,我真的不知道为什么。
ERROR: syntax error at or near "::"
LINE 1: ... user_reputation_idx ON users (("user"->>'reputation')::int)
Run Code Online (Sandbox Code Playgroud)
表定义非常简单。它只是一列user
type json
。
所以,看起来像这样:
CREATE TABLE users
(
"user" json
)
Run Code Online (Sandbox Code Playgroud) 我们可以为 JSONB 数据类型的键/值创建索引吗?
例如,对于这些架构:
CREATE TABLE x (
id BIGSERIAL,
data JSONB
);
CREATE TABLE y (
id BIGSERIAL,
data JSONB
);
Run Code Online (Sandbox Code Playgroud)
慢查询:
SELECT *
FROM x
LEFT JOIN y
ON (y.data->>'x_id')::BIGINT = x.id
Run Code Online (Sandbox Code Playgroud)
如何为该索引创建y.data->>'x_id'
可用于此类查询的索引?
postgresql performance index postgresql-9.4 postgresql-performance