I'm having very slow requests to my queries. I created an index on the filter columns and made partitioning but requests is still too slow. How can I make it faster? Here's query and explain:
SELECT
(
date_trunc('day', install_time)
) AS "d",
COUNT (
"application_events"."id"
) AS "agg"
FROM
"application_events"
WHERE
(
"application_events"."application_id" = 9
AND "application_events"."event_type" = 'install'
AND "application_events"."install_time" BETWEEN '2015-10-01 00:00:00+00:00'
AND '2015-10-14 23:59:59.999999+00:00'
)
GROUP BY
(
date_trunc('day', install_time)
)
EXPLAIN ANALYZE
output:
HashAggregate (cost=349746.20..349755.53 rows=746 width=12) (actual time=91074.961..91074.979 rows=14 loops=1)
-> Result (cost=3364.54..349742.47 rows=746 width=12) (actual time=5443.146..91065.907 rows=2232 loops=1)
-> Append (cost=3364.54..349740.61 rows=746 width=12) (actual time=5443.125..91047.422 rows=2232 loops=1)
-> Bitmap Heap Scan on application_events (cost=3364.54..222625.69 rows=737 width=12) (actual time=5443.124..64304.831 rows=2232 loops=1)
Recheck Cond: (application_id = 9)
Rows Removed by Index Recheck: 1325863
Filter: ((install_time >= '2015-10-01 00:00:00+00'::timestamp with time zone) AND (install_time <= '2015-10-14 23:59:59.999999+00'::timestamp with time zone) AND (event_type = 'install'::text))
Rows Removed by Filter: 170837
-> Bitmap Index Scan on application_events_application_id (cost=0.00..3364.36 rows=177590 width=0) (actual time=2889.887..2889.887 rows=173069 loops=1)
Index Cond: (application_id = 9)
-> Bitmap Heap Scan on application_events_y2015m09 (cost=1702.64..127114.92 rows=9 width=12) (actual time=26738.094..26738.094 rows=0 loops=1)
Recheck Cond: (application_id = 9)
Filter: ((install_time >= '2015-10-01 00:00:00+00'::timestamp with time zone) AND (install_time <= '2015-10-14 23:59:59.999999+00'::timestamp with time zone) AND (event_type = 'install'::text))
Rows Removed by Filter: 99474
-> Bitmap Index Scan on application_events_y2015m09_application_id_idx (cost=0.00..1702.64 rows=101095 width=0) (actual time=480.529..480.529 rows=99474 loops=1)
Index Cond: (application_id = 9)
Total runtime: 91076.032 ms
Full structure
/*
Target Server Type : PGSQL
Target Server Version : 90309
File Encoding : 65001
Date: 2015-10-14 19:09:50
*/
CREATE TABLE "public"."application_events" (
"id" int4 DEFAULT nextval('application_events_id_seq'::regclass) NOT NULL,
"application_id" int4 NOT NULL,
"device_id" int4 NOT NULL,
"install_time" timestamptz(6) NOT NULL,
"event_date" date NOT NULL,
"event_time" timestamptz(6) NOT NULL,
"event_name" text COLLATE "default" NOT NULL,
"event_value" text COLLATE "default" NOT NULL,
"event_type" text COLLATE "default" NOT NULL,
) ;
-- ----------------------------
-- Alter Sequences Owned By
-- ----------------------------
-- ----------------------------
-- Indexes structure for table application_events
-- ----------------------------
CREATE INDEX "application_events_application_id" ON "public"."application_events" USING btree (application_id);
CREATE INDEX "application_events_device_id" ON "public"."application_events" USING btree (device_id);
CREATE INDEX "application_events_event_date" ON "public"."application_events" USING btree (event_date);
CREATE INDEX "application_events_event_name" ON "public"."application_events" USING btree (event_name);
CREATE INDEX "application_events_event_name_like" ON "public"."application_events" USING btree (event_name "pg_catalog"."text_pattern_ops");
-- ----------------------------
-- Triggers structure for table application_events
-- ----------------------------
CREATE TRIGGER "after_insert_application_events_trigger" AFTER INSERT ON "public"."application_events"
FOR EACH ROW
EXECUTE PROCEDURE "application_events_delete_master"();
CREATE TRIGGER "before_insert_application_events_trigger" BEFORE INSERT ON "public"."application_events"
FOR EACH ROW
EXECUTE PROCEDURE "application_events_insert_child"();
CREATE TRIGGER "bucardo_delta" AFTER INSERT OR UPDATE OR DELETE ON "public"."application_events"
FOR EACH ROW
EXECUTE PROCEDURE "bucardo"."delta_public_application_events"();
CREATE TRIGGER "bucardo_kick_sync" AFTER TRUNCATE ON "public"."application_events"
FOR EACH STATEMENT
EXECUTE PROCEDURE "bucardo"."bucardo_kick_sync"();
CREATE TRIGGER "bucardo_note_trunc_sync" AFTER TRUNCATE ON "public"."application_events"
FOR EACH STATEMENT
EXECUTE PROCEDURE "bucardo"."bucardo_note_truncation"('sync');
-- ----------------------------
-- Primary Key structure for table application_events
-- ----------------------------
ALTER TABLE "public"."application_events" ADD PRIMARY KEY ("id");
-- ----------------------------
-- Foreign Key structure for table "public"."application_events"
-- ----------------------------
ALTER TABLE "public"."application_events" ADD FOREIGN KEY ("device_id") REFERENCES "public"."application_devices" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "public"."application_events" ADD FOREIGN KEY ("application_id") REFERENCES "public"."applications" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED;
Table analyze.
INFO: analyzing "public.application_events"
INFO: "application_events": scanned 4 of 4 pages, containing 0 live rows and 48 dead rows; 0 rows in sample, 0 estimated total rows
INFO: analyzing "public.application_events" inheritance tree
INFO: "application_events_y2015m09": scanned 11584 of 129704 pages, containing 368816 live rows and 419 dead rows; 11584 rows in sample, 4133206 estimated total rows
INFO: "application_events_y2015m10": scanned 18415 of 206186 pages, containing 582845 live rows and 0 dead rows; 18415 rows in sample, 6524143 estimated total rows