Lists: | pgsql-general |
---|
From: | John Barham <jbarham(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Why sequential scan for currval? |
Date: | 2005-04-27 07:28:18 |
Message-ID: | 4f34febc0504270028579a6da5@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
test=# create table tt (id serial unique, s varchar);
[populate tt w/ 100000 rows]
test=# insert into tt (s) values ('foo');
test=# select currval('tt_id_seq');
currval
---------
100002
(1 row)
test=# explain select s from tt where id = 100002;
QUERY PLAN
---------------------------------------------------------------------
Index Scan using tt_id_key on tt (cost=0.00..6.01 rows=1 width=32)
Index Cond: (id = 100002)
(2 rows)
test=# explain select s from tt where id = currval('tt_id_key');
QUERY PLAN
------------------------------------------------------
Seq Scan on tt (cost=0.00..1734.42 rows=1 width=32)
Filter: (id = currval('tt_id_key'::text))
(2 rows)
Why is a sequential scan used when comparing id to currval() value vs.
index scan when compared to a constant?
TIA,
John
From: | John Barham <jbarham(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Why sequential scan for currval? |
Date: | 2005-04-27 07:32:27 |
Message-ID: | 4f34febc05042700323fa02243@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
> test=# explain select s from tt where id = currval('tt_id_key');
> QUERY PLAN
> ------------------------------------------------------
> Seq Scan on tt (cost=0.00..1734.42 rows=1 width=32)
> Filter: (id = currval('tt_id_key'::text))
> (2 rows)
should be:
test=# explain select s from tt where id = currval('tt_id_seq'); --
tt_id_seq vs. tt_id_key
QUERY PLAN
------------------------------------------------------
Seq Scan on tt (cost=0.00..1734.42 rows=1 width=32)
Filter: (id = currval('tt_id_seq'::text))
(2 rows)
but the question still holds.
John
From: | Klint Gore <kg(at)kgb(dot)une(dot)edu(dot)au> |
---|---|
To: | John Barham <jbarham(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Why sequential scan for currval? |
Date: | 2005-04-27 07:49:24 |
Message-ID: | 426F44044F.35D7KG@129.180.47.120 |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
On Wed, 27 Apr 2005 00:28:18 -0700, John Barham <jbarham(at)gmail(dot)com> wrote:
> test=# create table tt (id serial unique, s varchar);
> [populate tt w/ 100000 rows]
> test=# insert into tt (s) values ('foo');
> test=# select currval('tt_id_seq');
> currval
> ---------
> 100002
> (1 row)
> test=# explain select s from tt where id = 100002;
> QUERY PLAN
> ---------------------------------------------------------------------
> Index Scan using tt_id_key on tt (cost=0.00..6.01 rows=1 width=32)
> Index Cond: (id = 100002)
> (2 rows)
> test=# explain select s from tt where id = currval('tt_id_key');
> QUERY PLAN
> ------------------------------------------------------
> Seq Scan on tt (cost=0.00..1734.42 rows=1 width=32)
> Filter: (id = currval('tt_id_key'::text))
> (2 rows)
>
> Why is a sequential scan used when comparing id to currval() value vs.
> index scan when compared to a constant?
currval is volatile which means it can change from one row in a
statement to the next. So the scan has to be sequential to check if the
value of currval() has changed.
klint.
+---------------------------------------+-----------------+
: Klint Gore : "Non rhyming :
: EMail : kg(at)kgb(dot)une(dot)edu(dot)au : slang - the :
: Snail : A.B.R.I. : possibilities :
: Mail University of New England : are useless" :
: Armidale NSW 2351 Australia : L.J.J. :
: Fax : +61 2 6772 5376 : :
+---------------------------------------+-----------------+