Search code examples
sqlsql-serverormdatabase-performancesqlperformance

are performance/code-maintainability concerns surrounding SELECT * on MS SQL still relevant today, with modern ORMs?


summary: I've seen a lot of advice against using SELECT * in MS SQL, due to both performance and maintainability concerns. however, many of these posts are very old - 5 to 10 years! it seems, from many of these posts, that the performance concerns may have actually been quite small, even in their time, and as to the maintainability concerns ("oh no, what if someone changes the columns, and you were getting data by indexing an array! your SELECT * would get you in trouble!"), modern coding practices and ORMs (such as Dapper) seem - at least in my experience - to eliminate such concerns.

and so: are there concerns with SELECT * that are still relevant today?


greater context: I've started working at a place with a lot of old MS code (ASP scripts, and the like), and I've been helping to modernize a lot of it, however: most of my SQL experience is actually from MySQL and PHP frameworks and ORMs - this is my first time working with MS SQL - and I know there are subtle differences between the two. ALSO: my co-workers are a little older than I am, and have some concerns that - to me - seem "older". ("nullable fields are slow! avoid them!") but again: in this particular field, they definitely have more experience than I do.

for this reason, I'd also like to ask: whether SELECT * with modern ORMs is or isn't safe and sane to do today, are there recent online resources which indicate such?

thanks! :)


Solution

  • This question is out some time now, and noone seems to be able to find, what Ben is looking for...

    I think this is, because the answer is "it depends".

    There just NOT IS THE ONE answer to this.

    Examples

    • As i pointed out before, if a database is not yours, and it may be altered often, you cannot guarantee performance, because with select * the amount of data per row may explode
    • If you write an application using ITS OWN database, noone alters your DB (hopefully) and you need your columns, so whats wrong with select *
    • If you build some kind of lazy loading with "main properties" beeing loaded instantly and others beeing loaded later (of same entity), you cannot go with select * because you get all
    • If you use select * other developers will every time think about "did he think about select *" as they will try to optimize. So you should add enough comments...
    • If you build 3-Tier-Application building large caches in the middle-Tier and performance is a theme beeing done by cache, you may use select *
    • Expanding 3Tier: If you have many many concurrent users and/or really big data, you should consider every single byte, because you have to scale up your middle-Tier with every byte beeing wasted (as someone pointed out in the comments before)
    • If you build a small app for 3 users and some thousands of records, the budget may not give time to optimize speed/db-layout/something
    • Speak to your dba... HE will advice you WHICH statement has to be changed/optimized/stripped down/...

    I could go on. There just is not ONE answer. It just depends on to many factors.