J'ai environ 5 millions de lignes dans une table postgres. Je voudrais savoir combien de lignes correspondent start_time >= NOW()
, mais malgré un index sur start_time la requête est extrêmement lente (de l'ordre de plusieurs heures).Problèmes de performances des requêtes de date PostgreSQL
EXPLAIN SELECT COUNT(*) FROM core_event WHERE start_time >= NOW();
Aggregate (cost=449217.81..449217.82 rows=1 width=0)
-> Index Scan using core_event_start_time on core_event (cost=0.00..447750.83 rows=586791 width=0)
Index Cond: (start_time >= now())
est ici les informations de schéma pour la table:
id | integer | not null default nextval('core_event_id_seq'::regclass)
source | character varying(100) | not null
external_id | character varying(100) |
title | character varying(250) | not null
location | geometry | not null
start_time | timestamp with time zone |
stop_time | timestamp with time zone |
thumb | character varying(300) |
image | character varying(100) |
image_thumb | character varying(100) |
address | character varying(300) |
description | text |
venue_name | character varying(100) |
website | character varying(300) |
city_id | integer |
category | character varying(100) |
phone | character varying(50) |
place_id | integer |
image_url | character varying(300) |
event_type | character varying(200) |
hidden | boolean | not null
views | integer | not null
added | timestamp with time zone |
Je index sur les champs suivants:
city_id
external_id (unique)
location
location_id
place_id
start_time
Est-il un moyen facile pour moi d'accélérer la requête (par exemple, un index partiel), ou vais-je devoir recourir au partitionnement des données par date?
http://wiki.postgresql.org/wiki/Slow_Counting – StefanNch
@StefanNch - Aïe – MatBailie
Cela s'applique-t-il vraiment? En lisant le lien wiki, le lien de @ StefanNch n'est pertinent que lorsque count n'est pas limité à un champ utilisé dans un index. –