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

后端 未结 8 2207
囚心锁ツ
囚心锁ツ 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:56

    It's absolutely true that "*" is "all columns". And you're right in the point of if you've a table with an incredible number of columns (say 100+), these kind of queries can be bad in terms of efficiency.

    I believe that the best solution is creating database views previously filtering the amount of records evolved in the count operation, so, the performance impact isn't a big problem, because views can be cached.

    In the other hand, it seems that "*" operator should be avoided when returning records, and it's brutally better to select the fields you really need to use in some business.

提交回复
热议问题