4

MySql indexes are not applied in GROUP BY

 2 years ago
source link: https://www.codesd.com/item/mysql-indexes-are-not-applied-in-group-by.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MySql indexes are not applied in GROUP BY

advertisements

I have two tables to make my search engine, one containing all keywords and the other contains all the possible targets for each keyword.

Table: keywords
id (int)
keyword (varchar)

Table: results
id (int)
keyword_id (int)
table_id (int)
target_id (int)

For both tables, I set MyISAM as storage engine since 95% of the times I am just running select queries on these tables and in 5% of the times, insert queries. And off course, I already compared the performance using InnoDB and the performance was poor considering my later queries.

I also added the following indexes

keywords.keyword (unique)
results.keyword_id (index)
results.table_id (index)
results.target_id (index)

in the keywords table, I have about 1.2 million records and in results table I have about 9.8 million records.

Now the issue is that I run the following query and the results is made in 0.0014 seconds

SELECT rs.table_id, rs.target_id
FROM keywords ky INNER JOIN results rs ON ky.id=rs.keyword_id
WHERE ky.keyword LIKE "x%" OR ky.keyword LIKE "y%"

But when I add GROUP BY, the result is made in 0.2 seconds

SELECT rs.table_id, rs.target_id
FROM keywords ky INNER JOIN results rs ON ky.id=rs.keyword_id
WHERE ky.keyword LIKE "x%" OR ky.keyword LIKE "y%"
GROUP BY rs.table_id, rs.target_id

I tested composite indexes, single column indexes and even dropping table_id and target_id indexes but in all the cases the performance is the same and it seems that in Group By clause, the index is not applied.

The explain plan shows that:

id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | ky | range | PRIMARY,keyword | keyword | 767 | NULL | 3271 | Using index condition; Using where; Using temporary; Using filesort
1 | SIMPLE | rs | ref | keyword_id | keyword_id | 4 | ky.id | 3

I have the following composite key already added

ALTER TABLE results ADD INDEX `table_id` (`table_id`, `target_id`) USING BTREE;


Here's MySQL documentation for GROUP BY optimization, this is what it says:

The most important preconditions for using indexes for GROUP BY are that all GROUP BY columns reference attributes from the same index

So, if you have different index on these two columns, they won't be used by GROUP BY. You should try creating a composite index on table_id and target_id.

Also, the query seem to be using LIKE operator. Please note that if the value being compared in LIKE has leading wildcard in it then MySQL won't be able to use any index for that column anyway. Have a look at explain plan of the query and see which indices are used.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK