Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Ive got a small database in Postgres, about 10,000 records storing company customers.
I have a "slow" performing query (about half a second) which is performed very frequently, and my boss wants me to improve it.

First off - my code:

select customer_alias.id, customer_alias.name, site.address, phone.phonenumber
from customer_alias
join customer on customer_alias.customer_id = customer.id
left join site on customer.default_site_id = site.id
left join contact_phonenumbers as phone on site.default_phonenumber = phone.id

(Edited left join customer to join customer)

What leaps out to me is I am performing a join to customer even though i am not selecting anything from that record. I currently have to join it to get the default_site_id, a foreign key to the site table.

Each customer can have multiple sites, but only one should be displayed in this list (a customer has to be opened to view all sites). So my question is, if i cant optimize the query, is there a different way i can store a default site for a particular customer? The same goes for the default phonenumber

A customer can have many sites, but a site has only one customer( many to one).


EXPLAIN returns:

Hash Join  (cost=522.72..943.76 rows=5018 width=53)
  Hash Cond: (customer.id = customer_alias.customer_id)
  ->  Hash Right Join  (cost=371.81..698.77 rows=5018 width=32)
        Hash Cond: (site.id = customer.default_site_id)
        ->  Hash Right Join  (cost=184.91..417.77 rows=5018 width=32)
              Hash Cond: (phone.id = site.default_phonenumber)
              ->  Seq Scan on contact_phonenumbers phone  (cost=0.00..121.70 rows=6970 width=17)
              ->  Hash  (cost=122.18..122.18 rows=5018 width=23)
                    ->  Seq Scan on site  (cost=0.00..122.18 rows=5018 width=23)
        ->  Hash  (cost=124.18..124.18 rows=5018 width=8)
              ->  Seq Scan on customer  (cost=0.00..124.18 rows=5018 width=8)
  ->  Hash  (cost=88.18..88.18 rows=5018 width=29)
        ->  Seq Scan on customer_alias  (cost=0.00..88.18 rows=5018 width=29)

EXPLAIN ANALYZE returns:

Hash Join  (cost=522.72..943.76 rows=5018 width=53) (actual time=12.457..26.655 rows=5018 loops=1)
  Hash Cond: (customer.id = customer_alias.customer_id)
  ->  Hash Right Join  (cost=371.81..698.77 rows=5018 width=32) (actual time=8.589..18.796 rows=5018 loops=1)
        Hash Cond: (site.id = customer.default_site_id)
        ->  Hash Right Join  (cost=184.91..417.77 rows=5018 width=32) (actual time=4.499..11.067 rows=5018 loops=1)
              Hash Cond: (phone.id = site.default_phonenumber)
              ->  Seq Scan on contact_phonenumbers phone  (cost=0.00..121.70 rows=6970 width=17) (actual time=0.007..1.581 rows=6970 loops=1)
              ->  Hash  (cost=122.18..122.18 rows=5018 width=23) (actual time=4.465..4.465 rows=5018 loops=1)
                    Buckets: 1024  Batches: 1  Memory Usage: 277kB
                    ->  Seq Scan on site  (cost=0.00..122.18 rows=5018 width=23) (actual time=0.007..2.383 rows=5018 loops=1)
        ->  Hash  (cost=124.18..124.18 rows=5018 width=8) (actual time=4.072..4.072 rows=5018 loops=1)
              Buckets: 1024  Batches: 1  Memory Usage: 197kB
              ->  Seq Scan on customer  (cost=0.00..124.18 rows=5018 width=8) (actual time=0.009..2.270 rows=5018 loops=1)
  ->  Hash  (cost=88.18..88.18 rows=5018 width=29) (actual time=3.855..3.855 rows=5018 loops=1)
        Buckets: 1024  Batches: 1  Memory Usage: 309kB
        ->  Seq Scan on customer_alias  (cost=0.00..88.18 rows=5018 width=29) (actual time=0.008..1.664 rows=5018 loops=1)
Total runtime: 27.290 ms"

Table structures


/* ---------------------------------------------------------------------------
 * Table: contacts.customer
 * -------------------------------------------------------------------------*/
CREATE TABLE contacts.customer (
    id                  SERIAL NOT NULL,
    name                integer,   -- Foreign key to contacts.customer_alias
    default_site_id     integer,   -- Foreign key to contacts.site
    -- 12 other fields unrelated to query
    CONSTRAINT customer_pkey PRIMARY KEY (id )
    CONSTRAINT default_site_id FOREIGN KEY (default_site_id)
    REFERENCES contacts.site (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION;
);

-- circular foreign key to customer_alias added later

-- Indexed: id, name, default_site_id (btree)


/*----------------------------------------------------------------------------
 * Table: contacts.customer_alias
 *--------------------------------------------------------------------------*/
CREATE TABLE contacts.customer_alias (
    id              SERIAL NOT NULL,
    customer_id     integer,
    name            text,
    -- 5 other fields (not used in query)
    CONSTRAINT customer_alias_pkey PRIMARY KEY (id ),
    CONSTRAINT customer_alias_customer_id_fkey FOREIGN KEY (customer_id)
         REFERENCES contacts.customer (id) MATCH SIMPLE
         ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
-- indexed: id, customer_id (btree)

-- Customer_alias foreign key
ALTER TABLE contacts.customer
    ADD CONSTRAINT customer_name_fkey FOREIGN KEY (name)
         REFERENCES contacts.customer_alias (id) MATCH SIMPLE
         ON UPDATE NO ACTION ON DELETE NO ACTION;


/* ---------------------------------------------------------------------------
 * Table: contacts.site
 * -------------------------------------------------------------------------*/
CREATE TABLE contacts.site (
    id                  SERIAL NOT NULL,
    customer_id         integer,
    address             text,
    default_contact_id  integer,
    default_phonenumber integer,
    -- 9 other unrelated fields
    CONSTRAINT site_pkey PRIMARY KEY (id ),
    CONSTRAINT site_customer_id_fkey FOREIGN KEY (customer_id)
         REFERENCES contacts.customer (id) MATCH SIMPLE
         ON UPDATE NO ACTION ON DELETE NO ACTION
)
indexed: id, customer_id, default_contact_id, default_phonenumber ( btree)


/* ---------------------------------------------------------------------------
 * Table: contacts.contact_phonenumbers
 * -------------------------------------------------------------------------*/
CREATE TABLE contacts.contact_phonenumbers (
    id              SERIAL NOT NULL,
    site_id         integer,
    phonenumber     text,
    -- 4 other unrelated fields
    CONSTRAINT contact_phonenumbers_pkey PRIMARY KEY (id )
)
-- indexed: id, site_id (btree)

If I run the query client side, through ODBC, it takes between 450-500 milliseconds. If I run the query in pgAdmin III, it states the query takes approx 250 milliseconds, although sometimes it takes 60-100ms (which is what I'm aiming for).
I don't currently have SSH access to the server so I can't run it directly.

I only view about 100 of these rows at any time on screen, is it possible to retrieve only the relevant rows? I tried limiting the result for instance LIMIT 100, OFFSET 2345, but that performs a fresh search every time.

Thanks very much for the help so far!

share|improve this question

migrated from stackoverflow.com Mar 15 '12 at 16:27

This question came from our site for professional and enthusiast programmers.

    
It looks like you need customer to get to site. What does EXPLAIN give you? –  swasheck Mar 15 '12 at 16:04
    
Another question: LEFT JOIN will return NULL values if no match is found. Is this a requirement of this report? –  swasheck Mar 15 '12 at 16:15
    
Thanks swasheck, yes it needs to display customers that don't have any sites at all (so the user can add them). I'll edit my post to add the EXPLAIN –  berkeleybross Mar 15 '12 at 16:18
1  
The index is not ignored (see my comment to the answer of @swasheck below). Can you please post the definition of the tables/views used in the query (relevant parts only: PKs and FKs, indexes). –  tscho Mar 15 '12 at 18:13
3  
Please update the EXPLAIN with an EXPLAIN ANALYZE. Differences between planner estimated costs and actual execution can point to the real issue. –  dbenhur Mar 15 '12 at 20:18

1 Answer 1

up vote 5 down vote accepted

You write:

Each customer can have multiple sites, but only one should be displayed in this list.

Yet, your query retrieves all rows. That would be a point to optimize. But you also do not define which site is to be picked.

Either way, it does not matter much here. Your EXPLAIN shows only 5026 rows for the site scan (5018 for the customer scan). So hardly any customer actually has more than one site. Did you ANALYZE your tables before running EXPLAIN?

From the numbers I see in your EXPLAIN, indexes will give you nothing for this query. Sequential table scans will be the fastest possible way. Half a second is rather slow for 5000 rows, though. Maybe your database needs some general performance tuning?

Maybe the query itself is faster, but "half a second" includes network transfer? EXPLAIN ANALYZE would tell us more.

If this query is your bottleneck, I would suggest you implement a materialized view.


After you provided more information I find that my diagnosis pretty much holds.

The query itself needs 27 ms. Not much of a problem there. "Half a second" was the kind of misunderstanding I had suspected. The slow part is the network transfer (plus ssh encoding / decoding, possibly rendering). You should only retrieve 100 rows, that would solve most of it, even if it means to execute the whole query every time.

If you go the route with a materialized view like I proposed you could add a serial number without gaps to the table plus index on it - by adding a column row_number() OVER (<your sort citeria here>) AS mv_id.

Then you can query:

SELECT *
FROM   materialized_view
WHERE  mv_id >= 2700
AND    mv_id <  2800;

This will perform very fast. LIMIT / OFFSET cannot compete, that needs to compute the whole table before it can sort and pick 100 rows.


pgAdmin timing

When you execute a query from the query tool, the message pane shows something like:

Total query runtime: 62 ms.

And the status line shows the same time. I quote pgAdmin help about that:

The status line will show how long the last query took to complete. If a dataset was returned, not only the elapsed time for server execution is displayed, but also the time to retrieve the data from the server to the Data Output page.

If you want to see the time on the server you need to use SQL EXPLAIN ANALYZE or the built in Shift + F7keyboard shortcut or Query -> Explain analyze. Then, at the bottom of the explain output you get something like this:

Total runtime: 0.269 ms
share|improve this answer
    
After creating a cursor the search is pretty much instant. Thanks for bringing attention to the real bottleneck, Ill accept as answer Out of curiosity, do you know why PgAdmin reports 250ms time? Is that also reporting data transfer time? I thought it reports running time on server... –  berkeleybross Mar 16 '12 at 15:10
    
@berkeleybross: EXPLAIN ANALYZE reports the time on server. pgAdmin reports the time until it gets the response, including data transfer. I added a quote to my answer. –  Erwin Brandstetter Mar 16 '12 at 18:20
    
Thanks very much! If I could upvote your answer I would. I'll come back when I get 15 rep –  berkeleybross Mar 16 '12 at 18:50
    
@ErwinBrandstetter Please explain " serial number without gaps to the table" I didnt understand. –  vedic Feb 26 at 7:02
1  
@vedic: I added a recipe for the serial number to the answer above. –  Erwin Brandstetter Feb 26 at 15:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.