如何在Postgres中制作多列JSON索引?

发布于 2025-02-06 14:34:36 字数 649 浏览 2 评论 0原文

我有一个查询,可以通过普通文本列查询,而在JSON列中进行了值。我想知道如何为查询创建最佳索引?

这是查询:

explain select * from "tags" where "slug"->>'en' = 'slugName' 
and "type" in ('someType1','someType1');
-------
Seq Scan on tags  (cost=0.00..1.47 rows=1 width=888)
"  Filter: (((type)::text = ANY ('{dsfdsf,fgsdf}'::text[])) AND ((slug ->> 'en'::text) = 'dsfdsf'::text))"

“ slug”列是类型JSON,“类型”列是类型Varchar(191)。我很熟悉我可以在JSON列中添加索引:

CREATE INDEX tag_slug_index ON tags USING btree ((slug ->> 'en'));

但是我想知道,如何在slug名称上与类型列相结合?

I have a query where I query by a normal text column and a value in a JSON column. What I'm wondering is how to create the best index for the query?

This is the query:

explain select * from "tags" where "slug"->>'en' = 'slugName' 
and "type" in ('someType1','someType1');
-------
Seq Scan on tags  (cost=0.00..1.47 rows=1 width=888)
"  Filter: (((type)::text = ANY ('{dsfdsf,fgsdf}'::text[])) AND ((slug ->> 'en'::text) = 'dsfdsf'::text))"

The "slug" column is type JSON and the "type" column is type varchar(191). I'm familiar that I can add an index to the JSON column like:

CREATE INDEX tag_slug_index ON tags USING btree ((slug ->> 'en'));

But I'm wondering, how do I create a multi-column index on the slug name combined with the type column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-02-13 14:34:36

没有什么特别的,您只需通过逗号将它们分开来以正常方式进行:

CREATE INDEX ON tags USING btree ("type", (slug ->> 'en'));

表达式仍然需要额外的括号中,同样是索引中唯一的“列”。

There is nothing special about it, you just do it the normal way, by separating them with a comma:

CREATE INDEX ON tags USING btree ("type", (slug ->> 'en'));

The expression does still need to be in an extra set of parentheses, same is if it were the only 'column' in the index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文