pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedPostgreSQL 9.3.4 Query Problems
4+ messages / 3 participants
[nested] [flat]
* PostgreSQL 9.3.4 Query Problems
@ 2014-07-21 05:07 Burgess, Freddie <FBurgess@Radiantblue.com>
0 siblings, 2 replies; 4+ messages in thread
From: Burgess, Freddie @ 2014-07-21 05:07 UTC (permalink / raw)
To: pgsql-bugs
Query planner is not selecting the most efficient index, can anything be done to correct this problem?
Thanks
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
loongrunningquery_07_18_14.txt (text/plain, 9.8K) download
explain analyze select count(*) as y0_ from RPI_SPONSER_REPORT this_
where this_.session_UID=1
and this_.ORIGIN_DATE_TIME between '2014-06-01 00:00:00'::timestamp without time zone and '2014-06-01 06:11:56.33'::timestamp without time zone
and (ST_within (this_.SPONSER_LOCATION,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F')
or ST_touches (this_.SPONSER_LOCATION,'010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'));
QUERY
PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=9.79..9.82 rows=1 width=0) (actual time=63912.612..63912.613 rows=1 loops=1)
-> Append (cost=0.00..9.78 rows=3 width=0) (actual time=63912.606..63912.606 rows=0 loops=1)
-> Seq Scan on rpi_sponser_report this_ (cost=0.00..0.00 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
Filter: ((origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone)
AND (sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
AND (session_uid = 1) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location)
OR _st_touches(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
-> Seq Scan on rpi_sponser_report_overflow this__1 (cost=0.00..72.40 rows=1 width=0) (actual time=0.000..0.000 rows=0 loops=1)
Filter: ((origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone)
AND (sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
AND (session_uid = 1) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location)
OR _st_touches(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
-> Index Scan using sidx_sponser_report_y2014m06 on rpi_sponser_report_y2014m06 this__2 (cost=0.55..5.09 rows=1 width=0) (actual time=63912.525..63912.525 rows=0 loops=1)
Index Cond: (sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
Filter: ((origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone) AND (session_uid = 1)
AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location) OR _st_touches
(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
Rows Removed by Filter: 3849011
Total runtime: 63913.610 ms
(12 rows)
Time: 73490.262 ms
-- Then I disable the spatial index
update pg_index set indisvalid = false where indexrelid = 'sidx_sponser_report_y2014m06'::regclass;
Indexes:
"rpi_sponser_report_y2014m06_pkey" PRIMARY KEY, btree (sponser_report_uid), tablespace "sponser_data_y2014"
"idx_sessiondatetime_rpi_sponser_report_y2014m06" btree (session_uid, origin_date_time), tablespace "sponser_data_y2014"
"idx_uuid_rpi_sponser_report_y2014m06" btree (sponser_report_uuid), tablespace "sponser_data_y2014"
"sidx_sponser_report_y2014m06" gist (sponser_location) INVALID, tablespace "sponser_data_y2014"
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=33612.11..33612.14 rows=1 width=0) (actual time=1124.165..1124.165 rows=1 loops=1)
-> Append (cost=0.00..33612.10 rows=3 width=0) (actual time=1124.161..1124.161 rows=0 loops=1)
-> Seq Scan on rpi_sponser_report this_ (cost=0.00..0.00 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1)
Filter: ((origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone)
AND (sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
AND (session_uid = 1) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location)
OR _st_touches(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
-> Seq Scan on rpi_sponser_report_overflow this__1 (cost=0.00..72.40 rows=1 width=0) (actual time=0.000..0.000 rows=0 loops=1)
Filter: ((origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone)
AND (sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
AND (session_uid = 1) AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location)
OR _st_touches(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
-> Index Scan using idx_sessiondatetime_rpi_sponser_report_y2014m06 on rpi_sponser_report_y2014m06 this__2 (cost=0.57..33607.41 rows=1 width=0) (actual time=1124.144..1124.144 rows=0 loops=1)
Index Cond: ((session_uid = 1) AND (origin_date_time >= '2014-06-01 00:00:00'::timestamp without time zone) AND (origin_date_time <= '2014-06-01 06:11:56.33'::timestamp without time zone))
Filter: ((sponser_location && '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)
AND (_st_contains('010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry, sponser_location)
OR _st_touches(sponser_location, '010300000001000000050000006787D1E89889F5BFFBA6BE01196EE53F1AF703F9588EF5BF6D9AC3A07D5FE53F0C2792E0B193F5BF6D9AC3A07D5FE53FC096C4F07198F5BFFBA6BE01196EE53F6787D1E89889F5BFFBA6BE01196EE53F'::geometry)))
Rows Removed by Filter: 1128161
Total runtime: 1124.355 ms
(11 rows)
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PostgreSQL 9.3.4 Query Problems
@ 2014-07-21 14:08 Matheus de Oliveira <matioli.matheus@gmail.com>
parent: Burgess, Freddie <FBurgess@Radiantblue.com>
1 sibling, 0 replies; 4+ messages in thread
From: Matheus de Oliveira @ 2014-07-21 14:08 UTC (permalink / raw)
To: Burgess, Freddie <FBurgess@radiantblue.com>; +Cc: pgsql-bugs
On Mon, Jul 21, 2014 at 2:07 AM, Burgess, Freddie <FBurgess@radiantblue.com>
wrote:
> Query planner is not selecting the most efficient index, can anything be
> done to correct this problem?
Perhaps you should post your problem to PERFORM list, and follow the
guideline at [1].
[1] https://wiki.postgresql.org/wiki/Slow_Query_Questions
--
Matheus de Oliveira
Analista de Banco de Dados
Dextra Sistemas - MPS.Br nĂvel F!
www.dextra.com.br/postgres
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PostgreSQL 9.3.4 Query Problems
@ 2014-07-21 15:33 Emre Hasegeli <emre@hasegeli.com>
parent: Burgess, Freddie <FBurgess@Radiantblue.com>
1 sibling, 1 reply; 4+ messages in thread
From: Emre Hasegeli @ 2014-07-21 15:33 UTC (permalink / raw)
To: Burgess, Freddie <FBurgess@Radiantblue.com>; +Cc: pgsql-bugs
> Query planner is not selecting the most efficient index, can anything be done to correct this problem?
Trimmed explain outputs:
> -> Index Scan using sidx_sponser_report_y2014m06
> Index Cond: (sponser_location && ...
> Filter: ((origin_date_time >= ...
> Rows Removed by Filter: 3849011
> Total runtime: 63913.610 ms
> -- Then I disable the spatial index
> -> Index Scan using idx_sessiondatetime_rpi_sponser_report_y2014m06
> Index Cond: ((session_uid = ...
> Filter: ((sponser_location && ...
> Rows Removed by Filter: 1128161
> Total runtime: 1124.355 ms
It looks like a problem caused by selectivity estimation. PostgreSQL
will choose the index which will return less rows. The second index
actually returns less rows than the first one, but probably
PostgreSQL does not know about it. There is more chance that
the selectivity estimation function of the && operator misguides
the planner. I do not know about PostGIS, but selectivity estimation
functions for geometrical data types in the core are just stubs
returning constants. So, I would not expect much.
Best solution I can think of is to avoid that kind of queries.
pgsql-general@, pgsql-performance@ or maybe PostGIS mailing lists
seems like a better place to ask for help.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PostgreSQL 9.3.4 Query Problems
@ 2014-07-22 15:13 Burgess, Freddie <FBurgess@Radiantblue.com>
parent: Emre Hasegeli <emre@hasegeli.com>
0 siblings, 0 replies; 4+ messages in thread
From: Burgess, Freddie @ 2014-07-22 15:13 UTC (permalink / raw)
To: Emre Hasegeli <emre@hasegeli.com>; +Cc: pgsql-bugs
I disabled the btree compound index to forced the planner to use the spatial index and visa versa, and the select count returns zero rows in both these cases.
I ran some other similar queries that did return rows and they matched in both cases (Using the gist index or Using the btree index).
I wish we could avoid this type of query but these are being generated by Hibernate. We are now strongly considering dropping the gist index altogether, since queries on the compound index run so much faster and seem to return the same data.
thanks for you help
________________________________________
From: Emre Hasegeli [emre@hasegeli.com]
Sent: Monday, July 21, 2014 11:33 AM
To: Burgess, Freddie
Cc: pgsql-bugs@postgresql.org
Subject: Re: [BUGS] PostgreSQL 9.3.4 Query Problems
> Query planner is not selecting the most efficient index, can anything be done to correct this problem?
Trimmed explain outputs:
> -> Index Scan using sidx_sponser_report_y2014m06
> Index Cond: (sponser_location && ...
> Filter: ((origin_date_time >= ...
> Rows Removed by Filter: 3849011
> Total runtime: 63913.610 ms
> -- Then I disable the spatial index
> -> Index Scan using idx_sessiondatetime_rpi_sponser_report_y2014m06
> Index Cond: ((session_uid = ...
> Filter: ((sponser_location && ...
> Rows Removed by Filter: 1128161
> Total runtime: 1124.355 ms
It looks like a problem caused by selectivity estimation. PostgreSQL
will choose the index which will return less rows. The second index
actually returns less rows than the first one, but probably
PostgreSQL does not know about it. There is more chance that
the selectivity estimation function of the && operator misguides
the planner. I do not know about PostGIS, but selectivity estimation
functions for geometrical data types in the core are just stubs
returning constants. So, I would not expect much.
Best solution I can think of is to avoid that kind of queries.
pgsql-general@, pgsql-performance@ or maybe PostGIS mailing lists
seems like a better place to ask for help.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2014-07-22 15:13 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-07-21 05:07 PostgreSQL 9.3.4 Query Problems Burgess, Freddie <FBurgess@Radiantblue.com>
2014-07-21 14:08 ` Matheus de Oliveira <matioli.matheus@gmail.com>
2014-07-21 15:33 ` Emre Hasegeli <emre@hasegeli.com>
2014-07-22 15:13 ` Burgess, Freddie <FBurgess@Radiantblue.com>
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