Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DD129477266 for ; Wed, 15 Jan 2003 10:19:30 -0500 (EST) Received: from krogh.officenet.no (unknown [193.212.174.47]) by postgresql.org (Postfix) with ESMTP id 76CD2477148 for ; Wed, 15 Jan 2003 10:06:43 -0500 (EST) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by krogh.officenet.no (Postfix) with ESMTP id 768D7B7785; Wed, 15 Jan 2003 10:04:47 -0500 (EST) From: Andreas Joseph Krogh Organization: OfficeNet AS To: Achilleus Mantzios Subject: Re: index on to_char(created, 'YYYY') doesn't work Date: Wed, 15 Jan 2003 15:04:46 +0000 User-Agent: KMail/1.5 Cc: pgsql-sql@postgresql.org References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Description: clearsigned data Content-Disposition: inline Message-Id: <200301151504.47092.andreak@officenet.no> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/217 X-Sequence-Number: 11082 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 15 January 2003 18:55, Achilleus Mantzios wrote: > On Wed, 15 Jan 2003, Andreas Joseph Krogh wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA1 > > > > On Wednesday 15 January 2003 16:12, you wrote: > > > On Wed, 15 Jan 2003, Andreas Joseph Krogh wrote: > > > > -----BEGIN PGP SIGNED MESSAGE----- > > > > Hash: SHA1 > > > > > > > > On Wednesday 15 January 2003 11:37, you wrote: > > > > > The following does not work: > > > > > > > > > > create index session_u_idx on session (to_char(created, 'YYYY')); > > > > > ERROR: parser: parse error at or near "'YYYY'" at character 57 > > > > > > > > > > Can I make a function to do this and index using the result of th= at > > > > > funtion? Do anyone have an example of such a function? > > > > > > > > I tried the following function: > > > > - ----------------------------------------------------------------- > > > > create function drus (timestamp) returns varchar AS' > > > > DECLARE > > > > str_created VARCHAR; > > > > created ALIAS FOR $1; > > > > BEGIN > > > > str_created:=3D to_char(created, ''YYYY''); > > > > RETURN str_created; > > > > END; > > > > ' LANGUAGE 'plpgsql'; > > > > > > add > > > WITH (iscachable) > > > > Thank you, not _that_ works:-) > > But now this doesn't work: > > create index session_u_idx on session (drus(created), username); > > Functinal indexes are single column indexes. > > Why dont you change your function to: > > create function drus (timestamp,varchar) returns varchar A > > and return the concatenation of to_char(created, ''YYYY'')||$2 > > and then create the index as usual (passing the date and the username > as params to your function)???? OK, thank you. Let me explain what I want to do: I have the following schema for gathering statistics from the web: CREATE TABLE session ( session_id varchar(256) NOT NULL PRIMARY KEY, created timestamp DEFAULT 'now' NOT NULL, last_accessed timestamp NOT NULL, destroyed timestamp NOT NULL, username varchar -- Allow sessions from not logged in users ); create or replace function drus (timestamp) returns varchar AS' DECLARE str_created VARCHAR; created ALIAS FOR $1; BEGIN str_created:=3D to_char(created, ''YYYY''); RETURN str_created; END; ' LANGUAGE 'plpgsql' WITH (iscachable); create index session_u_idx on session (drus(created)) where username is nul= l; Now I want to get statistics for number of hits pr. week where users are no= t=20 lnogged in(username IS NULL) for the year 2002: select to_char(created, 'IW') as week, count(session_id) from session WHERE= =20 username IS NULL and drus(created) =3D '2002' group by week ORDER BY week; week | count - ------+------- 01 | 6321 18 | 74 19 | 12153 20 | 17125 21 | 22157 22 | 25316 23 | 24265 24 | 26234 25 | 28583 26 | 29156 27 | 28335 28 | 23587 29 | 23203 This table is quite large(900 000 rows) and the query takes several minutes= to=20 run, which makes the browser timeout. Do I have a design-issue here, should I rather batch-generate the stats in = its=20 own table so I don't have to process all the data(900 000 rows) each time. Is there any way to optimize/rewrite this query? Is the use of to_char on t= he=20 timestamp wrong, should I use another comparation method for getting the ye= ar=20 2002? - --=20 Andreas Joseph Krogh There will always be someone who agrees with you but is, inexplicably, a moron. gpg public_key: http://dev.officenet.no/~andreak/public_key.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE+JXiPUopImDh2gfQRAlf/AJ9xlcUDqa7NcXghtse8PAqQxkf1lACdEGxH vBXYxoFZnS6J35iQGw+14wE=3D =3DxCVY -----END PGP SIGNATURE-----