MySQL Forums
Forum List  »  Performance

Re: optimizing count statement
Posted by: Karolis
Date: October 12, 2005 06:31AM

Sure. Here is a fast example:

mysql> SELECT COUNT(*) AS Cnt FROM INDEX_KEYS T1, INDEX_KEYS T2 WHERE (T1.KeywordID = 7 AND T2.KeywordID = 79 ) AND (T1.DocumentID = T2.DocumentID );
+------+
| Cnt |
+------+
| 1103 |
+------+
1 row in set (0.02 sec)




Here is a slow example:
mysql> SELECT COUNT(*) AS Cnt FROM INDEX_KEYS T1, INDEX_KEYS T2 WHERE (T1.KeywordID = 7 AND T2.KeywordID = 8 ) AND (T1.DocumentID = T2.DocumentID );
+------+
| Cnt |
+------+
| 6204 |
+------+
1 row in set (1.42 sec)



Here is EXPLAIN query of the slow example:
Not sure if It can be read on this forum though...
+----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+----------------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+----------------------------+------+--------------------------+
| 1 | SIMPLE | T2 | ref | PRIMARY,INDEXKEYS_KeywordID,index_PR_KeyRank,index_PR2_KeyRank | INDEXKEYS_KeywordID | 4 | const | 4364 | Using where; Using index |
| 1 | SIMPLE | T1 | eq_ref | PRIMARY,INDEXKEYS_KeywordID,index_PR_KeyRank,index_PR2_KeyRank | PRIMARY | 8 | search.T2.DocumentID,const | 1 | Using where; Using index |
+----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+----------------------------+------+--------------------------+


Table Structure:
mysql> DESCRIBE INDEX_KEYS;
+-------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+-------+
| DocumentID | int(11) | | PRI | 0 | |
| KeywordID | int(11) | | PRI | 0 | |
| KeyDensity | int(11) | | | 0 | |
| KeyWeight | double | | | 0 | |
| KeyTitleDensity | int(11) | | | 0 | |
| KeyTitleWeight | double | | | 0 | |
| KeyLinkDensity | int(11) | | | 0 | |
| KeyLinkWeight | double | | | 0 | |
| KeyHeadingDensity | int(11) | | | 0 | |
| KeyHeadingWeight | double | | | 0 | |
| KeyDocumentRank | double | | | 0 | |
| PR_KeyRank | double | | | 0 | |
| PR2_KeyRank | double | | | 0 | |
+-------------------+---------+------+-----+---------+-------+
13 rows in set (0.00 sec)


Indexes:
mysql> SHOW INDEX FROM INDEX_KEYS;
+------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| INDEX_KEYS | 0 | PRIMARY | 1 | DocumentID | A | 14766 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 0 | PRIMARY | 2 | KeywordID | A | 590652 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | INDEXKEYS_KeywordID | 1 | KeywordID | A | 24610 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR_KeyRank | 1 | KeywordID | A | 84378 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR_KeyRank | 2 | DocumentID | A | 590652 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR_KeyRank | 3 | PR_KeyRank | A | 590652 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR2_KeyRank | 1 | KeywordID | A | 4018 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR2_KeyRank | 2 | DocumentID | A | 590652 | NULL | NULL | | BTREE | |
| INDEX_KEYS | 1 | index_PR2_KeyRank | 3 | PR2_KeyRank | A | 590652 | NULL | NULL | | BTREE | |
+------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
9 rows in set (0.01 sec)

Karolis

Options: ReplyQuote


Subject
Views
Written By
Posted
3931
October 11, 2005 11:30AM
1835
October 12, 2005 04:56AM
Re: optimizing count statement
1760
October 12, 2005 06:31AM
1439
October 12, 2005 07:11AM
1639
October 12, 2005 07:40AM
1557
October 12, 2005 07:53AM
1729
October 12, 2005 07:50AM
1710
October 12, 2005 08:01AM
1701
October 12, 2005 08:05AM
1433
October 12, 2005 08:23AM
1722
October 12, 2005 08:22AM
1671
October 12, 2005 08:38AM
1799
October 12, 2005 08:46AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.