PostgreSQL中的串联索引

Luf*_*ude 2 sql postgresql indexing

所以基本上我是通过匹配2个表中的字符串来匹配地址

表B有500万行,所以我真的不想每次想匹配地址时都为其创建新列

因此,我考虑改为创建索引,以匹配地址的当前索引如下所示:

CREATE INDEX matchingcol_idx  ON tableB USING btree (sub_building_name || ', ' || building_name )
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,它不接受连接栏

我的更新查询将等于= b.sub_building_name || ','|| b.building_name

没有新的列和索引,这将需要多个小时

有没有一种方法而无需创建新的串联列?

a_h*_*ame 5

对于基于表达式的索引,需要将表达式放在括号之间:

CREATE INDEX matchingcol_idx  
   ON tableB USING btree ( (sub_building_name || ', ' || building_name) );
Run Code Online (Sandbox Code Playgroud)

但是,只有在子句中 使用完全相同的条件时,才会使用该索引where。仅引用列之一的任何条件都不会使用该索引。

  • @Luffydude:也许在((sub_building_name,building_name))上使用多列索引是更好的选择。但是没有您的查询和完整的表定义,这是不可能回答的 (2认同)