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

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

    COUNT(*) is different from COUNT(column1) !
    COUNT(*) returns the number of records, and does NOT use more resources, while COUNT(column1) counts the number of records where column1 is non null.

    For SELECT, it is different. SELECT * will of course request more data.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题