I'm working on optimizing a Postgres table that stores information from a log file.
Here is the query:
SELECT c_ip as ip
, x_ctx as file_name
, date_time
, live
, c_user_agent as user_agent
FROM events
WHERE x_event = 'play'
AND date = '2012-12-01'
AND username = 'testing'
There are b-tree indexes on x_event, date, and username. In this table, there are around 25 million rows. Right now the query takes about 20-25 (correction, more like 40) seconds, and returns 143,000 rows.
Is that time expected? I would have thought it would be faster because of the indexes. Perhaps because of the sheer amount of data it has to go thru?
EDIT: Here is the EXPLAIN ANALYZE:
Bitmap Heap Scan on events (cost=251347.32..373829.74 rows=35190 width=56) (actual time=5768.409..6124.313 rows=143061 loops=1)
Recheck Cond: ((date = '2012-12-01'::date) AND (username = 'testing'::text) AND (x_event = 'play'::text))
-> BitmapAnd (cost=251347.32..251347.32 rows=35190 width=0) (actual time=5762.083..5762.083 rows=0 loops=1)
-> Bitmap Index Scan on index_events_fresh_date (cost=0.00..10247.04 rows=554137 width=0) (actual time=57.568..57.568 rows=572221 loops=1)
Index Cond: (date = '2012-12-01'::date)
-> Bitmap Index Scan on index_events_fresh_username (cost=0.00..116960.55 rows=6328206 width=0) (actual time=3184.053..3184.053 rows=6245831 loops=1)
Index Cond: (username = 'testing'::text)
-> Bitmap Index Scan on index_events_fresh_x_event (cost=0.00..124112.84 rows=6328206 width=0) (actual time=2478.919..2478.919 rows=6245841 loops=1)
Index Cond: (x_event = 'play'::text)
Total runtime: 6148.313 ms
I have several questions about that:
- Am I correct that there are 554137 rows in the date index? There are less than 50 date's that should be in there.
- How do I know what index it is using of the three listed?
- The total runtime listed was around 6 seconds, but when I run the query w/o EXPLAIN ANALYZE, it takes around 40 seconds.