Should I COUNT(*) or not?

前端 未结 14 1330
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 15:49

I know it\'s generally a bad idea to do queries like this:

SELECT * FROM `group_relations`

But when I just want the count, should I go for this

相关标签:
14条回答
  • 2021-01-30 16:37

    COUNT(*) facts and myths:

    MYTH: "InnoDB doesn't handle count(*) queries well":

    Most count(*) queries are executed same way by all storage engines if you have a WHERE clause, otherwise you InnoDB will have to perform a full table scan.

    FACT: InnoDB doesn't optimize count(*) queries without the where clause

    0 讨论(0)
  • 2021-01-30 16:37

    It is best to count by an indexed column such as a primary key.

    SELECT COUNT(`group_id`) FROM `group_relations`
    
    0 讨论(0)
提交回复
热议问题