๐ช๐ต๐ฒ๐ป ๐๐น๐ฒ๐ฎ๐ป ๐๐ผ๐ฑ๐ฒ ๐๐ถ๐น๐น๐ ๐ฃ๐ฒ๐ฟ๐ณ๐ผ๐ฟ๐บ๐ฎ๐ป๐ฐ๐ฒ
You love ORMs. Tools like Prisma or TypeORM make your code look clean. You treat database tables like simple classes.
But there is a conflict. ORMs focus on developer experience. Databases focus on execution plans. These goals often clash.
If you do not check the SQL your code sends, you outsource performance to a tool. This often kills your app.
The N+1 problem is common. You fetch 50 posts. You then fetch the author for each post one by one. This is 51 queries.
Your local machine handles this. Your production server does not. Under load, your CPU spikes. Your API goes down.
Fix: Use relations to fetch everything in one query.
ORMs often fetch every column. Your table has huge data blobs. You only need an email. This wastes network speed. It slows your query.
Fix: Use select to pick only the columns you need.
The save method is a trap. It often runs a SELECT before an INSERT. This doubles your database load for every write.
Fix: Use insert for new records.
Stop writing generic code to switch databases. You will not switch. Use specific features like JSONB or Lateral Joins.
SQL in your code is not dirty. Ignoring your database engine is.
Senior developers do these things:
- Turn on query logging.
- Stop treating databases like object bags.
- Use raw SQL for complex logic.