简体   繁体   中英

MySQL: indexes to group by on joined column

I'm trying to optimize my indexes for doing a JOIN and then GROUP BY one of the columns from the joined table.

I'm testing by running the script below, playing with the indexes, but I just can't seem to figure out what indexes I need for the 3rd query.

SOLVED: adding dummy data makes sql behave differently and one of the indexes I previously tried works just fine!

CREATE DATABASE stats_idx_test;

USE stats_idx_test;

DROP TABLE stats;
CREATE TABLE stats (article_id INT, cnt INT, type INT);
ALTER TABLE stats ADD INDEX idxs1 (article_id, cnt, type);

DROP TABLE article;
CREATE TABLE article (id INT, cat_id INT);
ALTER TABLE article ADD INDEX idxa2 (cat_id, id);

INSERT INTO article (id, cat_id) VALUES (1, 1);
INSERT INTO stats (article_id, cnt, type) VALUES (1, 9, 1);
INSERT INTO stats (article_id, cnt, type) VALUES (1, 13, 2);

EXPLAIN 
SELECT SUM(stats.cnt)
FROM stats
WHERE stats.type = 1 AND stats.article_id = 1;
-- Using where; Using index

EXPLAIN 
SELECT article.cat_id, SUM(stats.cnt)
FROM stats
JOIN article ON (stats.article_id = article.id)
WHERE stats.type = 1 AND article.cat_id = 1;
-- Using where; Using index

EXPLAIN 
SELECT article.cat_id, SUM(stats.cnt)
FROM stats
JOIN article ON (stats.article_id = article.id)
WHERE stats.type = 1
GROUP BY article.cat_id;
-- Using index
-- Using where; Using index

For the group by you need a index on cat_id. The current index that you have cannot be applied.

Group by and indexing

Also you should consider making id a primary key.

添加虚拟数据会使sql的行为有所不同,我之前尝试过的索引之一也可以正常工作!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM