TL;DR: Solution
The problem was that one instance hat magnetic storage attached and was therefore significantly slower.
Original Question
I have two identical AWS RDS PostgreSQL 10.6 databases running on separate t2.medium
instances in different VPC subnets, but within the same availability zone. They represent our staging
/production
environment, respectively. The only difference between both instances I can see is that the production
instance is multi-AZ, while the staging
instance is not.
When I run the simple query
select * from auva_orders
on a table with ~18 mio. rows each, I see dramatic runtime differences:
- staging: 1:30m - 1:50m
- production: ~0:30m
Both tables are continuously populated with new entries by a Kafka consumer, contain nearly the same number of rows and have identical indices:
Indexes:
"auva_order_pk" PRIMARY KEY, btree (topic_partition, topic_offset)
"auvaOrderCompanyCountryOrderdate" btree (company_code, country_code, orderdate)
If I EXPLAIN (ANALYSE, BUFFERS)
the queries for each environment, I get the following results:
staging
:
EXPLAIN (ANALYZE, BUFFERS) select count(*) from auva_order;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=421029.42..421029.43 rows=1 width=8) (actual time=97541.306..97541.306 rows=1 loops=1)
Buffers: shared hit=52389 read=273887 dirtied=129
-> Gather (cost=421029.21..421029.42 rows=2 width=8) (actual time=97540.845..97542.098 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=52389 read=273887 dirtied=129
-> Partial Aggregate (cost=420029.21..420029.22 rows=1 width=8) (actual time=97538.854..97538.855 rows=1 loops=3)
Buffers: shared hit=52389 read=273887 dirtied=129
-> Parallel Seq Scan on auva_order (cost=0.00..401278.57 rows=7500257 width=0) (actual time=4.692..95822.212.
. rows=6000544 loops=3)
Buffers: shared hit=52389 read=273887 dirtied=129
Planning time: 0.069 ms
Execution time: 97542.148 ms
(12 rows)
production
:
EXPLAIN (ANALYZE, BUFFERS) select count(*) from auva_order;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=417818.52..417818.53 rows=1 width=8) (actual time=30899.683..30899.684 rows=1 loops=1)
Buffers: shared hit=60023 read=263042 dirtied=1
-> Gather (cost=417818.30..417818.51 rows=2 width=8) (actual time=30898.544..30900.360 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=60023 read=263042 dirtied=1
-> Partial Aggregate (cost=416818.30..416818.31 rows=1 width=8) (actual time=30897.146..30897.147 rows=1 loops=3)
Buffers: shared hit=60023 read=263042 dirtied=1
-> Parallel Seq Scan on auva_order (cost=0.00..398067.64 rows=7500264 width=0) (actual time=0.312..29168.159.
. rows=6000545 loops=3)
Buffers: shared hit=60023 read=263042 dirtied=1
Planning time: 0.070 ms
Execution time: 30900.420 ms
(12 rows)
I have no idea what causes the differences in runtime. Our staging
instance might be under heavier load during the current development phase, but I don't think this explains a factor of 3-4 in runtime. The CPU credits of the burstable AWS EC2 instances are "full", so there is no shortage of CPU power in any of the instances. Any ideas what might be the cause?