๐—œ ๐—”๐—ฑ๐—ฑ๐—ฒ๐—ฑ ๐Ÿฎ๐Ÿฌ ๐—œ๐—ป๐—ฑ๐—ฒ๐˜…๐—ฒ๐˜€ ๐—”๐—ป๐—ฑ ๐— ๐˜† ๐——๐—• ๐—š๐—ผ๐˜ ๐Ÿฏ๐˜… ๐—ฆ๐—น๐—ผ๐˜„๐—ฒ๐—ฟ

I started working with a PostgreSQL database. API response times were 8 seconds. Users timed out.

I added 20 indexes to fix it. I thought more indexes meant more speed. I was wrong.

The database slowed down. Writes crawled. Disk space filled up.

Here is why it happened.

PostgreSQL updates every index during every write. Every INSERT or UPDATE takes longer. Too many indexes kill write speed.

Some indexes are dead weight. I indexed a status column with 4 values. The database ignored the index. It used a full table scan instead.

I also had redundant indexes. I had one for email and one for email and name. The second one already covered the first.

Here is how I fixed it.

I switched to partial indexes for specific statuses. These are smaller and faster.

The results.

My rules for indexes.

Indexes are surgical tools. Use them with care.

Source: https://dev.to/kollittle/i-added-20-indexes-to-fix-slow-queries-my-database-got-3x-slower-3ac2 Optional learning community: https://t.me/GyaanSetuAi