How to Query Data the Professional Way in SQL Server: A Performance-First Guide for Beginners
If you've just started writing SQL Server queries, there's a good chance you probably wrote a query, saw it return the right rows, and moved on. That's normal and it's also exactly why so many beginner-written queries fall over the moment a table grows from 500 rows to 5 million.
This article walks through the habits, patterns, and models that separate a "SELECT * and hope" query from one a senior engineer would actually approve in a code review.
1. Stop Using SELECT *
It's the first habit almost everyone picks up, and the first one you should drop.
-- AvoidSELECT * FROM Orders WHERE CustomerId = 100;-- PreferSELECT OrderId, OrderDate, TotalAmountFROM OrdersWHERE CustomerId = 100;
Why it matters:
- SQL Server has to fetch every column, including large or unused ones (think NVARCHAR(MAX) or VARBINARY blobs), inflating I/O.
- It breaks covering indexes (more on that below): the optimizer can't satisfy...
Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE