Re: Conversion of string to int using digits at beginning

Поиск
Список
Период
Сортировка
От Sam Mason
Тема Re: Conversion of string to int using digits at beginning
Дата
Msg-id 20081119143313.GC2459@frubble.xen.chris-lamb.co.uk
обсуждение исходный текст
Ответ на Re: Conversion of string to int using digits at beginning  ("Andrus" <kobruleht2@hot.ee>)
Список pgsql-general
On Wed, Nov 19, 2008 at 12:50:23PM +0200, Andrus wrote:
> Sam,
> > SELECT MAX(nullif(regexp_replace(test, '^([0-9]*).*$', E'\\1'),'')::INT);
>
> Thank you.
> This seems to work but is bit slow.

It will have to be executed against every row before you get an answer,
so if you're just after the max of a whole table will be pretty slow.

> How to speed it up ?
> Should I create index
>
> CREATE INDEX test ON  test ( nullif(regexp_replace(test, '^([0-9]*).*$',
> E'\\1'),'')::INT );

that would work.  I'd be tempted to use the substring() function instead
as it looks a bit prettier (peformance seems indistuinguishable).  So
I'd use the following pair:

  CREATE INDEX test_test_idx ON test
    ((nullif(substring(test, '^[0-9]*'),'')::int));

and

  SELECT MIN(nullif(substring(test, '^[0-9]*'),'')::int) FROM test;

you could use a view as well, at which point you wouldn't have to
remember how you were converting the string into an int:

  CREATE VIEW test_v AS
    SELECT *, nullif(substring(test, '^[0-9]*'),'')::int AS test_int
    FROM test;

allowing a simple:

  SELECT MIN(test_int) FROM test_v;

hope that helps!


  Sam

В списке pgsql-general по дате отправления:

Предыдущее
От: "Grzegorz Jaśkiewicz"
Дата:
Сообщение: Re: tracking down a warning
Следующее
От: Sam Mason
Дата:
Сообщение: Re: Any risks in using FUNCTIONs (stored procedures) instead of raw sql queries?