pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreak@officenet.no>
To: Achilleus Mantzios <achill@matrix.gatewaynet.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: index on to_char(created, 'YYYY') doesn't work
Date: Wed, 15 Jan 2003 12:08:01 +0000
Message-ID: <200301151208.01894.andreak@officenet.no> (raw)
In-Reply-To: <Pine.LNX.4.44.0301151411490.2448-100000@matrix.gatewaynet.com>
References: <Pine.LNX.4.44.0301151411490.2448-100000@matrix.gatewaynet.com>

-----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 that
> > > 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:= 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);

Can't I have multicolumn-indexes with functions? Any idea how to rewrite that 
so it works?
Here is my session table:
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
);

Here is my query I wish to optimize using indexes:
SELECT to_char(created, 'IW') as week, count(session_id) from session WHERE 
username IS NULL and to_char(created, 'YYYY') = '2002' group by week ORDER BY 
week;

Any hints on optimizing this query, index-usage etc?

- -- 
Andreas Joseph Krogh <andreak@officenet.no>
	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+JU8hUopImDh2gfQRAuA5AKCXyqCZk92d6oCgyJ/Auf8c4xkSaQCgr4Lq
/+r2WSydbYWXNomMvbmt2E8=
=N6NQ
-----END PGP SIGNATURE-----




view thread (11+ messages)  latest in thread

Message-ID: <200301151208.01894.andreak@officenet.no>
Permalink:  ../200301151208.01894.andreak@officenet.no/
Also on:    postgresql.org/message-id/200301151208.01894.andreak@officenet.no

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: andreak@officenet.no, achill@matrix.gatewaynet.com
  Subject: Re: index on to_char(created, 'YYYY') doesn't work
  In-Reply-To: <200301151208.01894.andreak@officenet.no>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox