タグ付けされた記事があるとすると、1つの記事にn個のタグを付けることができます。現在、約25万のタグエントリがあり、それらはすべて所属する記事を指し示しています。
今、私は特定の基準に一致する記事からすべてのタグを見つけたいと思います。私は2つの異なるアプローチを思いついた。どちらにも欠点があり、遅いです。誰かが私をスピードアップする方法について正しい方向に向けたり、より良い解決策を考え出すことができるかもしれません。
キー(ind、rindex)はvarchar(255)ですが、残念ながらこれは変更できません
クエリ#1
7.5を取る-副選択は50msで60レコードを返します
SELECT count(*) AS tagscount, tags.value FROM tags
WHERE tags.`rindex` IN
(
SELECT article.ind
FROM article
INNER JOIN struktur ON (struktur.ind = article.struktur)
WHERE article.date = '2011-12-21'
)
AND tags.`rtable` = 'article'
GROUP BY tags.value ORDER BY tagscount DESC LIMIT 20
クエリ#2
60msかかります
SELECT count(*) AS tagscount, tags.value FROM tags
INNER JOIN article ON (article.ind = tags.rindex AND tags.rtable = 'article')
LEFT JOIN structure ON (article.structure = structure.ind)
WHERE article.date = '2011-12-21'
GROUP BY tags.value ORDER BY tagscount DESC LIMIT 20
奇妙な部分-重要
クエリ#1に変更article.date = '2011-12-21'
したときarticle.date >= '2009-12-21'
- 10.1秒かかる-subselectは70msで18k行を返します
クエリ#2
- 14.2秒かかる
さらに詳しい情報が必要な場合は、喜んで提供させていただきます
スキーマ
mysql> SHOW COLUMNS FROM tags;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ind | varchar(255) | NO | PRI | | |
| rtable | varchar(255) | NO | MUL | | |
| rindex | varchar(255) | NO | MUL | | |
| value | varchar(40) | YES | MUL | NULL | |
+---------+--------------+------+-----+---------+-------+
mysql> SHOW indexes FROM tags
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tags | 0 | tags_ind | 1 | ind | A | 275834 | NULL | NULL | | BTREE | |
| tags | 1 | tags_tag | 1 | tag | A | 27583 | NULL | NULL | YES | BTREE | |
| tags | 1 | tags_rindex | 1 | rindex | A | 55166 | NULL | NULL | | BTREE | |
| tags | 1 | tags_rindex_tabelle | 1 | tabelle | A | 4 | 30 | NULL | | BTREE | |
| tags | 1 | tags_rindex_tabelle | 2 | rindex | A | 55166 | 50 | NULL | | BTREE | |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
mysql> SHOW COLUMNS FROM structure;
+------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+-------+
| ind | varchar(255) | NO | PRI | | |
+------------------------+--------------+------+-----+---------+-------+
mysql> SHOW COLUMNS FROM artikel;
+--------------------+--------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+------------+-------+
| ind | varchar(255) | NO | PRI | | |
| date | date | NO | MUL | 0000-00-00 | |
+--------------------+--------------+------+-----+------------+-------+
説明
mysql> explain #1
+----+--------------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+
| 1 | PRIMARY | tags | ref | tags_rindex_tabelle | tags_rindex_tabelle | 32 | const | 177175 | Using where; Using temporary; Using filesort |
| 2 | DEPENDENT SUBQUERY | artikel | eq_ref | artikel_ind,zeitraum_start_i,freigabe_i,korrektur_i,struktur_i,artikel_start_slot_i | artikel_ind | 257 | func | 1 | Using where |
| 2 | DEPENDENT SUBQUERY | struktur | eq_ref | struktur_ind,struktur_host | struktur_ind | 257 | ec.artikel.struktur | 1 | Using where |
+----+--------------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+
mysql> explain #2
+----+-------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+
| 1 | SIMPLE | tags | ref | tags_rindex,tags_rindex_tabelle | tags_rindex_tabelle | 32 | const | 177175 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | artikel | eq_ref | artikel_ind,zeitraum_start_i,freigabe_i,korrektur_i,struktur_i,artikel_start_slot_i | artikel_ind | 257 | ec.tags.rindex | 1 | Using where |
| 1 | SIMPLE | struktur | eq_ref | struktur_ind,struktur_host | struktur_ind | 257 | ec.artikel.struktur | 1 | Using where |
+----+-------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+