加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 15:54:30 所属栏目:百科 来源:网络整理
导读:我有一个PostgreSQL数据库表,其上定义了text [](数组)列.我正在使用这些列以这种方式在数据库中搜索特定记录: select obj from businesswhere ((('street' = ANY (address_line_1) and 'a_city' = ANY (city) and 'a_state' = ANY (state))or ('street' = A
我有一个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)表可能会更好地为您服务.从长远来看,它通常会起作用,即使它最初看起来更像是工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读