[pgsql-jp: 29043] Re: Like を使った前方一致検索時のインデックス使用条件について
Nakagawa Yasuharu
yasuharu.nakagawa @ jp.yokogawa.com
2003年 2月 14日 (金) 11:42:33 JST
中川@東京武蔵野です。
関数インデックスを使うといいかもって話が出ていましたが、どうでしょう?
検索列の頭2文字に意味があるとしたら、その部分をsubstrなどで切り出して
インデックスを作ってみると良いと思います。
以下は、私のところで使っている関数インデックスの例です。
date型のデータフィールドを、年月で切り出すのに使っています。
関数定義:
CREATE FUNCTION func_ym(date) RETURNS integer AS '
declare ret integer ;
begin
select into ret
(date_part(''year'',$1)::integer*12 + date_part(''month'',$1)::integer) ;
return ret ;
end ;
' LANGUAGE 'plpgsql' WITH ( iscachable );
関数インデックス定義:
CREATE INDEX t1_ym_idx ON t1
USING btree (func_ym(v_date));
psqlでの結果:
foo=> explain select count(*) from t1 where func_ym(v_date) = 2002*12+12 ;
NOTICE: QUERY PLAN:
Aggregate (cost=935.83..935.83 rows=1 width=0)
-> Index Scan using t1_ym_idx on t1 (cost=0.00..935.10 rows=294 width=0)
EXPLAIN
foo=> explain select count(*) from t1 where date_part('year',v_date)*12+date_part('month',v_date) = 2002*12+12 ;
NOTICE: QUERY PLAN:
Aggregate (cost=1874.85..1874.85 rows=1 width=0)
-> Seq Scan on t1 (cost=0.00..1874.11 rows=294 width=0)
EXPLAIN
foo=> select count(*) from t1;
58838
foo=> select now();select count(*) from t1 where date_part('year',v_date)*12+date_part('month',v_date) = 24036 ;select now();
2003-02-14 11:21:35.123071+09
3051
2003-02-14 11:21:37.523397+09
foo=> select now(); select count(*) from t1 where func_ym(v_date) = 24036 ; select now() ;
2003-02-14 11:22:13.964638+09
3051
2003-02-14 11:22:13.98941+09
--
pgsql-jp メーリングリストの案内