I have videos table with 18M rows. When I search for particular video by ID, it takes up to 6 seconds to complete. Sometimes it takes few milliseconds, sometimes up to 6 seconds, but on average it is around 2 seconds.
Application is hosted on heroku and I'm using Crane database (https://addons.heroku.com/heroku-postgresql) with 410MB of RAM.
Is there any way to speed this up? I'm querying for videos 50 times per second on average, and new videos are inserted/updated at rate of 50 per second.
explain analyze SELECT * FROM videos WHERE id = 17841464 LIMIT 1;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..6.43 rows=1 width=119) (actual time=2337.892..2337.894 rows=1 loops=1)
-> Index Scan using videos_pkey on videos (cost=0.00..6.43 rows=1 width=119) (actual time=2337.888..2337.888 rows=1 loops=1)
Index Cond: (id = 17841464)
Total runtime: 2337.943 ms
Here is how the table looks like:
\d+ videos;
Table "public.videos"
Column | Type | Modifiers | Storage | Stats target | Description
----------------+-----------------------------+-----------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('videos_id_seq'::regclass) | plain | |
uuid | character(11) | not null | extended | |
channel_id | integer | not null | plain | |
category_id | integer | | plain | |
title | character varying(255) | | extended | |
published_at | timestamp without time zone | | plain | |
view_count | bigint | | plain | |
like_count | integer | | plain | |
dislike_count | integer | | plain | |
favorite_count | integer | | plain | |
comment_count | integer | | plain | |
disabled | boolean | default false | plain | |
created_at | timestamp without time zone | | plain | |
updated_at | timestamp without time zone | | plain | |
Indexes:
"videos_pkey" PRIMARY KEY, btree (id)
"videos_uuid_idx" UNIQUE, btree (uuid)
"videos_latest_by_channel_idx" btree (channel_id, published_at DESC)
"videos_top_by_channel_idx" btree (channel_id, view_count DESC)
Has OIDs: no