2016-08-24 36 views
0

我的表結構是這樣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指數,它的速度很慢。 如何在此列上創建適當的索引?請幫助我。

+0

有你參加最後只有百分之嘗試過嗎?因爲諸如'%xxx%'這樣的查詢在所有afaik中都不使用索引。 – AndreyS

+0

是的,我嘗試過,但沒有運氣。 – ranjan

+0

'%'操作符不能使用索引。可以使用GIN索引的操作員列表列在手冊中:https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html –

回答

0

我以爲GIN索引只適用於tsvector數據類型?

影響你product_data到的tsvector類型,重新創建索引,並確保你投來的tsvector您的查詢:

SELECT * FROM Product WHERE ptype in (2,3) and '%steel%'::tsvector % any(product_data) offset 0 limit 10; 
+0

不,GIN索引也可用於數組。 –

+0

你有引用手嗎? – d1ll1nger

+0

手冊:https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html –

相關問題