Home » Performance » MySQL » should a count with an index take this long?
should a count with an index take this long? [message #1364] Mon, 04 June 2007 23:56 Go to next message
GeoffreyF67  is currently offline GeoffreyF67
Messages: 21
Registered: May 2007
Junior Member

CREATE TABLE `WikiParagraphs2` (
  `ID` int(9) unsigned NOT NULL auto_increment,
  `Paragraph` text collate utf8_swedish_ci NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci AUTO_INCREMENT=73035277 ;

mysql> select count(ID) from WikiParagraphs2 where ID < 9999999;
+-----------+
| count(ID) |
+-----------+
|   7375964 | 
+-----------+
1 row in set (44.79 sec)

mysql> explain select count(ID) from WikiParagraphs2 where ID < 9999999;

+----+-------------+-----------------+-------+---------------+---------+---------+------+---------+--------------------------+
| id | select_type | table           | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    |
+----+-------------+-----------------+-------+---------------+---------+---------+------+---------+--------------------------+
|  1 | SIMPLE      | WikiParagraphs2 | range | PRIMARY       | PRIMARY | 4       | NULL | 7997361 | Using where; Using index | 
+----+-------------+-----------------+-------+---------------+---------+---------+------+---------+--------------------------+
1 row in set (0.05 sec)



It says it's using an index so I don't understand why it would take 45 seconds to get the count...any ideas?

Edit: Added table definition.

[Updated on: Tue, 05 June 2007 00:04]


G-Man
Re: should a count with an index take this long? [message #1367 is a reply to message #1364 ] Tue, 05 June 2007 19:49 Go to previous messageGo to next message
sterin  is currently offline sterin
Messages: 324
Registered: March 2007
Location: Sweden
Senior Member
How large is your key_buffer_size?

But you should also consider that even though it is using an index it has to examine 7,997,361 rows.

Which means that each row is handled in about 5 microseconds or 177,719 rows per second, which isn't a half bad figure.

The important thing and content of this is that you shouldn't expect miracles even though it is using an index.

when you run a COUNT(col1) it means that is has to check the value of col1 and determine if it is zero or NULL before we are addint it to the count as normal.

Re: should a count with an index take this long? [message #1368 is a reply to message #1364 ] Tue, 05 June 2007 19:57 Go to previous messageGo to next message
GeoffreyF67  is currently offline GeoffreyF67
Messages: 21
Registered: May 2007
Junior Member

Even though it has NOT NULL for the primary key field it still has to check that?

My key_buffer_size is set to 16MB.

I'll try increasing it to see if it helps. What is a good value for that on a server that is dedicated to mysql and apache?


G-Man
Re: should a count with an index take this long? [message #1370 is a reply to message #1364 ] Wed, 06 June 2007 13:37 Go to previous messageGo to next message
sterin  is currently offline sterin
Messages: 324
Registered: March 2007
Location: Sweden
Senior Member
The key_buffer_size is depending a bit on how much memory other applications (apache in our case) needs.
But the general rule of thumb is 25% of the available RAM should be used as key_cache.
The rest should be left unused so that the OS can use it for file cache which is what MyISAM tables are relying on since they don't have an internal data cache.
Like for example InnoDB where indexes and data is cached internally in the same memory pool.

BTW: I should have noticed it earlier, your query should be written with SELECT count(*) FROM ... instead of SELECT count(ID) FROM ... because you are after the number of rows and not the nr of ID's.
It makes it easier to read the query and understand what you are after.
And possibly it could have an effect on performance as well, but I'm not sure.
Re: should a count with an index take this long? [message #1371 is a reply to message #1364 ] Wed, 06 June 2007 15:11 Go to previous message
GeoffreyF67  is currently offline GeoffreyF67
Messages: 21
Registered: May 2007
Junior Member

Ahhh I see.

I raised the key size to 128MB yesterday but I've got 4gb on the box so perhaps I should raise it a bit higher Smile

I read somewhere that using /dev/shm is good for tmpfs - is that really true or should I not be doing that?

Current stats on my db have the following info:

http://www.pduck.com/status.txt

Thanks for the help!


G-Man
Previous Topic:Preventing filesort in a simple (but big) query?
Next Topic:SELECTs to slave, INSERTs to master by application?
Goto Forum:
  


Current Time: Mon Jul 6 17:16:55 EDT 2009

Total time taken to generate the page: 0.01190 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.