pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Jose Martinez <jmartinez@opencrowd.com>
Cc: pgsql-general@postgresql.org
Cc: pgsql-admin@postgresql.org
Subject: Re: Simple Query Very Slow
Date: Sat, 22 Dec 2012 13:06:21 +0100
Message-ID: <79F38E90-60F7-45AC-A3A0-E1EAC42515D6@gmail.com> (raw)
In-Reply-To: <CA+ixmX37YK+RZcLab_xXKywojFG8sXJFpHD3GWGZqNoo8yRrVA@mail.gmail.com>
References: <CA+ixmX37YK+RZcLab_xXKywojFG8sXJFpHD3GWGZqNoo8yRrVA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

> and here's my query
> 
> select * from ticket 
> where created between '2012-12-19 00:00:00' and  '2012-12-20 00:00:00'
> 
> This was working fine until the number of records started to grow (about 5 million) and now it's taking forever to return. 
> 
> Explain analyze reveals this:
> 
> "Index Scan using ticket_1_idx on ticket  (cost=0.00..10202.64 rows=52543 width=1297) (actual time=0.109..125.704 rows=53340 loops=1)"
> "  Index Cond: ((created >= '2012-12-19 00:00:00+00'::timestamp with time zone) AND (created <= '2012-12-20 00:00:00+00'::timestamp with time zone))"
> "Total runtime: 175.853 ms"

> Nothing works. What am I doing wrong? why is it selecting sequential scan? the indexes are supposed to make the query fast. Anything that can be done to optimize it?

It is not selecting sequential scan, you're looking at an index scan. That should be pretty fast, and it isn't that slow - that's still sub-second performance (0.176s).
Is that explain from the correct table? According to the results there are but 53 thousand rows in it, not anywhere near 5 million.

Perhaps your index got bloated? Do you have any long-running transactions still active that prevent cleaning up deprecated index entries? If that's the case, close the application that keeps that connection open and run a vacuum.
Are you vacuuming that table often enough?
If none of that helps, perhaps a REINDEX does.

Is that a dedicated database machine or is it also doing other stuff that's eating up resources?

You didn't mention what version of Postgres you're on or what OS you're using.

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (5+ messages)  latest in thread

Message-ID: <79F38E90-60F7-45AC-A3A0-E1EAC42515D6@gmail.com>
Permalink:  ../79F38E90-60F7-45AC-A3A0-E1EAC42515D6@gmail.com/
Also on:    postgresql.org/message-id/79F38E90-60F7-45AC-A3A0-E1EAC42515D6@gmail.com

 · 

reply

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-admin@postgresql.org
  Cc: haramrae@gmail.com, jmartinez@opencrowd.com, pgsql-general@postgresql.org
  Subject: Re: Simple Query Very Slow
  In-Reply-To: <79F38E90-60F7-45AC-A3A0-E1EAC42515D6@gmail.com>

* 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