[pgsql-jp: 29792] Re: substring を使用した場合の検索結果について

Hirokazu Matsumura matsumurah @ citizen.co.jp
2003年 5月 6日 (火) 17:19:30 JST


石田様、返信が遅くなって申し訳御座いません。
松村です。

> 上記の URL にも書いてありますが、和訳では、
> http://www.postgresql.jp/document/pg721doc/user/indexes-functional.
> html
> インデックス定義の関数は1つ以上の引数を取ることができますが、
> 定数ではなくテーブル列でなくてはいけません。
>
> なので、例えば、
>
> create function substr_1_3(text) returns text as
> 'select substr($1, 1, 3)' language 'sql' with (iscachable, isstrict);
>
> のような関数を定義して、
>
> create index col2_substr on test1 (substr_1_3(cols))
>
> としてやればうまくいくのではないでしょうか?

なるほど。和訳のページは既に拝見していたのですが、全然理解できませんで
した。すいません。
丁寧に例題まで出して頂き、又解りやすく説明して頂きまして大変有難うござ
います。

今さっき、「f_substr10」という関数をcreate functionして関数Indexを作成
して
試してみました。(名称等は適当です)
******************************************************************
substrにて
******************************************************************
explain analyze select * from test01 where substr(col1,1,10)
= '1234567890';

------------------------------------------------------------------
 Seq Scan on test01  (cost=0.00..9186.84 rows=108 width=249)
 (actual time=8745.45..8745.45 rows=0 loops=1)
   Filter: (substr((col1)::text, 1, 10) = '1234567890'::text)

 Total runtime: 8745.74 msec
(3 rows)

******************************************************************
f_substr10にて
******************************************************************
explain analyze select * from test01 where f_substr10(col1)
= '1234567890';

-------------------------------------------------------------------
 Index Scan using test01_idx01 on test01  (cost=0.00..434.22 rows=108
width=249)
 (actual time=0.61..0.61 rows=0 loops=1)
   Index Cond: (f_substr10((col1)::text) = '1234567890'::text)

 Total runtime: 0.91 msec
(3 rows)
******************************************************************

おお〜!石田様のおっしゃる通りIndexも使用されたみたいでとても速くなりま
した!(^^)

今回は10桁の項目を作り、そこに対してIndexを貼るというヘンな力技の対応
をしてしまいましたが、
今後の糧とさせて頂きます。
(暇があればこの様に修正したいのですが...)

どうも有難う御座いました!!

----------------------------------------------
松村 浩一
----------------------------------------------




pgsql-jp メーリングリストの案内