pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Jacobson <joel@gluefinance.com>
To: Sherry.CTR.Zhu@faa.gov
To: pgsql-performance@postgresql.org
Cc: Robert Haas <robertmhaas@gmail.com>
Subject: Re: LIMIT causes planner to do Index Scan using a less optimal index
Date: Wed, 7 Apr 2010 00:30:17 +0200
Message-ID: <o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com> (raw)
In-Reply-To: <OF25CDEB33.E5240CF8-ON852576FD.0069C312-852576FD.0069D841@faa.gov>
References: <OF5F73A084.4CC92971-ON852576FD.00699450-852576FD.00699A9E@LocalDomain>
	<OF25CDEB33.E5240CF8-ON852576FD.0069C312-852576FD.0069D841@faa.gov>

Actually, swapping the order of the conditions did in fact make some
difference, strange.

I ran the query a couple of times for each variation to see if the
difference in speed was just a coincidence or a pattern. Looks like the
speed really is different.

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((AccountID =
108) AND (Currency = 'SEK')) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1384.401..1384.402
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1384.399..1384.399
rows=1 loops=1)
         Filter: ((accountid = 108) AND (currency = 'SEK'::bpchar))
 Total runtime: 1384.431 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((Currency =
'SEK') AND (AccountID = 108)) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1710.166..1710.167
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1710.164..1710.164
rows=1 loops=1)
         Filter: ((currency = 'SEK'::bpchar) AND (accountid = 108))
 Total runtime: 1710.200 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((AccountID =
108) AND (Currency = 'SEK')) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1366.526..1366.527
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1366.525..1366.525
rows=1 loops=1)
         Filter: ((accountid = 108) AND (currency = 'SEK'::bpchar))
 Total runtime: 1366.552 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((Currency =
'SEK') AND (AccountID = 108)) ORDER BY TransactionID LIMIT 1;
                                                                     QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1685.395..1685.396
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1685.394..1685.394
rows=1 loops=1)
         Filter: ((currency = 'SEK'::bpchar) AND (accountid = 108))
 Total runtime: 1685.423 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((AccountID =
108) AND (Currency = 'SEK')) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1403.904..1403.905
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1403.903..1403.903
rows=1 loops=1)
         Filter: ((accountid = 108) AND (currency = 'SEK'::bpchar))
 Total runtime: 1403.931 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((Currency =
'SEK') AND (AccountID = 108)) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1689.014..1689.014
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1689.012..1689.012
rows=1 loops=1)
         Filter: ((currency = 'SEK'::bpchar) AND (accountid = 108))
 Total runtime: 1689.041 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((AccountID =
108) AND (Currency = 'SEK')) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1378.322..1378.323
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1378.320..1378.320
rows=1 loops=1)
         Filter: ((accountid = 108) AND (currency = 'SEK'::bpchar))
 Total runtime: 1378.349 ms
(4 rows)

EXPLAIN ANALYZE SELECT TransactionID FROM Transactions WHERE ((Currency =
'SEK') AND (AccountID = 108)) ORDER BY TransactionID LIMIT 1;
                                                                      QUERY
PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..116.02 rows=1 width=4) (actual time=1696.830..1696.831
rows=1 loops=1)
   ->  Index Scan using transactions_pkey on transactions
 (cost=0.00..1260254.03 rows=10862 width=4) (actual time=1696.828..1696.828
rows=1 loops=1)
         Filter: ((currency = 'SEK'::bpchar) AND (accountid = 108))
 Total runtime: 1696.858 ms
(4 rows)



2010/4/6 <Sherry.CTR.Zhu@faa.gov>

>
> I mean the time you spent on prune which one is cheaper might be another
> cost.
> Thanks much!
>
> Xuefeng Zhu (Sherry)
> Crown Consulting Inc. -- Oracle DBA
> AIM Lab Data Team
> (703) 925-3192
>
>
>
>  *Sherry CTR Zhu/AWA/CNTR/FAA*
> AJR-32, Aeronautical Information Mgmt Group
>
> 04/06/2010 03:13 PM
>   To
> Robert Haas <robertmhaas@gmail.com>
>  cc
> Joel Jacobson <joel@gluefinance.com>
>  Subject
> Re: [PERFORM] LIMIT causes planner to do Index Scan using a less
> optimal indexLink<Notes:///852576860052CAFA/DABA975B9FB113EB852564B5001283EA/15C2483F84B5A6D0852576FD006911AC>
>
>
>
> Have you tried before?
>
> Thanks much!
>
> Xuefeng Zhu (Sherry)
> Crown Consulting Inc. -- Oracle DBA
> AIM Lab Data Team
> (703) 925-3192
>
>
>
>  *Robert Haas <robertmhaas@gmail.com>*
>
> 04/06/2010 03:07 PM
>   To
> Sherry CTR Zhu/AWA/CNTR/FAA@FAA
> cc
> Joel Jacobson <joel@gluefinance.com>
> Subject
> Re: [PERFORM] LIMIT causes planner to do Index Scan using a less
> optimal index
>
>
>
>
> On Tue, Apr 6, 2010 at 3:05 PM, <*Sherry.CTR.Zhu@faa.gov*<Sherry.CTR.Zhu@faa.gov>>
> wrote:
>
> Just curious,
>
> Switch the where condition to try to make difference.
>
> how about change
> ((accountid = 108) AND (currency = 'SEK'::bpchar))
> to
> ( (currency = 'SEK'::bpchar) AND (accountid = 108) ).
>
>
> In earlier version of Oracle, this was common knowledge that optimizer took
> the last condition index to use.
>
> Ignore me if you think this is no sence.  I didn't have a time to read your
> guys' all emails.
>
> PostgreSQL doesn't behave that way - it guesses which order will be
> cheaper.
>
> ...Robert
>
>


-- 
Best regards,

Joel Jacobson
Glue Finance

E: jj@gluefinance.com
T: +46 70 360 38 01

Postal address:
Glue Finance AB
Box  549
114 11  Stockholm
Sweden

Visiting address:
Glue Finance AB
Birger Jarlsgatan 14
114 34 Stockholm
Sweden

view thread (11+ messages)  latest in thread

Message-ID: <o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com>
Permalink:  ../o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com/
Also on:    postgresql.org/message-id/o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: joel@gluefinance.com, Sherry.CTR.Zhu@faa.gov, robertmhaas@gmail.com
  Subject: Re: LIMIT causes planner to do Index Scan using a less optimal index
  In-Reply-To: <o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com>
  References: <OF5F73A084.4CC92971-ON852576FD.00699450-852576FD.00699A9E@LocalDomain> <OF25CDEB33.E5240CF8-ON852576FD.0069C312-852576FD.0069D841@faa.gov> <o2x8bdec0841004061530g672b7873l9c0bf041569c87ce@mail.gmail.com>

* 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