MySQL GROUP BY оптимизация

0

Этот вопрос является более конкретной версией предыдущего вопроса, который я задал

Таблица


CREATE TABLE Test4_ClusterMatches 
(
    `match_index` INT UNSIGNED,
    `cluster_index` INT UNSIGNED, 
    `id` INT NOT NULL AUTO_INCREMENT,
    `tfidf` FLOAT,
    PRIMARY KEY (`cluster_index`,`match_index`,`id`)
);

Запрос, который я хочу запустить


mysql> explain SELECT `match_index`, SUM(`tfidf`) AS total 
FROM Test4_ClusterMatches WHERE `cluster_index` IN (1,2,3 ... 3000) 
GROUP BY `match_index`;

Проблема с запросом


Он использует временные и filesort, чтобы замедлить

+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+-----------------------------------------------------------+
| id | select_type | table                | type  | possible_keys | key     | key_len | ref  | rows  | Extra                                                     |
+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+-----------------------------------------------------------+
|  1 | SIMPLE      | Test4_ClusterMatches | range | PRIMARY       | PRIMARY | 4       | NULL | 51540 | Using where; Using index; Using temporary; Using filesort | 
+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+-----------------------------------------------------------+
При текущем индексировании запрос должен сначала сортироваться по принципу cluster_index, чтобы исключить использование временного файла и fileort, но при этом дает неправильные результаты для sum (tfidf). Изменение первичного ключа на
PRIMARY KEY (`match_index`,`cluster_index`,`id`)

Не использует таблицы сортировки файлов или temp, но он использует 14 932 441 строку, поэтому он также замедляет

+----+-------------+----------------------+-------+---------------+---------+---------+------+----------+--------------------------+
| id | select_type | table                | type  | possible_keys | key     | key_len | ref  | rows     | Extra                    |
+----+-------------+----------------------+-------+---------------+---------+---------+------+----------+--------------------------+
|  1 | SIMPLE      | Test5_ClusterMatches | index | NULL          | PRIMARY | 16      | NULL | 14932441 | Using where; Using index | 
+----+-------------+----------------------+-------+---------------+---------+---------+------+----------+--------------------------+

Tight Index Scan


Используя жесткую проверку индекса, запустив поиск только одного индекса

mysql> explain SELECT match_index, SUM(tfidf) AS total
FROM Test4_ClusterMatches WHERE cluster_index =3000 
GROUP BY match_index;
Устраняет временные таблицы и файлы.
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+--------------------------+
| id | select_type | table                | type | possible_keys | key     | key_len | ref   | rows | Extra                    |
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | Test4_ClusterMatches | ref  | PRIMARY       | PRIMARY | 4       | const |   27 | Using where; Using index | 
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+--------------------------+
Я не уверен, что это может быть использовано с каким-то волшебным sql-fu, с которым я еще не сталкивался?

Вопрос


Как я могу изменить свой запрос так, чтобы он использовал 3000 кластерных указателей, избегает использования временного и файлового управления без необходимости использования 14 932 441 строки?



Обновление


Использование таблицы

CREATE TABLE Test6_ClusterMatches 
(
  match_index INT UNSIGNED,
  cluster_index INT UNSIGNED, 
  id INT NOT NULL AUTO_INCREMENT,
  tfidf FLOAT,
  PRIMARY KEY (id),
  UNIQUE KEY(cluster_index,match_index)
);

Затем запрос ниже дает 10 строк в наборе (0,41 с):)

SELECT `match_index`, SUM(`tfidf`) AS total FROM Test6_ClusterMatches WHERE 
`cluster_index` IN (.....)
GROUP BY `match_index` ORDER BY total DESC LIMIT 0,10;

но его использование временного и filesort

+----+-------------+----------------------+-------+---------------+---------------+---------+------+-------+----------------------------------------------+
| id | select_type | table                | type  | possible_keys | key           | key_len | ref  | rows  | Extra                                        |
+----+-------------+----------------------+-------+---------------+---------------+---------+------+-------+----------------------------------------------+
|  1 | SIMPLE      | Test6_ClusterMatches | range | cluster_index | cluster_index | 5       | NULL | 78663 | Using where; Using temporary; Using filesort | 
+----+-------------+----------------------+-------+---------------+---------------+---------+------+-------+----------------------------------------------+

Мне интересно, если theres все равно, чтобы получить его быстрее, исключив использование временных и using filesort?

  • 0
    @Ben - Вам не нужно cluster_index , match_index в качестве первичного ключа, id уже определен как auto_increment, оставить его в покое в качестве первичного ключа. Построить уникальный ключ на cluster_index , match_index , и повторить запрос, видит улучшение?
  • 0
    Чтобы воспользоваться преимуществами индексного индекса innodb или первичного ключа ( dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html ), у вас должен быть следующий первичный ключ (cluster_id, match_id, ...) - попробуй тогда: P
Показать ещё 5 комментариев
Теги:
optimization
query-optimization

2 ответа

2
Лучший ответ

У меня был быстрый взгляд, и это то, что я придумал - надеюсь, что это поможет...

Таблица SQL

drop table if exists cluster_matches;
create table cluster_matches
(
 cluster_id int unsigned not null,
 match_id int unsigned not null,
 ...
 tfidf float not null default 0,
 primary key (cluster_id, match_id) -- if this isnt unique add id to the end !!
)
engine=innodb;

Данные тестирования

select count(*) from cluster_matches

count(*)
========
17974591

select count(distinct(cluster_id)) from cluster_matches;

count(distinct(cluster_id))
===========================
1000000

select count(distinct(match_id)) from cluster_matches;

count(distinct(match_id))
=========================
6000

explain select
 cm.match_id,
 sum(tfidf) as sum_tfidf,
 count(*) as count_tfidf
from
 cluster_matches cm
where
 cm.cluster_id between 5000 and 10000
group by
 cm.match_id
order by
 sum_tfidf desc limit 10;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
==  =========== =====   ====    =============   === ======= === ====    =====
1   SIMPLE  cm  range   PRIMARY PRIMARY 4       290016  Using where; Using temporary; Using filesort

runtime - 0.067 seconds.

Довольно респектабельное время работы 0,067 секунды, но я думаю, что мы можем сделать это лучше.

Сохраненная процедура

Вам придется простить меня за то, что вы не хотите вводить/передавать список из 5000+ случайных кластеров!

call sum_cluster_matches(null,1); -- for testing
call sum_cluster_matches('1,2,3,4,....5000',1);

Основная часть sproc не очень элегантна, но все, что она делает, - это разделение строки csv на отдельные cluster_ids и заполнение таблицы temp.

drop procedure if exists sum_cluster_matches;

delimiter #

create procedure sum_cluster_matches
(
in p_cluster_id_csv varchar(65535),
in p_show_explain tinyint unsigned
)
proc_main:begin

declare v_id varchar(10);
declare v_done tinyint unsigned default 0;
declare v_idx int unsigned default 1;

    create temporary table tmp(cluster_id int unsigned not null primary key);   

    -- not every elegant - split the string into tokens and put into a temp table...

    if p_cluster_id_csv is not null then
        while not v_done do
            set v_id = trim(substring(p_cluster_id_csv, v_idx, 
                if(locate(',', p_cluster_id_csv, v_idx) > 0, 
                        locate(',', p_cluster_id_csv, v_idx) - v_idx, length(p_cluster_id_csv))));

                if length(v_id) > 0 then
                set v_idx = v_idx + length(v_id) + 1;
                        insert ignore into tmp values(v_id);
                else
                set v_done = 1;
                end if;
        end while;
    else
        -- instead of passing in a huge comma separated list of cluster_ids im cheating here to save typing
        insert into tmp select cluster_id from clusters where cluster_id between 5000 and 10000;
        -- end cheat
    end if;

    if p_show_explain then

        select count(*) as count_of_tmp from tmp;

        explain
        select
         cm.match_id,
         sum(tfidf) as sum_tfidf,
         count(*) as count_tfidf
        from
         cluster_matches cm
        inner join tmp on tmp.cluster_id = cm.cluster_id
        group by
         cm.match_id
        order by
         sum_tfidf desc limit 10;
    end if;

    select
     cm.match_id,
     sum(tfidf) as sum_tfidf,
     count(*) as count_tfidf
    from
     cluster_matches cm
    inner join tmp on tmp.cluster_id = cm.cluster_id
    group by
     cm.match_id
    order by
     sum_tfidf desc limit 10;

    drop temporary table if exists tmp;

end proc_main #

delimiter ;

Результаты

call sum_cluster_matches(null,1);

count_of_tmp
============
5001

id  select_type table   type    possible_keys   key key_len ref rows    Extra
==  =========== =====   ====    =============   === ======= === ====    =====
1   SIMPLE  tmp index   PRIMARY PRIMARY 4       5001    Using index; Using temporary; Using filesort
1   SIMPLE  cm  ref PRIMARY PRIMARY 4   vldb_db.tmp.cluster_id  8   

match_id    sum_tfidf   count_tfidf
========    =========   ===========
1618        387         64
1473        387         64
3307        382         64
2495        373         64
1135        373         64
3832        372         57
3203        362         58
5464        358         67
2100        355         60
1634        354         52

runtime 0.028 seconds.

Объяснение плана и времени выполнения значительно улучшилось.

  • 0
    Я думаю, что вы не можете игнорировать поле auto_increment, которое делает каждую запись уникальной друг для друга
  • 0
    ну, я действительно не вижу смысла в auto_inc PK, но если это необходимо, вы можете просто добавить его в конец существующего первичного ключа (cluster_id, match_id, some_other_id) без каких-либо изменений в производительности
Показать ещё 1 комментарий
0

Если значения cluster_index в WHERE являются непрерывными, вместо IN используйте:

WHERE (cluster_index >= 1) and (cluster_index <= 3000)

Если значения не являются непрерывными, вы можете создать временную таблицу для хранения значений cluster_index с индексом и использовать INNER JOIN во временную таблицу.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню