I have a huge table that has fields ip, mac, and timestamp.
Neither of the three fields is unique, but the combination of all three is.
Table is automatically populated, with newer records added all the time. The field timestamp refers to when a row was added. Records are never UPDATEd.
Here's the table description:
Column | Type | Nullable | Default
-----------+-----------------------------+----------+--------
event | text | not null |
ip | inet | not null |
mac | macaddr8 | not null |
timestamp | timestamp without time zone | not null | now()
Indexes:
"ip_idx" btree (ip)
"mac_idx" btree (mac)
"time_idx" btree ("timestamp")
"timestamp_ip_event_key" UNIQUE CONSTRAINT, btree ("timestamp", ip, event)
I have this very slow query, causing the website to take very long time to load
How can I speed it up?
Is it possible to take advantage of the fact that the table is basically ordered by timestamp?
I do not have access to the script that adds records.
Executed SQL
select ip,
max(timestamp)
from my_table
WHERE ip << inet '10.38.69.0/24'
group by ip
order by ip;
Time
2151.577594000173 ms
Database
default
QUERY PLAN
Finalize GroupAggregate (cost=291919.94..292190.52 rows=1068 width=15) (actual time=696.246..704.698 rows=151 loops=1)
Group Key: ip
-> Gather Merge (cost=291919.94..292169.16 rows=2136 width=15) (actual time=696.220..704.558 rows=429 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Sort (cost=290919.92..290922.59 rows=1068 width=15) (actual time=679.313..679.325 rows=143 loops=3)
Sort Key: ip
Sort Method: quicksort Memory: 31kB
Worker 0: Sort Method: quicksort Memory: 31kB
Worker 1: Sort Method: quicksort Memory: 31kB
-> Partial HashAggregate (cost=290855.52..290866.20 rows=1068 width=15) (actual time=679.192..679.233 rows=143 loops=3)
Group Key: ip
Batches: 1 Memory Usage: 81kB
Worker 0: Batches: 1 Memory Usage: 81kB
Worker 1: Batches: 1 Memory Usage: 81kB
-> Parallel Bitmap Heap Scan on my_table (cost=12023.68..289019.89 rows=367126 width=15) (actual time=67.898..580.432 rows=312819 loops=3)
Filter: (ip << '10.38.69.0/24'::inet)
Rows Removed by Filter: 1716854
Heap Blocks: exact=15528 lossy=18121
-> Bitmap Index Scan on my_table_ip_idx (cost=0.00..11803.41 rows=881097 width=0) (actual time=62.721..62.721 rows=938457 loops=1)
Index Cond: ((ip > '10.38.69.0/24'::inet) AND (ip <= '10.38.69.255'::inet))
Planning Time: 1.049 ms
JIT:
Functions: 30
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 2.180 ms, Inlining 0.000 ms, Optimization 1.470 ms, Emission 29.303 ms, Total 32.952 ms
Execution Time: 726.126 ms