Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQGpE-00068M-2X for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 18:36:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQGpA-0003oi-AX for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 18:36:52 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQGp9-0003oT-Qx for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 18:36:52 +0000 Received: from mail-it0-x22c.google.com ([2607:f8b0:4001:c0b::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fQGp7-0004yX-7M for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 18:36:50 +0000 Received: by mail-it0-x22c.google.com with SMTP id y127-v6so14015945itd.1 for ; Tue, 05 Jun 2018 11:36:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=2TJ98pSyye6a1ixRbrvZm0qyTPzqbXSVD/6qjurdRWg=; b=Fly7MLaZLN2hPr2pivpXFoahzHCB70FV58tlEyM+fz/Wt5Rbm04On4HQ6bxCH0HHqu COv8bTItSa+TXPP23tpHuLj+G4IxsE1WZLcoamFEtgLLW9tAc3ad37tKLKSstLcLPBDo XWVbf5XrGJrItC5Yjwi55KqGr/KI4ForNqm+wv/hPERUF/Kmpk2t3ZC5ejxefSzCh8D2 bLHVUEPCRmosTSAK3w1hlFGBIccmvZbG6N//Ds3/iu+JF6Ez4crX+ipYWN+Vs+77LsiF RYKBehK+YOUiTxvxdozGvP0jtzJrPJXDBfUTLvYUa2fTc8HsK7b8jIVSeFvdgxAE78r3 rzTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=2TJ98pSyye6a1ixRbrvZm0qyTPzqbXSVD/6qjurdRWg=; b=TG4iVebB0SabRdk2RIXYglXiaAJUU6TrUb3wq14wQ17R0Fbtx8tv75VokaQGTiNVh9 SgFhaUv0IW9QsLyx2br6jrTAIEBdUIrHwczlE/xhL49htV40P+TpUau2JAHVQK7vnoNN 6fWh/qY7PlK2Sr+1yA4D0q0Sr/WR/Tn2aeosZqd0d8VdzNiqxmpJp6dIe1sc5HtoC2TY Y9VCWbTfTGOyuDdnVzzyF+HVlbqi78/vmp/aTLXWqg372WdS68Lg3RXAFn43AoXnME2n i+RE7g7Fns30wI94RYnkvxNBBnMqHBGRPFvjaQRitHYU5R8DPQc4g8bRK76XDoHoG4VY hNlg== X-Gm-Message-State: APt69E05s//gAd5yBQyDi9udLcy1K3GQpQDbJpJb2goFDbU0iTjI8HNR YqvUPmv5lKCUD1C443IUme8= X-Google-Smtp-Source: ADUXVKKbgz+hEOyK8uOieJpjxnEHrtD/UfaIyeWmmNdqnUPh3r41lqcKD4PSTAJ6kC6woZWpY/aqNQ== X-Received: by 2002:a24:c887:: with SMTP id w129-v6mr332679itf.121.1528223808402; Tue, 05 Jun 2018 11:36:48 -0700 (PDT) Received: from ?IPv6:2605:a601:2062:d00:dc9c:bd1c:7370:376a? ([2605:a601:2062:d00:dc9c:bd1c:7370:376a]) by smtp.gmail.com with ESMTPSA id e76-v6sm1024281itd.7.2018.06.05.11.36.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Jun 2018 11:36:47 -0700 (PDT) From: armand pirvu Message-Id: <346D4D06-DEB6-43DA-BBBC-9BE84CA11990@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_D9257572-BBE8-4668-94D1-B2C4C69B481D" Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: limit and query planner Date: Tue, 5 Jun 2018 13:36:16 -0500 In-Reply-To: Cc: "pgsql-generallists.postgresql.org" To: Pavel Stehule References: <59ED87E7-B582-481A-9FD8-CB77C7A3B268@gmail.com> X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_D9257572-BBE8-4668-94D1-B2C4C69B481D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jun 5, 2018, at 1:32 PM, Pavel Stehule = wrote: >=20 >=20 >=20 > 2018-06-05 20:24 GMT+02:00 armand pirvu >: > All >=20 > Please see below >=20 > explain analyze select * from sp_i2birst_reg_staging_test where evt_id = =3D 'ACSF17' =20 > and status=3D0 limit 10; > QUERY PLAN = =20 > = --------------------------------------------------------------------------= --------------------------------------------------------- > Limit (cost=3D0.00..1.29 rows=3D10 width=3D519) (actual = time=3D0.110..0.439 rows=3D10 loops=3D1) > -> Seq Scan on sp_i2birst_reg_staging_test (cost=3D0.00..548.40 = rows=3D4239 width=3D519) (actual time=3D0.109..0.429 rows=3D10 loops=3D1) > Filter: (((evt_id)::text =3D 'ACSF17'::text) AND (status =3D = 0)) > Rows Removed by Filter: 115 > Planning time: 3.022 ms > Execution time: 0.639 ms > (6 rows) >=20 >=20 >=20 > birstdb=3D# \d sp_i2birst_reg_staging_test > Table = "csischema.sp_i2birst_reg_staging_test" > Column | Type | = Modifiers =20 > = ---------------+-----------------------------+----------------------------= --------------------------------------------- > action_id | bigint | not null default = nextval('i2birst_reg_staging_action_id_seq'::regclass) > reg_uid | integer | not null > evt_uid | integer | not null > evt_id | character varying(10) | not null > operation | character varying(6) | not null > status | smallint | not null > category | character varying(20) | not null default = ''::character varying > add_date | timestamp with time zone | not null default now() > mod_date | timestamp with time zone | not null default now() > ingres_data | jsonb |=20 > thread_number | bigint | not null default 0 > start_time | timestamp without time zone |=20 > end_time | timestamp without time zone |=20 > Indexes: > "sp_i2birst_reg_staging_test_pkey" PRIMARY KEY, btree (action_id) > "sp_i2birst_reg_staging_test_idx" btree (status, evt_id, category) > Check constraints: > "sp_i2birst_reg_staging_test_status_check" CHECK (status =3D ANY = (ARRAY[0, 1, 2, 3])) >=20 > Even if add an index on evt_id and status same table scan >=20 > But=20 >=20 > select count(*) from sp_i2birst_reg_staging_test; > count=20 > ------- > 6860 >=20 > select count(*) from sp_i2birst_reg_staging_test where evt_id =3D = 'ACSF17' =20 > and status=3D0 ; > count=20 > ------- > 4239 >=20 > So I can see why the planner is choosing a table scan=20 >=20 > My question is: I suspect the limit simply limits the fethching to the = first n-records retrieved and has no implications whatsoever on the = planner, meaning the planner ignores it. Am I right or wrong ? >=20 > LIMIT is last clause and it is processed after aggregation.=20 >=20 > probably you would select count(*) from (select * from = sp_i2birst_reg_staging_test where evt_id =3D 'ACSF17' LIMIT 10) s; >=20 > more you have not index on evt_id column - there is composite index, = but the chance can be low >=20 > Regards >=20 > Pavel >=20 >=20 > Thanks > =E2=80=94 Armand >=20 >=20 >=20 >=20 >=20 >=20 Thank you Pavel I put the counts to show that the number of records retrieved without = limit relative to a plain select count(*) is far more than 5% and an = index is just from this very reason deemed useless, aka the restriction = is really non existent =E2=80=94 Armand --Apple-Mail=_D9257572-BBE8-4668-94D1-B2C4C69B481D Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On Jun 5, 2018, at 1:32 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:



2018-06-05 20:24 GMT+02:00 armand pirvu <armand.pirvu@gmail.com>:
All

Please see below

explain analyze select * from sp_i2birst_reg_staging_test where evt_id =3D= 'ACSF17'   
and status=3D0 limit 10;
                    =                     =                     = QUERY PLAN                  =                     =                     =    
-----------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=3D0.00..1.29 rows=3D10 width=3D519) (actual = time=3D0.110..0.439 rows=3D10 loops=3D1)
   ->  Seq Scan on sp_i2birst_reg_staging_test  = (cost=3D0.00..548.40 rows=3D4239 width=3D519) (actual time=3D0.109..0.429 = rows=3D10 loops=3D1)
         Filter: (((evt_id)::text =3D = 'ACSF17'::text) AND (status =3D 0))
         Rows Removed by Filter: 115
 Planning time: 3.022 ms
 Execution time: 0.639 ms
(6 rows)



birstdb=3D# \d sp_i2birst_reg_staging_test
                    =                  Table = "csischema.sp_i2birst_reg_staging_test"
    Column     |        =     Type            =  |                  =               Modifiers    =                     =        
---------------+-----------------------------+-------------------------------------------------------------------------
 action_id     | bigint        =               | not null default = nextval('i2birst_reg_staging_action_id_seq'::regclass)
 reg_uid       | integer      =                | not null
 evt_uid       | integer      =                | not null
 evt_id        | character varying(10)  =      | not null
 operation     | character varying(6)    =     | not null
 status        | smallint      =               | not null
=  category      | character varying(20)    =    | not null default ''::character varying
 add_date      | timestamp with time zone  =   | not null default now()
 mod_date      | timestamp with time zone  =   | not null default now()
 ingres_data   | jsonb          =              |
 thread_number | bigint            =           | not null default 0
 start_time    | timestamp without time zone |
 end_time      | timestamp without time zone |
Indexes:
    "sp_i2birst_reg_staging_test_pkey" PRIMARY = KEY, btree (action_id)
    "sp_i2birst_reg_staging_test_idx" btree = (status, evt_id, category)
Check constraints:
    "sp_i2birst_reg_staging_test_status_check" = CHECK (status =3D ANY (ARRAY[0, 1, 2, 3]))

Even if add an index on evt_id and status same table scan

But

select count(*) from sp_i2birst_reg_staging_test;
 count
-------
  6860

select count(*) from sp_i2birst_reg_staging_test where evt_id =3D = 'ACSF17'   
and status=3D0 ;
 count
-------
  4239

So I can see why the planner is choosing a table scan

My question is: I suspect the limit simply limits the fethching to the = first n-records retrieved and has no implications whatsoever on the = planner, meaning the planner ignores it. Am I right or wrong ?

LIMIT is last clause and it is processed after aggregation. =

probably you would select count(*) from (select * from = sp_i2birst_reg_staging_test where evt_id =3D 'ACSF17'  LIMIT 10) = s;

more you = have not index on evt_id column - there is composite index, but the = chance can be low

Regards

Pavel


Thanks
=E2=80=94 = Armand








Thank you Pavel

I put the counts to show that the = number of records retrieved without limit relative to a plain select = count(*) is far more than 5% and an index is just from this very reason = deemed useless, aka the restriction is really non existent

=E2=80=94 = Armand

= --Apple-Mail=_D9257572-BBE8-4668-94D1-B2C4C69B481D--