Is using COUNT(*) or SELECT * a good idea?

后端 未结 8 2209
囚心锁ツ
囚心锁ツ 2020-12-10 17:03

I\'ve heard several times that you shouldn\'t perform COUNT(*) or SELECT * for performance reasons, but wasn\'t able to dig up some further informa

8条回答
  •  不知归路
    2020-12-10 17:38

    When using count(*) the * doesn't mean "all fields". Using count(field) will count all non-null values in the field, but count(*) will always count all records even if all fields in all records are null, so it doesn't need to check the data in the fields at all.

    Using select * means that you almost always return more data than you are going to use, which of course is a waste. However, perhaps more serious is the maintainence problem; if you add fields to a table your query will return these too. That might mean that the record becomes too large to fit in the buffer, resulting in an error message.

提交回复
热议问题