If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Poor performance for count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-04, 11:24
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Poor performance for count

Hi all,

I'm using MySQL 3.23.54, running on a Sun system 1GHZ with 1MB RAM. My table is about 600,000 records (each is about 50 bytes). When I run the following query it takes 5 secs (!) and returns 226,000:

SELECT count(*),tran_type from logrecord WHERE device_id=4000 and tran_datetime between '2003-12-23 16:36:09' and '2004-03-23 14:31:28' GROUP BY tran_type

I've already tries using count(indexed_col) but it didn't help. Also, if it using an index or not doesn't change the time.

Any idea?

Thanks,

Tal
Reply With Quote
  #2 (permalink)  
Old 04-01-04, 08:24
ajandris ajandris is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
make indexes for columns:
device_id, tran_datetime and tran_type

These are search or sort columns...
Reply With Quote
  #3 (permalink)  
Old 04-01-04, 08:29
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
I have the following index:

CREATE INDEX type_rep on logrecord(device_id, tran_type, onus, tran_datetime)

shouldn't this work? on the explain I see it gets this index and uses it, but it takes the same time with or without it....
Reply With Quote
  #4 (permalink)  
Old 04-01-04, 08:49
ajandris ajandris is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
multiple column index could not work for single column from this index.
Create indexes for each column. It may help.
Reply With Quote
  #5 (permalink)  
Old 04-01-04, 11:09
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
That didn't do much... it takes the same time...
Reply With Quote
  #6 (permalink)  
Old 04-01-04, 14:33
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: Poor performance for count

Quote:
Originally posted by localt
Hi all,

I'm using MySQL 3.23.54, running on a Sun system 1GHZ with 1MB RAM. My table is about 600,000 records (each is about 50 bytes). When I run the following query it takes 5 secs (!) and returns 226,000:

SELECT count(*),tran_type from logrecord WHERE device_id=4000 and tran_datetime between '2003-12-23 16:36:09' and '2004-03-23 14:31:28' GROUP BY tran_type

I've already tries using count(indexed_col) but it didn't help. Also, if it using an index or not doesn't change the time.

Any idea?
Tal
If you are using InnoDB table type, this is a well known problem that can't be fixed for now...
Reply With Quote
  #7 (permalink)  
Old 04-01-04, 14:42
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Re: Poor performance for count

How can I know it is? Also, if so, how can I change it?
Reply With Quote
  #8 (permalink)  
Old 04-01-04, 14:46
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: Poor performance for count

Quote:
Originally posted by localt
How can I know it is? Also, if so, how can I change it?
Well, if you need transations, you have to stay with InnoDb. But if you can live without transactions , then:

You can tell the type of your tables by looking at: SHOW TABLE STATUS;
You can change from InnoDb to MyISAM by executing: ALTER TABLE yourtablename TYPE=MyISAM;

Hope this helps
Reply With Quote
  #9 (permalink)  
Old 04-01-04, 14:55
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Re: Poor performance for count

Thanks bstjean, but it didn't.... My tables are already MyISAM.... any more ideas anyone? please?
Reply With Quote
  #10 (permalink)  
Old 04-01-04, 14:58
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: Poor performance for count

Quote:
Originally posted by localt
Thanks bstjean, but it didn't.... My tables are already MyISAM.... any more ideas anyone? please?
Do you have a primary key on this table?

Then try with count(*) and count(yourprimarykey) to test which one is the fastest
Reply With Quote
  #11 (permalink)  
Old 04-01-04, 15:02
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Re: Poor performance for count

I have... Same time... I'm really clue less... and it is using the index (or so it says on the explain)...
Reply With Quote
  #12 (permalink)  
Old 04-01-04, 15:13
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: Poor performance for count

Quote:
Originally posted by localt
I have... Same time... I'm really clue less... and it is using the index (or so it says on the explain)...
Perhaps you show post a DESCRIBE of your table as well as a SHOW INDEX and the EXPLAIN of your query so we can understand better what is going on...
Reply With Quote
  #13 (permalink)  
Old 04-02-04, 10:03
localt localt is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Re: Poor performance for count

Ok, here it is:

The discribe of the table:

mysql> describe logrecord;
+---------------+---------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------------------+-------+
| device_id | decimal(7,0) | | | 0 | |
| device_type | decimal(2,0) | | | 0 | |
| inst | varchar(15) | | | | |
| onus | decimal(1,0) | | | 0 | |
| tran_type | varchar(4) | YES | | NULL | |
| tran_time | decimal(2,0) | | | 0 | |
| tp_fee | decimal(4,0) | | | 0 | |
| chp_fee | decimal(4,0) | | | 0 | |
| tran_datetime | datetime | | | 0000-00-00 00:00:00 | |
| amount_auth | decimal(15,0) | YES | | NULL | |
| void_code | decimal(4,0) | | | 0 | |
+---------------+---------------+------+-----+---------------------+-------+

indexes:

mysql> show index from logrecord;
+-----------+------------+----------+--------------+---------------+-----------+
-------------+----------+--------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |
Cardinality | Sub_part | Packed | Comment |
+-----------+------------+----------+--------------+---------------+-----------+
-------------+----------+--------+---------+
| logrecord | 1 | type_rep | 1 | device_id | A |
55 | NULL | NULL | |
| logrecord | 1 | type_rep | 2 | tran_type | A |
344 | NULL | NULL | |
| logrecord | 1 | type_rep | 3 | onus | A |
487 | NULL | NULL | |
| logrecord | 1 | type_rep | 4 | tran_datetime | A |
576585 | NULL | NULL | |
| logrecord | 1 | onus_rep | 1 | onus | A |
1 | NULL | NULL | |
| logrecord | 1 | onus_rep | 2 | tran_type | A |
15 | NULL | NULL | |
| logrecord | 1 | onus_rep | 3 | void_code | A |
224 | NULL | NULL | |
| logrecord | 1 | onus_rep | 4 | inst | A |
655 | NULL | NULL | |
| logrecord | 1 | onus_rep | 5 | tran_datetime | A |
576585 | NULL | NULL | |
| logrecord | 1 | onus_rep | 6 | device_type | A |
576585 | NULL | NULL | |
| logrecord | 1 | avrg_rep | 1 | device_id | A |
55 | NULL | NULL | |
| logrecord | 1 | avrg_rep | 2 | void_code | A |
1337 | NULL | NULL | |
| logrecord | 1 | avrg_rep | 3 | tran_type | A |
2200 | NULL | NULL | |
| logrecord | 1 | avrg_rep | 4 | amount_auth | A |
28829 | NULL | NULL | |
| logrecord | 1 | avrg_rep | 5 | tran_datetime | A |
576585 | NULL | NULL | |
| logrecord | 1 | act_rep | 1 | inst | A |
15 | NULL | NULL | |
| logrecord | 1 | act_rep | 2 | tran_datetime | A |
576585 | NULL | NULL | |
| logrecord | 1 | act_rep | 3 | device_type | A |
576585 | NULL | NULL | |
| logrecord | 1 | resp_rep | 1 | tran_time | A |
10 | NULL | NULL | |
| logrecord | 1 | resp_rep | 2 | inst | A |
95 | NULL | NULL | |
| logrecord | 1 | resp_rep | 3 | tran_datetime | A |
576585 | NULL | NULL | |
| logrecord | 1 | resp_rep | 4 | device_type | A |
576585 | NULL | NULL | |
+-----------+------------+----------+--------------+---------------+-----------+
-------------+----------+--------+---------+

and the explain:

mysql> explain SELECT count(*),tran_type from logrecord WHERE device_id=4000 and
tran_datetime between '2003-12-23 16:36:09' and '2004-03-23 14:31:28' GROUP BY
tran_type;
+-----------+------+-------------------+----------+---------+-------+--------+--
-----------------------+
| table | type | possible_keys | key | key_len | ref | rows | E
xtra |
+-----------+------+-------------------+----------+---------+-------+--------+--
-----------------------+
| logrecord | ref | type_rep,avrg_rep | type_rep | 8 | const | 260164 | w
here used; Using index |
+-----------+------+-------------------+----------+---------+-------+--------+--
-----------------------+
Reply With Quote
  #14 (permalink)  
Old 04-08-04, 18:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
Just for the jolly factor, could you try:
PHP Code:
CREATE INDEX ptp_junque ON logrecord (device_idtran_datetimetran_type
If that doesn't help, then try using myisamchk to force that index order on the data rows themselves.

-PatP
Reply With Quote
  #15 (permalink)  
Old 04-08-04, 18:17
rdjabarov rdjabarov is offline
Registered User
 
Join Date: Jul 2003
Location: San Antonio, TX
Posts: 3,611
...That's where you're hanging out, traitor!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On