<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: COUNT(*) for Innodb Tables</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/</link>
	<description>Percona&#039;s MySQL &#38; InnoDB performance and scalability blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 16:45:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Josh Q</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-860801</link>
		<dc:creator>Josh Q</dc:creator>
		<pubDate>Tue, 20 Dec 2011 19:17:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-860801</guid>
		<description>I&#039;ve tried the following metrics on one of our production MySQL servers and found some interesting results related to InnoDB count().  

We&#039;re using XtraDB(InnoDB by Percona) tables (version 5.5.14-rel20.5-log), this is a web-mail system that stores all messages in a table, so it is fairly good sized.  

I did a simple count(*) on the messages table:

mysql&gt; select count(*) from messages;
+----------+
&#124; count(*) &#124;
+----------+
&#124;  2849204 &#124;
+----------+
1 row in set (5.82 sec)

I did this a dozen times and it came back at around 6 seconds each time.

Then I counted the primary key:
mysql&gt; select count(id) from messages;
+-----------+
&#124; count(id) &#124;
+-----------+
&#124;   2849205 &#124;
+-----------+
1 row in set (6.05 sec)

Basically the same results, even after a dozen iterations.  (this is a production system so the count will increase).  

Then I counted a column that is a key from another table, not null :

mysql&gt; desc messages;
+---------------+--------------+------+-----+---------+----------------+
&#124; Field             &#124; Type         &#124; Null &#124; Key &#124; Default &#124; Extra          &#124;
+---------------+--------------+------+-----+---------+----------------+
&#124; id               &#124; bigint(20)   &#124; NO   &#124; PRI &#124; NULL    &#124; auto_increment &#124;
&#124; id_acct       &#124; int(11)      &#124; NO   &#124; MUL &#124; 0       &#124;                &#124;


mysql&gt; select count(id_acct) from awm_messages;
+----------------+
&#124; count(id_acct) &#124;
+----------------+
&#124;        2849213 &#124;
+----------------+
1 row in set (3.40 sec)


Each time, right around 3.5 seconds.  I&#039;m not sure if this is just more efficient for InnoDB/XtraDB, or if it&#039;s due to the difference in field type (bigint vs int).</description>
		<content:encoded><![CDATA[<p>I&#8217;ve tried the following metrics on one of our production MySQL servers and found some interesting results related to InnoDB count().  </p>
<p>We&#8217;re using XtraDB(InnoDB by Percona) tables (version 5.5.14-rel20.5-log), this is a web-mail system that stores all messages in a table, so it is fairly good sized.  </p>
<p>I did a simple count(*) on the messages table:</p>
<p>mysql&gt; select count(*) from messages;<br />
+&#8212;&#8212;&#8212;-+<br />
| count(*) |<br />
+&#8212;&#8212;&#8212;-+<br />
|  2849204 |<br />
+&#8212;&#8212;&#8212;-+<br />
1 row in set (5.82 sec)</p>
<p>I did this a dozen times and it came back at around 6 seconds each time.</p>
<p>Then I counted the primary key:<br />
mysql&gt; select count(id) from messages;<br />
+&#8212;&#8212;&#8212;&#8211;+<br />
| count(id) |<br />
+&#8212;&#8212;&#8212;&#8211;+<br />
|   2849205 |<br />
+&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (6.05 sec)</p>
<p>Basically the same results, even after a dozen iterations.  (this is a production system so the count will increase).  </p>
<p>Then I counted a column that is a key from another table, not null :</p>
<p>mysql&gt; desc messages;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| Field             | Type         | Null | Key | Default | Extra          |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| id               | bigint(20)   | NO   | PRI | NULL    | auto_increment |<br />
| id_acct       | int(11)      | NO   | MUL | 0       |                |</p>
<p>mysql&gt; select count(id_acct) from awm_messages;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| count(id_acct) |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
|        2849213 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
1 row in set (3.40 sec)</p>
<p>Each time, right around 3.5 seconds.  I&#8217;m not sure if this is just more efficient for InnoDB/XtraDB, or if it&#8217;s due to the difference in field type (bigint vs int).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-756082</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Thu, 29 Apr 2010 23:44:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-756082</guid>
		<description>@John - pretty clever.  I haven&#039;t done any testing to see how close this number stays, or how slow it is to update, but it seems to be a great solution if you just need an approximation.  Thanks!</description>
		<content:encoded><![CDATA[<p>@John &#8211; pretty clever.  I haven&#8217;t done any testing to see how close this number stays, or how slow it is to update, but it seems to be a great solution if you just need an approximation.  Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SELECT COUNT(*) sehr langsam - php.de</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-649812</link>
		<dc:creator>SELECT COUNT(*) sehr langsam - php.de</dc:creator>
		<pubDate>Tue, 08 Sep 2009 15:47:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-649812</guid>
		<description>[...] ZÃ¤hlen fÃ¼r MySQL kein Problem ist. Selbst bei 8 Mio. Zeilen.    Ã¼bliches work-around sind trigger bei INSERT/DELETE mit einer zusÃ¤tzlichen tabelle.    __________________ AC/DC Fanpage ifyouwantblood.de (Stewie [...]</description>
		<content:encoded><![CDATA[<p>[...] ZÃ¤hlen fÃ¼r MySQL kein Problem ist. Selbst bei 8 Mio. Zeilen.    Ã¼bliches work-around sind trigger bei INSERT/DELETE mit einer zusÃ¤tzlichen tabelle.    __________________ AC/DC Fanpage ifyouwantblood.de (Stewie [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-639898</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Wed, 26 Aug 2009 16:16:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-639898</guid>
		<description>SC,

Note these stats are approximate.</description>
		<content:encoded><![CDATA[<p>SC,</p>
<p>Note these stats are approximate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SC</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-639851</link>
		<dc:creator>SC</dc:creator>
		<pubDate>Wed, 26 Aug 2009 15:14:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-639851</guid>
		<description>If your table has a primary key, you can also get the count from 
show index from , the cardinality of the key will give you count of the rows in the table.</description>
		<content:encoded><![CDATA[<p>If your table has a primary key, you can also get the count from<br />
show index from , the cardinality of the key will give you count of the rows in the table.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cloudspace - A Cool Little Company's Thoughts, News, Happenings</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-625217</link>
		<dc:creator>Cloudspace - A Cool Little Company's Thoughts, News, Happenings</dc:creator>
		<pubDate>Thu, 06 Aug 2009 14:57:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-625217</guid>
		<description>[...] never contain a NULL value, and tell MySQL to &#8216;use index (index_name)&#8217;. Also, thanks to Wallace, who commented on the MySQL Performance Blog      Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] never contain a NULL value, and tell MySQL to &#8216;use index (index_name)&#8217;. Also, thanks to Wallace, who commented on the MySQL Performance Blog      Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wallace</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-529049</link>
		<dc:creator>Wallace</dc:creator>
		<pubDate>Fri, 03 Apr 2009 11:40:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-529049</guid>
		<description>Why the performance gap?

Running this slow query took 138 seconds (2 min 18.09 sec)
SELECT COUNT(*) FROM dbmail_messageblks;
+----------+
&#124; COUNT(*) &#124;
+----------+
&#124;   262788 &#124;
+----------+
1 row in set (2 min 18.09 sec)

After optimizing the SQL, it took 0.27 seconds.
SELECT COUNT(*) FROM dbmail_messageblks use index(physmessage_id_index);
+----------+
&#124; COUNT(*) &#124;
+----------+
&#124;   262796 &#124;
+----------+
1 row in set (0.27 sec)

&gt; SHOW CREATE TABLE dbmail_messageblks\G
*************************** 1. row ***************************
        Table: dbmail_messageblks
Create Table: CREATE TABLE `dbmail_messageblks` (
   `messageblk_idnr` bigint(21) NOT NULL auto_increment,
   `physmessage_id` bigint(21) NOT NULL default &#039;0&#039;,
   `messageblk` longblob NOT NULL,
   `blocksize` bigint(21) NOT NULL default &#039;0&#039;,
   `is_header` tinyint(1) NOT NULL default &#039;0&#039;,
   PRIMARY KEY  (`messageblk_idnr`),
   KEY `physmessage_id_index` (`physmessage_id`),
   KEY `physmessage_id_is_header_index` (`physmessage_id`,`is_header`),
   CONSTRAINT `dbmail_messageblks_ibfk_1` FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=602519 DEFAULT CHARSET=utf8

&gt; EXPLAIN SELECT COUNT(*) FROM dbmail_messageblks\G
*************************** 1. row ***************************
            id: 1
   select_type: SIMPLE
         table: dbmail_messageblks
          type: index
possible_keys: NULL
           key: PRIMARY
       key_len: 8
           ref: NULL
          rows: 1930308
         Extra: Using index

&gt; EXPLAIN SELECT COUNT(*) FROM dbmail_messageblks use index(physmessage_id_index)\G
*************************** 1. row ***************************
            id: 1
   select_type: SIMPLE
         table: dbmail_messageblks
          type: index
possible_keys: NULL
           key: physmessage_id_index
       key_len: 8
           ref: NULL
          rows: 1930310
         Extra: Using index</description>
		<content:encoded><![CDATA[<p>Why the performance gap?</p>
<p>Running this slow query took 138 seconds (2 min 18.09 sec)<br />
SELECT COUNT(*) FROM dbmail_messageblks;<br />
+&#8212;&#8212;&#8212;-+<br />
| COUNT(*) |<br />
+&#8212;&#8212;&#8212;-+<br />
|   262788 |<br />
+&#8212;&#8212;&#8212;-+<br />
1 row in set (2 min 18.09 sec)</p>
<p>After optimizing the SQL, it took 0.27 seconds.<br />
SELECT COUNT(*) FROM dbmail_messageblks use index(physmessage_id_index);<br />
+&#8212;&#8212;&#8212;-+<br />
| COUNT(*) |<br />
+&#8212;&#8212;&#8212;-+<br />
|   262796 |<br />
+&#8212;&#8212;&#8212;-+<br />
1 row in set (0.27 sec)</p>
<p>&gt; SHOW CREATE TABLE dbmail_messageblks\G<br />
*************************** 1. row ***************************<br />
        Table: dbmail_messageblks<br />
Create Table: CREATE TABLE `dbmail_messageblks` (<br />
   `messageblk_idnr` bigint(21) NOT NULL auto_increment,<br />
   `physmessage_id` bigint(21) NOT NULL default &#8217;0&#8242;,<br />
   `messageblk` longblob NOT NULL,<br />
   `blocksize` bigint(21) NOT NULL default &#8217;0&#8242;,<br />
   `is_header` tinyint(1) NOT NULL default &#8217;0&#8242;,<br />
   PRIMARY KEY  (`messageblk_idnr`),<br />
   KEY `physmessage_id_index` (`physmessage_id`),<br />
   KEY `physmessage_id_is_header_index` (`physmessage_id`,`is_header`),<br />
   CONSTRAINT `dbmail_messageblks_ibfk_1` FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE<br />
) ENGINE=InnoDB AUTO_INCREMENT=602519 DEFAULT CHARSET=utf8</p>
<p>&gt; EXPLAIN SELECT COUNT(*) FROM dbmail_messageblks\G<br />
*************************** 1. row ***************************<br />
            id: 1<br />
   select_type: SIMPLE<br />
         table: dbmail_messageblks<br />
          type: index<br />
possible_keys: NULL<br />
           key: PRIMARY<br />
       key_len: 8<br />
           ref: NULL<br />
          rows: 1930308<br />
         Extra: Using index</p>
<p>&gt; EXPLAIN SELECT COUNT(*) FROM dbmail_messageblks use index(physmessage_id_index)\G<br />
*************************** 1. row ***************************<br />
            id: 1<br />
   select_type: SIMPLE<br />
         table: dbmail_messageblks<br />
          type: index<br />
possible_keys: NULL<br />
           key: physmessage_id_index<br />
       key_len: 8<br />
           ref: NULL<br />
          rows: 1930310<br />
         Extra: Using index</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nop</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-479705</link>
		<dc:creator>nop</dc:creator>
		<pubDate>Tue, 17 Feb 2009 01:28:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-479705</guid>
		<description>correction: replace &quot;8 seconds&quot; with &quot;5 minutes, 8 seconds&quot;.</description>
		<content:encoded><![CDATA[<p>correction: replace &#8220;8 seconds&#8221; with &#8220;5 minutes, 8 seconds&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nop</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-479697</link>
		<dc:creator>nop</dc:creator>
		<pubDate>Tue, 17 Feb 2009 01:18:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-479697</guid>
		<description>ready for a headache? try &quot;select count($field) from $table;&quot; and compare the time it takes to &quot;select count($field) from $table use index($field);&quot; 
($field is the primary key (int autoincrement) in $table which was converted from myisam and has around 900k rows in it).

for me, the standard count without use index takes around 8 seconds - the count with forced index takes below 0.1 seconds.

any thoughts on why this is so?</description>
		<content:encoded><![CDATA[<p>ready for a headache? try &#8220;select count($field) from $table;&#8221; and compare the time it takes to &#8220;select count($field) from $table use index($field);&#8221;<br />
($field is the primary key (int autoincrement) in $table which was converted from myisam and has around 900k rows in it).</p>
<p>for me, the standard count without use index takes around 8 seconds &#8211; the count with forced index takes below 0.1 seconds.</p>
<p>any thoughts on why this is so?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fekke</title>
		<link>http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/comment-page-1/#comment-419585</link>
		<dc:creator>Fekke</dc:creator>
		<pubDate>Sun, 21 Dec 2008 05:26:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/#comment-419585</guid>
		<description>3,000 records may be a problem. If you have three tables with 1,000 records each and you do a join involving the three tables with no indexes at all then you would have 1,000,000,000 records full scan to fetch the results.

So the optimization you acquire by creating the proper indexes not only relies on how many records your tables have but also what your queries are.</description>
		<content:encoded><![CDATA[<p>3,000 records may be a problem. If you have three tables with 1,000 records each and you do a join involving the three tables with no indexes at all then you would have 1,000,000,000 records full scan to fetch the results.</p>
<p>So the optimization you acquire by creating the proper indexes not only relies on how many records your tables have but also what your queries are.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

