Re: why the big difference on this explain analyze?

From: "Dan Langille" <dan(at)langille(dot)org>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: why the big difference on this explain analyze?
Date: 2002-03-17 00:18:11
Message-ID: 20020317001814.27A883F28@bast.unixathome.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 16 Mar 2002 at 17:07, Dan Langille wrote:

> I was trying to get a query to run. Yes run. Not run fast. Just run.
> ;) It was taking far too long.
>
> Here is the before:
>
> explain
> fp2migration=#
> fp2migration=# explain
> fp2migration-# UPDATE Ports_Check SET found_in_ports = TRUE
> fp2migration-# WHERE EXISTS
> fp2migration-# ( SELECT *
> fp2migration(# FROM categories, element, ports
> fp2migration(# WHERE ports.category_id = categories.id
> fp2migration(# AND ports.element_id = element.id
> fp2migration(# AND categories.name =
> Ports_Check.category_name fp2migration(# AND
> element.name = Ports_Check.port_name); NOTICE: QUERY PLAN:
>
> Seq Scan on ports_check (cost=0.00..7226574.01 rows=3354 width=27)
> SubPlan
> -> Hash Join (cost=103.10..1077.13 rows=1 width=1073)
> -> Hash Join (cost=101.47..1075.49 rows=2 width=1030)
> -> Seq Scan on ports (cost=0.00..938.99 rows=6999
> width=1000)
> -> Hash (cost=101.40..101.40 rows=27 width=30)
> -> Index Scan using element_name on element
> (cost=0.00..101.40 rows=27 width=30)
> -> Hash (cost=1.62..1.62 rows=1 width=43)
> -> Seq Scan on categories (cost=0.00..1.62 rows=1
> width=43)

FWIW, I solved the high cost by adding an index:

create index ports_element_id on ports (element_id);

The plan is now:

fp2migration-# UPDATE Ports_Check SET found_in_ports = TRUE
fp2migration-# WHERE EXISTS
fp2migration-# ( SELECT *
fp2migration(# FROM categories, element, ports
fp2migration(# WHERE ports.category_id = categories.id
fp2migration(# AND ports.element_id = element.id
fp2migration(# AND categories.name =
Ports_Check.category_name
fp2migration(# AND element.name =
Ports_Check.port_name);
NOTICE: QUERY PLAN:

Seq Scan on ports_check (cost=0.00..1772151.04 rows=3354 width=39)
SubPlan
-> Hash Join (cost=1.63..264.13 rows=1 width=1060)
-> Nested Loop (cost=0.00..262.49 rows=2 width=1017)
-> Index Scan using element_name on element
(cost=0.00..100.78 rows=27 width=30)
-> Index Scan using ports_element_id on ports
(cost=0.00..6.01 rows=1 width=987)
-> Hash (cost=1.62..1.62 rows=1 width=43)
-> Seq Scan on categories (cost=0.00..1.62 rows=1
width=43)

EXPLAIN

Which takes about 7 seconds to run. Thank you.
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Christopher Kings-Lynne 2002-03-17 03:51:22 Re: Btree index extension question
Previous Message Stephan Szabo 2002-03-17 00:07:59 Re: Variable Substitution for table name