I have a table called data_table. Below are schema, indexes, and their sizes.
Table "public.data_table"
Column | Type | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('data_table_id_seq'::regclass)
col1_id | integer | | not null |
col2_id | integer | | not null |
col3_id | integer | | not null |
col4 | double precision | | not null |
timestamp | timestamp with time zone | | not null |
Indexes: Size
"data_table_1col_idx" btree (col1_id) 8032 KB
"data_table_2col_idx" btree (col2_id) 8040 KB
"data_table_3col_idx" btree (col3_id) 8048 KB
"data_table_4col_idx" btree (col4) 25 MB
"data_table_epe_idx" btree (col1_id, col2_id, col3_id) 8216 KB
"data_table_pkey" btree (id) 25 MB
data_table has a total of 1184330 rows.
select count(*) from "data_table";
count
---------
1184330
(1 row)
select * from "data_table" limit 5;
id | col1_id | col2_id | col3_id | col4 | timestamp
-----------+---------+--------+----------+-----------+----------------------------
180102529 | 1 | 1 | 1 | 83.70361 | 2023-12-13 09:30:49.257+00
180102530 | 1 | 1 | 2 | 2827.6 | 2023-12-13 09:30:49.257+00
180102531 | 1 | 1 | 3 | 124.25156 | 2023-12-13 09:30:49.257+00
180102532 | 1 | 1 | 4 | 43.3 | 2023-12-13 09:30:49.257+00
180102533 | 1 | 1 | 5 | 282.4 | 2023-12-13 09:30:49.257+00
(5 rows)
col1_id is the primary key of table_1 and table_1 has the total number of rows is 11.
col2_id is the primary key of table_2 and table_2 has the total number of rows is 19.
col3_id is the primary key of table_3 and table_3 has the total number of rows is 115.
As you can see data_table_epe_idx is my composite index and has a size of around 8MB whereas data_table_pkey is the default index and has a size of 25MB.
Why is the size of the composite index (3x integer) less than the size of the PK index (1x integer)?