postgresql – Text []数组列的表索引

前端之家收集整理的这篇文章主要介绍了postgresql – Text []数组列的表索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Postgresql数据库表,其上定义了text [](数组)列.我正在使用这些列以这种方式在数据库搜索特定记录:

select obj from business
where ((('street' = ANY (address_line_1)
    and 'a_city' = ANY (city)
    and 'a_state' = ANY (state))
or    ('street' = ANY (address_line_1)
    and '1234' = ANY (zip_code)))
and ('a_business_name' = ANY (business_name)
    or 'a_website' = ANY (website_url)
    or array['123'] && phone_numbers))

我遇到的问题是,有大约100万条记录,查询变得非常慢.我的问题很简单,数组列有不同类型的索引吗?在这种情况下,有人知道要创建的最佳索引类型吗? (假设有不同的类型).

以防万一,这是解释分析响应:

"Seq Scan on business  (cost=0.00..207254.51 rows=1 width=32) (actual time=18850.462..18850.462 rows=0 loops=1)"
"  Filter: (('a'::text = ANY (address_line_1)) AND (('a'::text = ANY (business_name)) OR ('a'::text = ANY (website_url)) OR ('{123}'::text[] && phone_numbers)) AND ((('a'::text = ANY (city)) AND ('a'::text = ANY (state))) OR ('1234'::text = ANY (zip_code))))"
"  Rows Removed by Filter: 900506"
"Total runtime: 18850.523 ms"

提前致谢!

解决方法

您可以使用 GIN index有效地帮助阵列性能.
array operators结合使用.

例如:

CREATE INDEX business_address_line_1_idx ON business USING GIN (address_line_1);

对条件中涉及的所有数组列执行此操作.

可能值得考虑规范化您的架构.将多个条目拆分为单独的(1:n或n:m)表可能会更好地为您服务.从长远来看,它通常会起作用,即使它最初看起来更像是工作.

猜你在找的Postgre SQL相关文章