Does the number of columns returned affect the speed of a query?

前端 未结 18 2074
既然无缘
既然无缘 2020-12-10 16:27

If I have two queries

SELECT Id, Forename, Surname
FROM Person
WHERE PersonName Like(‘%frank%’)

And

SELECT *
FROM Person
WH         


        
18条回答
  •  抹茶落季
    2020-12-10 16:47

    I'm going to go against the flow here and say you should go with the select *. I think that premature optimization is the root of a lot of problems, and you may well find that it doesn't affect your performance when you get to real utilization. Of course, by the book it is slower, it must be, but that doesn't mean the difference is important in practice.

    Something to be aware of, though, is that some SQL engines (MS-SQL for sure) will cache the select *, so if you are using a prepared statement, or a view or stored procedure that has it, and change the table schema, it won't pick up on the change unless the view or sp is recompiled, so that is a good reason to avoid doing it if you aren't running these queries dynamically.

    And of course, this varies by database engine, so a little load testing would be in order to make sure the hit isn't obviously large.

提交回复
热议问题