Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dQCkD-0000xY-9e for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jun 2017 13:10:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dQCkC-00074z-8D for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jun 2017 13:10:56 +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.84_2) (envelope-from ) id 1dQCiR-0002zY-84 for pgsql-performance@postgresql.org; Wed, 28 Jun 2017 13:09:07 +0000 Received: from mail.fmed.uba.ar ([157.92.152.1] helo=azteca.fmed.uba.ar) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dQCiL-0004Yd-6k for pgsql-performance@postgresql.org; Wed, 28 Jun 2017 13:09:04 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id C1230E2126F0; Wed, 28 Jun 2017 10:08:55 -0300 (ART) Received: from azteca.fmed.uba.ar ([127.0.0.1]) by localhost (azteca.fmed.uba.ar [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id JeDuQmTd7EeQ; Wed, 28 Jun 2017 10:08:54 -0300 (ART) Received: from localhost (localhost.localdomain [127.0.0.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id E5305E21275F; Wed, 28 Jun 2017 10:08:54 -0300 (ART) X-Virus-Scanned: amavisd-new at fmed.uba.ar Received: from azteca.fmed.uba.ar ([127.0.0.1]) by localhost (azteca.fmed.uba.ar [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id or5e8hTf94Y9; Wed, 28 Jun 2017 10:08:54 -0300 (ART) Received: from azteca.fmed.uba.ar (azteca.fmed.uba.ar [157.92.152.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id C5158E2126F0; Wed, 28 Jun 2017 10:08:54 -0300 (ART) Date: Wed, 28 Jun 2017 09:08:54 -0400 (EDT) From: Gerardo Herzig To: Yevhenii Kurtov Cc: pgsql-performance@postgresql.org Message-ID: <1209632247.77753.1498655334706.JavaMail.zimbra@fmed.uba.ar> In-Reply-To: References: Subject: Re: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [157.92.152.69] X-Mailer: Zimbra 8.0.9_GA_6191 (ZimbraWebClient - FF45 (Linux)/8.0.9_GA_6191) Thread-Topic: [PERFORM] Thread-Index: gkngcLHqLm1CSEIYmtTrujkOpS/vSw== List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org ----- Mensaje original ----- > De: "Yevhenii Kurtov" > Para: pgsql-performance@postgresql.org > Enviados: Mi=C3=A9rcoles, 28 de Junio 2017 3:47:44 > Asunto: [PERFORM] >=20 > Hello, >=20 > We have a query that is run almost each second and it's very important to > squeeze every other ms out of it. The query is: >=20 > SELECT c0."id" FROM "campaign_jobs" AS c0 > WHERE (((c0."status" =3D $1) AND NOT (c0."id" =3D ANY($2)))) > OR ((c0."status" =3D $3) AND (c0."failed_at" > $4)) > OR ((c0."status" =3D $5) AND (c0."started_at" < $6)) > ORDER BY c0."priority" DESC, c0."times_failed" > LIMIT $7 > FOR UPDATE SKIP LOCKED >=20 > I added following index: >=20 > CREATE INDEX ON campaign_jobs(id, status, failed_at, started_at, priority > DESC, times_failed); >=20 > And it didn't help at all, even opposite - the planning phase time grew up > from ~2ms up to ~40 ms leaving execution time intact: >=20 > Limit (cost=3D29780.02..29781.27 rows=3D100 width=3D18) (actual > time=3D827.753..828.113 rows=3D100 loops=3D1) > -> LockRows (cost=3D29780.02..32279.42 rows=3D199952 width=3D18) (ac= tual > time=3D827.752..828.096 rows=3D100 loops=3D1) > -> Sort (cost=3D29780.02..30279.90 rows=3D199952 width=3D18) (= actual > time=3D827.623..827.653 rows=3D100 loops=3D1) > Sort Key: priority DESC, times_failed > Sort Method: external sort Disk: 5472kB > -> Seq Scan on campaign_jobs c0 (cost=3D0.00..22138.00 > rows=3D199952 width=3D18) (actual time=3D1.072..321.410 rows=3D200000 loo= ps=3D1) > Filter: (((status =3D 0) AND (id <> ALL > ('{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, > 23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42, > 43,44,45,46,47,48}'::integer[]))) OR ((status =3D 2) AND (failed_at > > '2017-06-22 03:18:09'::timestamp without time zone)) OR ((status =3D 1) A= ND > (started_at < '2017-06-23 03:11:09'::timestamp without time zone))) > Planning time: 40.734 ms > Execution time: 913.638 ms > (9 rows) >=20 >=20 > I see that query still went through the Seq Scan instead of Index Scan. Is > it due to poorly crafted index or because of query structure? Is it > possible to make this query faster? >=20 >=20 > Thanks >=20 Well, most of the time is spent ordering, and it is doing a (slow) disk sor= t. Try increasing work_mem for a in-memory sort. How many rows in campaign_jobs? If the query is returning most of the rows = in the table, it will not going to use any index anyway. HTH Gerardo --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance