I have a query to a table in Postgres with an order based on a date field and a number field, this table has 1000000 records
The data types of the table are:
fcv_id = serial
fcv_fecha_comprobante = timestamp without time zone
fcv_numero_comprobante = varchar(60)
The query is:
SELECT fcv_id, fcv_fecha_comprobante FROM factura_venta
ORDER BY fcv_fecha_comprobante, fcv_numero_comprobante
This query takes about 5 seconds, but if I take out the "order by" the query takes only 0.499 seconds
The problem I have is that I need to run this query in the shortest time possible, so I search on google what can I do and create a composite index with the following query
CREATE INDEX factura_venta_orden ON factura_venta
USING btree (fcv_fecha_comprobante ASC NULLS LAST
, fcv_numero_comprobante ASC NULLS LAST);
ALTER TABLE factura_venta CLUSTER ON factura_venta_orden;
But the query is taking the same time or even more.
I'm using Postgres 9.0.13, here is the EXPLAIN with 73436 rows
Sort (cost=11714.03..11897.62 rows=73436 width=27) (actual time=1260.759..1579.853 rows=73436 loops=1)
Sort Key: fcv_fecha_comprobante, fcv_numero_comprobante
Sort Method: external merge Disk: 2928kB
-> Seq Scan on factura_venta (cost=0.00..4018.36 rows=73436 width=27) (actual time=0.363..162.558 rows=73436 loops=1)
Total runtime: 1694.882 ms
Postgres is running on a Phenon II 1055T (3 cores) With 8 GB Ram and 500 GB disk.
How I can optimize this query?