pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Arup Rakshit <ar@zeit.io>
Cc: Andreas Kretschmer <andreas@a-kretschmer.de>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Why my query not using index to sort?
Date: Mon, 1 Oct 2018 06:56:29 -0700
Message-ID: <381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.com> (raw)
In-Reply-To: <6F29D803-9EC7-4D80-BFDB-BDF740243A68@zeit.io>
References: <D973243A-5DDC-4B20-A1DF-A0C14A487D2A@zeit.io>
	<20311.1538141875@sss.pgh.pa.us>
	<97DDEE09-FE33-43DB-A451-665F3535B5BA@zeit.io>
	<e737e767-6e89-3c37-e86f-c82e13855a51@a-kretschmer.de>
	<40D9D4B8-7D62-4E6E-B6E9-3F83DACA2D45@zeit.io>
	<61708e66-dcc6-6490-b876-979eff8c3efd@a-kretschmer.de>
	<05A26EE2-964C-4F53-8DB5-CD86D3D46D86@zeit.io>
	<2c10559c-21fa-bb73-3814-cec5583ddb6c@aklaver.com>
	<327CBA05-3910-416D-B1E0-175C44A256AE@zeit.io>
	<2be603be-1506-5a3d-c086-1fdd11d83bd3@aklaver.com>
	<13BB58F6-A4D6-475A-A71A-F6B4CF780644@zeit.io>
	<761996F5-EF2E-48C9-A250-B41A63239E01@zeit.io>
	<f0d3bd39-f40b-c3da-8ce3-bd060e46c366@aklaver.com>
	<658B2138-3146-4BE8-946F-827B1453A38E@zeit.io>
	<50c0d333-b979-c34b-46bd-a686b429868f@aklaver.com>
	<6F29D803-9EC7-4D80-BFDB-BDF740243A68@zeit.io>

On 9/30/18 9:26 PM, Arup Rakshit wrote:
> Hello Adrian,
> 
> The one I said in gist is different query, and the previous mail I 
> posted another query because I was testing with different types of 
> queries. So 1.5, 1.7 is not for this current one. My main point now I am 
> trying to understand why it is not matching at all. The current query 
> timing in psql and production log is very close, but not the explain output.

1) If you want to track down what is happening you will need to provide 
a consistent test case. Jumping from query to query, dataset to dataset, 
condition to condition is not conducive to coming up with an answer.

2) What is the answer you are looking for? Or to put it another way what 
is the problem you are trying to solve?

3) Taking 1) & 2) into account you need to do something along lines of:
a) Information about size of table.
b) A set query against said table.
c) Some indication of the problem the query is causing.

> 
> Regarding the file written time, I think it is not the time to write 
> into the file. Because If I don’t write this to the file still it shows 
> 14ms + always. I used the file not the sql output so that I can paste 
> here clean stuffs I want to show. If you want, I can show the output 
> without writing it to the external file, and you will see the same time.
> 
> 

Well you are looking at two different times. The EXPLAIN ANALYZE is 
showing the time to execute the query. The \timing is including the time 
to display the output which is why is comparable to what you see in the 
log. To illustrate:

select count(*) from projection ;
  count
-------
  28447
(1 row)

\timing
Timing is on.

  explain analyze select * from projection ;
                                                    QUERY PLAN 

----------------------------------------------------------------------------------------------------------------
  Seq Scan on projection  (cost=0.00..751.47 rows=28447 width=109) 
(actual time=0.012..3.853 rows=28447 loops=1)
  Planning time: 0.066 ms
  Execution time: 5.381 ms
(3 rows)



explain analyze select * from projection where p_item_no = 2;
                                                         QUERY PLAN 

---------------------------------------------------------------------------------------------------------------------------
  Index Scan using pj_pno_idx on projection  (cost=0.29..21.93 rows=5 
width=109) (actual time=0.021..0.032 rows=10 loops=1)
    Index Cond: (p_item_no = 2)
  Planning time: 0.117 ms
  Execution time: 0.061 ms
(4 rows)

select * from projection ; --Returns 28447 rows
Time: 56.186 ms

select * from projection where p_item_no = 2; --Returns 10 rows
Time: 0.372 ms

Repeating it:

explain analyze select * from projection ;
                                                    QUERY PLAN 

----------------------------------------------------------------------------------------------------------------
  Seq Scan on projection  (cost=0.00..751.47 rows=28447 width=109) 
(actual time=0.012..3.377 rows=28447 loops=1)
  Planning time: 0.056 ms
  Execution time: 4.759 ms
(3 rows)


explain analyze select * from projection where p_item_no = 2;
                                                         QUERY PLAN 

---------------------------------------------------------------------------------------------------------------------------
  Index Scan using pj_pno_idx on projection  (cost=0.29..21.93 rows=5 
width=109) (actual time=0.021..0.031 rows=10 loops=1)
    Index Cond: (p_item_no = 2)
  Planning time: 0.112 ms
  Execution time: 0.059 ms
(4 rows)


select * from projection ;
Time: 56.548 ms

select * from projection where p_item_no = 2;
Time: 0.463 ms





-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (25+ messages)

Message-ID: <381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.com>
Permalink:  ../381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.com/
Also on:    postgresql.org/message-id/381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.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-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, ar@zeit.io, andreas@a-kretschmer.de
  Subject: Re: Why my query not using index to sort?
  In-Reply-To: <381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.com>
  References: <D973243A-5DDC-4B20-A1DF-A0C14A487D2A@zeit.io> <20311.1538141875@sss.pgh.pa.us> <97DDEE09-FE33-43DB-A451-665F3535B5BA@zeit.io> <e737e767-6e89-3c37-e86f-c82e13855a51@a-kretschmer.de> <40D9D4B8-7D62-4E6E-B6E9-3F83DACA2D45@zeit.io> <61708e66-dcc6-6490-b876-979eff8c3efd@a-kretschmer.de> <05A26EE2-964C-4F53-8DB5-CD86D3D46D86@zeit.io> <2c10559c-21fa-bb73-3814-cec5583ddb6c@aklaver.com> <327CBA05-3910-416D-B1E0-175C44A256AE@zeit.io> <2be603be-1506-5a3d-c086-1fdd11d83bd3@aklaver.com> <13BB58F6-A4D6-475A-A71A-F6B4CF780644@zeit.io> <761996F5-EF2E-48C9-A250-B41A63239E01@zeit.io> <f0d3bd39-f40b-c3da-8ce3-bd060e46c366@aklaver.com> <658B2138-3146-4BE8-946F-827B1453A38E@zeit.io> <50c0d333-b979-c34b-46bd-a686b429868f@aklaver.com> <6F29D803-9EC7-4D80-BFDB-BDF740243A68@zeit.io> <381c04e8-583b-869d-82cf-7fe0beede79f@aklaver.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