473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Bad planner results


I think this is just a bad case of "nested loops are costed wrong". But it's a
really really bad case. Like, even without any overlap in records looked up in
the nested loop I cannot imagine it ever being the wrong plan for these stats.
And Postgres is insistent on avoiding it, I have to disable both merge and
hash joins to get the 5ms nested loop join.

db=> EXPLAIN ANALYZE
SELECT distinct foo
FROM t1 join t1_t2 using (t1_id) join t2 using (t2_id)
WHERE bar = 232
;

QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=13036.55..13056.98 rows=33 width=4) (actual time=1040.238..1040.368 rows=11 loops=1)
-> Sort (cost=13036.55..13046.76 rows=4087 width=4) (actual time=1040.234..1040.291 rows=93 loops=1)
Sort Key: t1.foo
-> Hash Join (cost=267.80..12791.39 rows=4087 width=4) (actual time=12.511..1040.084 rows=93 loops=1)
Hash Cond: ("outer".t1_id = "inner".t1_id)
-> Hash Join (cost=125.43..12587.72 rows=4087 width=4) (actual time=1.344..1028.681 rows=93 loops=1)
Hash Cond: ("outer".t2_id = "inner".t2_id)
-> Seq Scan on t1_t2 (cost=0.00..9507.28 rows=582828 width=8) (actual time=0.003..572.865 rows=582828 loops=1)
-> Hash (cost=125.14..125.14 rows=117 width=4) (actual time=0.463..0.463 rows=0 loops=1)
-> Index Scan using idx_t2_bar on t2 (cost=0.00..125.14 rows=117 width=4) (actual time=0.028..0.331 rows=95 loops=1)
Index Cond: (bar = 232)
-> Hash (cost=133.89..133.89 rows=3389 width=8) (actual time=10.537..10.537 rows=0 loops=1)
-> Seq Scan on t1 (cost=0.00..133.89 rows=3389 width=8) (actual time=0.018..6.422 rows=3389 loops=1)
Total runtime: 1040.752 ms
(14 rows)

db=> set enable_hashjoin = false;

QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=15715.48..15735.92 rows=33 width=4) (actual time=98.095..98.230 rows=11 loops=1)
-> Sort (cost=15715.48..15725.70 rows=4087 width=4) (actual time=98.091..98.151 rows=93 loops=1)
Sort Key: t1.foo
-> Merge Join (cost=15233.28..15470.33 rows=4087 width=4) (actual time=85.827..97.943 rows=93 loops=1)
Merge Cond: ("outer".t1_id = "inner".t1_id)
-> Index Scan using t1_pkey on t1 (cost=0.00..167.27 rows=3389 width=8) (actual time=0.025..10.374 rows=3291 loops=1)
-> Sort (cost=15233.28..15243.50 rows=4087 width=4) (actual time=83.872..83.936 rows=93 loops=1)
Sort Key: t1_t2.t1_id
-> Merge Join (cost=129.16..14988.13 rows=4087 width=4) (actual time=13.992..83.729 rows=93 loops=1)
Merge Cond: ("outer".t2_id = "inner".t2_id)
-> Index Scan using t1_t2_all on t1_t2 (cost=0.00..13600.12 rows=582828 width=8) (actual time=0.016..58.722 rows=24533 loops=1)
-> Sort (cost=129.16..129.45 rows=117 width=4) (actual time=0.866..0.974 rows=163 loops=1)
Sort Key: t2.t2_id
-> Index Scan using idx_t2_bar on t2 (cost=0.00..125.14 rows=117 width=4) (actual time=0.024..0.648 rows=95 loops=1)
Index Cond: (bar = 232)
Total runtime: 98.406 ms
(16 rows)

db=> set enable_hashjoin = false;
SET
db=> set enable_mergejoin = false;
SET

QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=42624.09..42644.53 rows=33 width=4) (actual time=4.257..4.390 rows=11 loops=1)
-> Sort (cost=42624.09..42634.31 rows=4087 width=4) (actual time=4.255..4.310 rows=93 loops=1)
Sort Key: t1.foo
-> Nested Loop (cost=0.00..42378.94 rows=4087 width=4) (actual time=0.249..4.095 rows=93 loops=1)
-> Nested Loop (cost=0.00..29219.05 rows=4087 width=4) (actual time=0.220..2.247 rows=93 loops=1)
-> Index Scan using idx_t2_bar on t2 (cost=0.00..125.14 rows=117 width=4) (actual time=0.029..0.372 rows=95 loops=1)
Index Cond: (bar = 232)
-> Index Scan using t1_t2_code on t1_t2 (cost=0.00..245.63 rows=243 width=8) (actual time=0.012..0.015 rows=1 loops=95)
Index Cond: (t1_t2.t2_id = "outer".t2_id)
-> Index Scan using t1_pkey on t1 (cost=0.00..3.21 rows=1 width=8) (actual time=0.012..0.015 rows=1 loops=93)
Index Cond: (t1.t1_id = "outer".t1_id)
Total runtime: 4.564 ms
(12 rows)

--
greg
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
1 1721
Greg Stark <gs*****@mit.edu> writes:
I think this is just a bad case of "nested loops are costed wrong".


Looks to me like a statistical failure. Why does it think there will be
4000 rows out of that join when there are only 93?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michele | last post by:
Hi all, There's a web-based project I'm working on, which my boss insists should be implemented as a java web service: I know Java, but I'm a complete newcomer to web services, so I would badly...
0
by: Shailesh | last post by:
Hello, I am using Oracle 10g and have configured OEM grid control. I am searching for some tool like Capacity Planner which was present in Oracle 9i OEM as a part of Oracle EM9i Management...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
2
by: Chris Travers | last post by:
Hi all; I suspect I know what the answer here is, but I thought I would ask anyway. How transparent are SQL language functions from the planner's perspective? For example if I create a table:...
4
by: Fabrizio | last post by:
HI, I need to create a form that shows a planner (with employees and working days ) where i can use a different colors for "busy" days and "available" days (i'll read the data from a Database)....
3
by: Denis | last post by:
RE: http://www.rental-planner.com/mainscreen.jpg Hello everyone, I am trying to build a car rental bookings software but I am not sure how to implement the drag'n'drop functionality. ..you...
59
by: Jeff Bowden | last post by:
For ease of configuration and other reasons, I would like for my single-user GUI app to be able to use postgresql in-process as a library accessing a database created in the users home directory. ...
1
by: Martin Marques | last post by:
I was reading the section about Planner Method Configuration, and I just don't get why all the options have this message: "This is used for debugging the query planner". Are they all debugging...
2
by: Ed L. | last post by:
The planner is choosing a sequential scan for my query. I am trying to understand why since it is clearly not the fastest choice, and what the proper tuning dial is to adjust here. Here's the...
3
by: WB | last post by:
Hi, Is it poosible to build an appointment planner on a webform using asp:Calendar control? I would like to display one month at a time and show all the appointments scheduled in every day...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.