Use order by clause, got index scan involved

From: 高健 <luckyjackgao(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Use order by clause, got index scan involved
Date: 2012-11-08 07:41:00
Message-ID: CAL454F3xtQm8dKNqwJ8CoNvdpKYVy5KqXMsFmjwkF1Dys-FJ5A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all:

What confused me is that: When I select data using order by clause, I got
the following execution plan:

postgres=# set session
enable_indexscan=true;

SET

postgres=# explain SELECT * FROM pg_proc ORDER BY
oid;

QUERY
PLAN

----------------------------------------------------------------------------------------

Index Scan using pg_proc_oid_index on pg_proc (cost=0.00..321.60
rows=2490 width=552)

(1
row)

postgres=#

My Question is :

If I want to find record using the where clause which hold the id column,
the index scan might be used.

But I just want to get all the records on sorted output format, Why
index scan can be used here?

I can’t imagine that:

Step 1 Index is read into memory, then for each tuple in it,

Step 2 Then we got the address of related data block, and then access the
data block .

Step 2 will be repeated for many times. I think it is not efficient.

But comparing with sort , I got that even index scan with all the entry ,
the cost is still lower than sort operation:

postgres=# set session enable_indexscan=false;

SET

postgres=# explain SELECT * FROM pg_proc ORDER BY oid;

QUERY PLAN

-------------------------------------------------------------------

Sort (cost=843.36..849.59 rows=2490 width=552)

Sort Key: oid

-> Seq Scan on pg_proc (cost=0.00..86.90 rows=2490 width=552)

(3 rows)

postgres=#

That is to say: cost of seq scan + sort > cost of index scan for every
index entry + cost of access for every related data ?

Maybe the database system is clever enough to accumulate data access for
same physical page, and reduce the times of physical page acess ?

And can somebody kindly give some more detailed information which help to
know the execution plan calculation process?

Thanks in advance.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message pantelis vlachos 2012-11-08 07:49:20 find a substring on a text (data type) column
Previous Message 高健 2012-11-08 07:17:14 How is execution plan cost calculated for index scan