"Slow API? Check your database indexes"

This title was summarized by AI from the post below.

API was slow. Everyone blamed .NET.” Turns out, .NET wasn’t the villain. The database was. A single missing index was turning a 300ms query into a 4-second bottleneck. One line fixed it all: CREATE INDEX IX_UserId ON Orders(UserId); Lesson learned? Before you optimize code — optimize what your code talks to. Backend issues often live one layer below where we’re looking.

  • graphical user interface, application

This is true. But we also need to be careful while adding indexes. It certainly boosts speed but can also slow it down if applied wrongly. Important to always check the execution plans pre and post adding indexes.

Never blame anything until you have evidence. In this case, the API call was slow, doesn’t matter if it was .Net, database, networking, etc. What is generally more concerning (sans context) is how a query that WAS .3 seconds (which was deemed fast enough), got to 4 seconds. Was the index lost, was it an immediate increase in time, did the stats change, etc. I assume it is even faster now? Ideally, if you knew the time was .3 seconds before, you probably have some monitoring process that should have told you that it was taking more and more time. Or that it jumped quickly. Either way, it is as often not the database systems fault as it is. The process needs to follow the evidence and follow the stack.

I'd say 'profile your app, make sure you have good telemetry', before making statements about where to look. Too many moving parts in most applications to make blanket statements, so gain observability first.

It's always the indexes! :) Having worked in SQL -heavy positions for at least 5 years in the past, it was rare that the code was the issue. Always work your way from the bottom up when it's about performance, you'll see things! I've seen SPs doing miracles when used correctly in place of complex code without sacrificing isolation of concerns. Indexes as well.

The worst part is that you won't really notice the lack of an index (or improperly defined index) until you hit high traffic scenarios and your reads start failing because you've exhausted your connection pool.

If there's one thing I've learned about performance issues over 25 years, it is that the performance bottleneck is always somewhere else than I assume. 🤭👌 Very often go for bugs too.

The lesson is: identify your bottlenecks. It can be your code, your algorithms, your database. In most cases it is one of two things: - what do you do? Do you really need it? - how do you do it? Can it be done faster? For instance, using a database.. You can add an index, but if your queries don't use it - you will not see performance gains. Building query stats help. If a particular query takes 10 ms but is executed 1M times - it's worth looking at it: - do you need that many queries? - can they do the same job in batches, like transactions, bulk queries? - what slows them down? Look at the query plan for answers..

So true, how you diagnose and find the real issue matters even more than the fix itself. With indexes, it’s all about knowing your queries and designing around them. Adding indexes blindly can actually hurt performance, slower writes, higher storage use, and long rebuild times down the road. It’s all about balance and understanding the workload.

Great reminder about the importance of understanding the full stack! Have you found any tools that help identify such bottlenecks quickly?

See more comments

To view or add a comment, sign in

Explore content categories