quick question: index optimisations on small tables

Started by Andrew Snowover 24 years ago5 messagesgeneral
Jump to latest
#1Andrew Snow
andrew@modulus.org

If I have:

CREATE TABLE small (
key integer PRIMARY KEY,
value text
);

and assuming there are only enough rows to fit in one page, doesn't it
make sense to use the index instead of a seq. scan for queries of type

SELECT value FROM small WHERE key = 12345;

Since it can get the answer straight out of the index, and if there are
potentially numerous rows, looking up a b-tree is faster than a linear
search?

- Andrew

#2Arne Weiner
aswr@gmx.de
In reply to: Andrew Snow (#1)
Re: quick question: index optimisations on small tables

Andrew Snow wrote:

If I have:

CREATE TABLE small (
key integer PRIMARY KEY,
value text
);

and assuming there are only enough rows to fit in one page, doesn't it
make sense to use the index instead of a seq. scan for queries of type

SELECT value FROM small WHERE key = 12345;

Since you have declared the column 'key' as PRIMARY KEY there is an
index on column 'key' anyway and SELECT value FROM small where key =
12345
will use that index: on my system psql said:

omicron=# EXPLAIN SELECT value FROM small WHERE key = 12345;
NOTICE: QUERY PLAN:

Index Scan using small_pkey on small (cost=0.00..8.14 rows=10 width=12)

Since it can get the answer straight out of the index, and if there are
potentially numerous rows, looking up a b-tree is faster than a linear
search?

Looking up from an index is of course faster than a seq. scan
(in almost all cases).

Arne.

Show quoted text

TIP 4: Don't 'kill -9' the postmaster

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Andrew Snow (#1)
Re: quick question: index optimisations on small tables

Andrew Snow writes:

Since it can get the answer straight out of the index, and if there are
potentially numerous rows, looking up a b-tree is faster than a linear
search?

You can't get the "answer straight out of the index". You always need to
check back in the real table to see whether the row is visible to your
transaction.

--
Peter Eisentraut peter_e@gmx.net http://funkturm.homeip.net/~peter

#4Andrew Snow
andrew@modulus.org
In reply to: Arne Weiner (#2)
Re: Re: quick question: index optimisations on small tables

Since you have declared the column 'key' as PRIMARY KEY there
is an index on column 'key' anyway and SELECT value FROM
small where key = 12345 will use that index: on my system psql said:

omicron=# EXPLAIN SELECT value FROM small WHERE key = 12345;
NOTICE: QUERY PLAN:

Index Scan using small_pkey on small (cost=0.00..8.14
rows=10 width=12)

Since it can get the answer straight out of the index, and if there
are potentially numerous rows, looking up a b-tree is faster than a
linear search?

Looking up from an index is of course faster than a seq. scan
(in almost all cases).

Hrmm... I have 26 rows in mine at the moment, and after vacuum
analyzing, it uses a seq. scan. How come yours used the index? I
thought mine wasn't using an index because postgres won't use an index
until the table is "big enough".

But if an index page is already in cache.. surely it'd be faster using
it than doing a seq. scan.

(Yes, I know its a small table, but I think the worst case for seq. scan
would be a fair bit worse than for the index, and every little bit
counts, right?)

- Andrew

#5Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Andrew Snow (#4)
Re: Re: quick question: index optimisations on small tables

On Fri, 31 Aug 2001, Andrew Snow wrote:

Hrmm... I have 26 rows in mine at the moment, and after vacuum
analyzing, it uses a seq. scan. How come yours used the index? I
thought mine wasn't using an index because postgres won't use an index
until the table is "big enough".

But if an index page is already in cache.. surely it'd be faster using
it than doing a seq. scan.

(Yes, I know its a small table, but I think the worst case for seq. scan
would be a fair bit worse than for the index, and every little bit
counts, right?)

If the table is small enough to fit in one page, a sequence scan across
those rows may be faster than the index scan since the index scan will
need to read two pages (one for the index, one for the heap -- the
visibility info is only in the heap so that must be consulted for
each index match)