我的表結構是這樣PostgreSQL的:如何創建角色率變化數組類型列
CREATE TABLE Product
(
id bigserial NOT NULL,
seller_id integer,
product_data character varying[],
ptype integer,
CONSTRAINT config_pkey PRIMARY KEY (id)
)
創建索引:
proudct_data列可以有陣列中的最大各地50個不同的產品名稱並且產品表有大約1M UNIQUE行。 我需要找出seller_id,其中'steel'是產品,其中'steel'可能是product_data中產品名稱的子字符串;
目前我使用下面的查詢:
SELECT * FROM Product WHERE ptype in (2,3) and '%steel%' % any(product_data) offset 0 limit 10;
上述查詢給出了預期的效果,但因爲它沒有使用product_name_idx
指數,它的速度很慢。 如何在此列上創建適當的索引?請幫助我。
有你參加最後只有百分之嘗試過嗎?因爲諸如'%xxx%'這樣的查詢在所有afaik中都不使用索引。 – AndreyS
是的,我嘗試過,但沒有運氣。 – ranjan
'%'操作符不能使用索引。可以使用GIN索引的操作員列表列在手冊中:https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html –