Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id F06E89552D4 for ; Tue, 28 Feb 2012 08:38:52 -0400 (AST) Received: from marvin.klingler.net ([91.212.121.244]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S2MKQ-0003Hc-TU for pgsql-sql@postgresql.org; Tue, 28 Feb 2012 12:38:52 +0000 Received: from [10.0.60.1] (vpn.klingler.net [213.193.121.98]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by marvin.klingler.net (Postfix) with ESMTPSA id C579933D2C for ; Tue, 28 Feb 2012 13:38:34 +0100 (CET) Date: Tue, 28 Feb 2012 13:38:35 +0100 From: Richard Klingler To: pgsql-sql@postgresql.org Message-ID: <20120228133835204823.bbf29d1f@klingler.net> In-Reply-To: References: <20111217113306531983.1eb85a86@klingler.net> Subject: Re: Natural sort order MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: quoted-printable X-Mailer: GyazMail version 1.5.12 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/95 X-Sequence-Number: 36388 Took some time until I could try out this... But as soon I want to create the fcuntion based index it tells me: =09Error : ERROR: functions in index expression must be marked IMMUTABLE Deleteing the sort function and recreating with the IMMUTABLE attribute giv= es the same error.. Here the functions: CREATE FUNCTION btrsort_nextunit(text) RETURNS text AS $$ =09SELECT=20 =09=09CASE WHEN $1 ~ '^[^0-9]+' THEN =09=09=09COALESCE( SUBSTR( $1, LENGTH(SUBSTRING($1 FROM '[^0-9]+'))+1 ), ''= ) =09=09ELSE =09=09=09COALESCE( SUBSTR( $1, LENGTH(SUBSTRING($1 FROM '[0-9]+'))+1 ), '' = ) =09=09END $$ LANGUAGE SQL IMMUTABLE; CREATE FUNCTION btrsort(text) RETURNS text AS $$ =09SELECT=20 =09=09CASE WHEN char_length($1)>0 THEN =09=09=09CASE WHEN $1 ~ '^[^0-9]+' THEN =09=09=09=09RPAD(SUBSTR(COALESCE(SUBSTRING($1 FROM '^[^0-9]+'), ''), 1, 12)= , 12, ' ') || btrsort(btrsort_nextunit($1)) =09=09=09ELSE =09=09=09=09LPAD(SUBSTR(COALESCE(SUBSTRING($1 FROM '^[0-9]+'), ''), 1, 12),= 12, ' ') || btrsort(btrsort_nextunit($1)) =09=09=09END =09=09ELSE =09=09=09$1 =09=09END ; $$ LANGUAGE SQL IMMUTABLE; And the index creation: create index port_name_btrsort_index on port(btrsort(name)); Which should speed up my query: =09select * from port where name not like '%Z' order by btrsort(name) asc cheers richard On Sat, 17 Dec 2011 16:16:07 +0100, Filip Rembia=B3kowski wrote: > If you use btrsort(column) from the example, you can just create a > functional index on this expression. >=20 > CREATE INDEX mytable_column_btrsort_idx ON mytable( btrsort(column) ); >=20 > this can help. >=20 >=20 >=20 >=20 >=20 > 2011/12/17 Richard Klingler : >> Morning... >>=20 >> What is the fastest way to achieve natural ordering from queries? >>=20 >> I found a function at: >> http://2kan.tumblr.com/post/361326656/postgres-natural-ordering >>=20 >> But it increases the query time from around 0.4msecs to 74msecs... >> Might be not much if occasional queries are made..but I use it for >> building >> up a hierarchical tree menu in a web application where every msecs >> counts (o; >>=20 >>=20 >> cheers >> richard >>=20 >>=20 >> -- >> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) >> To make changes to your subscription: >> http://www.postgresql.org/mailpref/pgsql-sql >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql