小编Zam*_*myr的帖子

如何在postgresql中为regexp_matches创建索引?

我有一个桌子产品

product_id | desciption                                     
============================================================
322919     | text {add}185{/add} text                       
322920     | text {add}184{/add} text {add}185{/add} text   
322921     | text {add}185{/add} text {add}187{/add} text
Run Code Online (Sandbox Code Playgroud)

使用like进行sql查询非常慢

SELECT product_id, desciption 
FROM product 
WHERE LOWER(desciption) like '%{add}185{/add}%'
> Time: 340,159s
Run Code Online (Sandbox Code Playgroud)

我只需要一个索引来搜索 {add}185{/add} 表达式。即需要为此表建立索引

SELECT product_id, regexp_matches (desciption, '(\{add\}\d+\{\/add\})', 'g') 
FROM product 
Run Code Online (Sandbox Code Playgroud)

返回:

product_id | regexp_matches 
================================================================================
322919     | {"{add}185{/add}"}
322920     | {"{add}184{/add}"}
322920     | {"{add}185{/add}"}
322921     | {"{add}185{/add}"}
322921     | {"{add}187{/add}"}
Run Code Online (Sandbox Code Playgroud)
  1. 为数据采样创建索引哪个更好?
  2. 在“WHERE”中使用哪个表达式更好?

sql postgresql indexing

4
推荐指数
1
解决办法
2273
查看次数

标签 统计

indexing ×1

postgresql ×1

sql ×1