Optimize API Performance with Better Data Shaping

Recently I came across a discussion on query performance that made me rethink a habit most of us have when writing APIs. You build an endpoint in ASP.NET Core, hook it to your database, and everything works fine. Clean code, async calls, repository pattern… all good... until one day the endpoint slows down. Not because of traffic. Not because of infrastructure. But because of data shape. Picture this: You have an endpoint that returns a list of orders with customer info and items. So you write a query using your ORM (like Entity Framework Core):  • Include Orders  • Include Customer  • Include Items Looks fine, right? But under the hood, this often becomes a massive join that multiplies rows: 𝟭 𝗼𝗿𝗱𝗲𝗿 × 𝟭 𝗰𝘂𝘀𝘁𝗼𝗺𝗲𝗿 × 𝗡 𝗶𝘁𝗲𝗺𝘀 = 𝗱𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝗱 𝗱𝗮𝘁𝗮 𝗼𝘃𝗲𝗿 𝘁𝗵𝗲 𝘄𝗶𝗿𝗲 I was reading a post from SQLAuthority that reminded of a key principle: The problem is not always the query, it’s what you ask the query to return. Instead of loading everything in one shot, a better approach in many cases is:  • Project only what you need (SELECT specific columns)  • Split queries when relationships explode  • Avoid blindly using .Include() for complex graphs For example:  • First query: Orders (lightweight)  • Second query: Items grouped by OrderId  • Merge in memory Yes, it’s two queries, but often faster, smaller, and more predictable. This becomes even more important when using databases like PostgreSQL or SQL Server in high-scale systems, where:  • Network payload matters  • Execution plans matter  • Memory pressure matters What I like about this is how it challenges a common assumption: “𝘍𝘦𝘸𝘦𝘳 𝘲𝘶𝘦𝘳𝘪𝘦𝘴 = 𝘣𝘦𝘵𝘵𝘦𝘳 𝘱𝘦𝘳𝘧𝘰𝘳𝘮𝘢𝘯𝘤𝘦” In reality, better-shaped data beats fewer queries almost every time. If you’re building APIs today, especially in microservices, it’s worth asking: 𝘈𝘳𝘦 𝘺𝘰𝘶 𝘰𝘱𝘵𝘪𝘮𝘪𝘻𝘪𝘯𝘨 𝘲𝘶𝘦𝘳𝘺 𝘤𝘰𝘶𝘯𝘵... 𝘰𝘳 𝘥𝘢𝘵𝘢 𝘧𝘭𝘰𝘸? #DotNet #EntityFramework #SQLServer #PostgreSQL #Performance #BackendDevelopment #Microservices #API #CleanArchitecture #SoftwareEngineering #Cloud

To view or add a comment, sign in

Explore content categories