From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kyle Moser <moser.kyle@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Hibernate generated query slow compared to 'equivalent' hand written one
Date: Fri, 14 Oct 2016 13:46:31 -0400
Message-ID: <12358.1476467191@sss.pgh.pa.us> (raw)
In-Reply-To: <CAAJSdFERiMvMdfqp4hHR5gW-XV_vfj8jEk4Bx06RaUkE47LSAw@mail.gmail.com>
References: <CAAJSdFERiMvMdfqp4hHR5gW-XV_vfj8jEk4Bx06RaUkE47LSAw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
Kyle Moser <moser.kyle@gmail.com> writes:
> The depesz link for explain (analyze, buffers) is shown below for 3
> different queries. The first two queries show a log dump of the postgres
> log, showing a query that was generated by Java Hibernate. The third query
> was one I wrote and ran in pgadmin that I think is similar to what
> Hibernate is doing.
It's not all that similar: according to the EXPLAIN output, the condition
Hibernate is generating is
Filter: ((FK_USER)::numeric = ANY ('{213,382,131,...,717}'::numeric[]))
whereas your handwritten query is generating
Index Cond: (fk_user = ANY ('{70,150,1248,1269,1530,...,199954}'::bigint[]))
IOW, Hibernate is telling the server that the parameters it's supplying
are NUMERIC not INTEGER, which results in a query using numeric_eq, which
can't be indexed by a bigint index.
If you can't find a hammer big enough to persuade Hibernate that it's
dealing with integers/bigints rather than numerics, you could probably
regain most of the performance by creating an index on (FK_USER::numeric).
BTW, why is one of your EXPLAINs showing the identifiers in upper case
and the other in lower case? One could be forgiven for wondering if
these were really against the same data.
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, moser.kyle@gmail.com
Subject: Re: Hibernate generated query slow compared to 'equivalent' hand written one
In-Reply-To: <12358.1476467191@sss.pgh.pa.us>
* 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