Author Topic: SELECT COUNT(*) GOOD IDEA?  (Read 418 times)

0 Members and 1 Guest are viewing this topic.

Offline AzazTopic starter

  • Irregular
  • Posts: 28
    • View Profile
SELECT COUNT(*) GOOD IDEA?
« on: July 30, 2010, 06:27:35 AM »
Code: [Select]
SELECT count(*) as total FROM forum_replies
My forum_replies has over 30,000 rows... how would mysql handle this on a forum on the main index, is there a better/simplified way ?

I just need to grab posts not *... ?

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: SELECT COUNT(*) GOOD IDEA?
« Reply #1 on: July 30, 2010, 06:43:30 AM »
Is it MyISAM or InnoDB table? For MyISAM this is extremely fast, because it stores the rowcount alongside the table.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline AzazTopic starter

  • Irregular
  • Posts: 28
    • View Profile
Re: SELECT COUNT(*) GOOD IDEA?
« Reply #2 on: July 30, 2010, 06:48:20 AM »
MYIASM, but i heard you can use

Code: [Select]
COUNT("pid") or just count 1 row instead of using  * still possible faster ? thanks[/code]
« Last Edit: July 30, 2010, 06:51:00 AM by Azaz »

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: SELECT COUNT(*) GOOD IDEA?
« Reply #3 on: July 30, 2010, 06:52:59 AM »
Not really. MySQL is coded to recognise SELECT COUNT(*) FROM tableName query and return stored rowcount. I'm not sure if this optimisation holds if you substitute * with actual field. Even if it does, there'd be no difference.

Also notice that this optimisation will only be in effect, when there's no WHERE clause in the query (i.e. you want to count all rows in the table)
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline AzazTopic starter

  • Irregular
  • Posts: 28
    • View Profile
Re: SELECT COUNT(*) GOOD IDEA?
« Reply #4 on: July 30, 2010, 06:53:54 AM »
Not really. MySQL is coded to recognise SELECT COUNT(*) FROM tableName query and return stored rowcount. I'm not sure if this optimisation holds if you substitute * with actual field. Even if it does, there'd be no difference.

Also notice that this optimisation will only be in effect, when there's no WHERE clause in the query (i.e. you want to count all rows in the table)

Sweet, Solved.

Gotta love people like you here, I hope to hear from you in the future,