| How to improve the speed of mysql query using count(*) [message #1134] |
Mon, 23 April 2007 06:32  |
julian Messages: 13 Registered: March 2007 |
Junior Member |
|
|
Hi
I'm using this kind of queries in mysql in InnoDB engine
Select count(*) from marking1 where persondate between '2007-04-23 00:00:00.000' and '2007-04-23 23:59:59.999' and PersonName='aaa'
While executing these queries from front end VB, It takes above 5 secs with 50 thousand records.
How can I improve speed for this kind of queries. Is there any alternation for this command.
Any one knows , Explain
thanks
|
|
|
|
| Re: How to improve the speed of mysql query using count(*) [message #1174 is a reply to message #1134 ] |
Mon, 30 April 2007 17:30   |
sterin Messages: 323 Registered: March 2007 Location: Sweden |
Senior Member |
|
|
Actually a COUNT(*) without any _WHERE condition_ is much faster on MyISAM since it stores the total nr of rows in the table in the table header.
But a COUNT(*) with a where condition like in your case needs a proper index to gain speed both on MyISAM and InnoDB.
What indexes do you have on the table?
My suggestion is a combined index on (PersonName, persondate) because then both columns in your WHERE condition is part of the index and that is the optimum.
[Updated on: Mon, 30 April 2007 17:31]
|
|
|
|
|
|
| Re: How to improve the speed of mysql query using count(*) [message #1196 is a reply to message #1134 ] |
Fri, 04 May 2007 05:12  |
sterin Messages: 323 Registered: March 2007 Location: Sweden |
Senior Member |
|
|
1.
Just to check, is this true?:
It seems so incredibly small.
2.
I just want to make sure,
did you really create a combined index or did you create two indexes, one each column?
I usually name my indexes like:
table_ix_col1_col2
Then I know which columns that are part of the index by just looking at the name.
In your case my index would be named:
| Quote: |
marking1_ix_personname_persondate
|
Then I know immediately what this index does.
3. my.cnf
That was a very small my.cnf.
And that means that you don't have almost any internal caching configured since MySQL is very conservative with the default values.
Here's a couple of addtions to your my.conf, just the most important ones for InnoDB:
| Quote: |
innodb_buffer_pool_size = 64M
innodb_additional_mem_pool_size = 8M
innodb_log_file_size = 20M
innodb_log_buffer_size = 32M
innodb_flush_log_at_trx_commit = 1
|
|
|
|