鉴于我有被标记的文章,一篇文章可以有 n 个标签。目前大约有 250,000 个标签条目都指向他们所属的文章。
现在我想从符合某些条件的文章中找到所有标签。我想出了两种不同的方法。两者都有缺点并且速度很慢。可能有人可以为我指出如何加快速度甚至提出更好的解决方案的正确方向。
键 (ind,rindex) 是 varchar(255) 不幸的是这不能改变
查询 #1
采取 7.5 - 子选择在 50 毫秒内返回 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
耗时 60 毫秒
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
奇怪的部分 -重要
当我更改article.date = '2011-12-21'
为article.date >= '2009-12-21'
查询 #1
- 耗时 10.1 秒 - 子选择在 70 毫秒内返回 18k 行
查询 #2
- 耗时 14.2s
如果您需要更多信息,我很乐意提供
模式
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 |
+----+-------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+