Mysql 在大型数据集上优化查询-使用JOIN还是IN?
假设我有标记的文章,一篇文章可以有n个标记。 目前大约有25万个标签条目,都指向后面 放在他们的物品上 现在,我想从符合特定条件的文章中查找所有标记。 我想出了两种不同的方法。这两种方法都有缺点,而且速度都很慢。 也许有人能为我指明正确的方向,告诉我如何加快他们的速度,甚至让我来 想出一个更好的解决办法 键(ind,rindex)是varchar(255),不幸的是这不能更改 问题#1 使用7.5-subselect在50ms内返回60条记录Mysql 在大型数据集上优化查询-使用JOIN还是IN?,mysql,sql,Mysql,Sql,假设我有标记的文章,一篇文章可以有n个标记。 目前大约有25万个标签条目,都指向后面 放在他们的物品上 现在,我想从符合特定条件的文章中查找所有标记。 我想出了两种不同的方法。这两种方法都有缺点,而且速度都很慢。 也许有人能为我指明正确的方向,告诉我如何加快他们的速度,甚至让我来 想出一个更好的解决办法 键(ind,rindex)是varchar(255),不幸的是这不能更改 问题#1 使用7.5-subselect在50ms内返回60条记录 SELECT count(*) AS tagscou
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.1s-子选择在70ms内返回18k行
- 取14.2秒
我假设
artikel.ind
不限于按照与artikel.date
相同的顺序升序。如果是,显而易见的解决方案是对与日期范围相对应的rindex
添加一个限制
事实上,似乎正在使用一个合适的计划
在不更改数据类型的情况下,最好创建一个索引为
(artikel.date,tags.value,artikel.ind)
的物化视图,然后查询该视图。我假设artikel.ind
不限于按与artikel.date
相同的顺序升序。如果是,显而易见的解决方案是对与日期范围相对应的rindex
添加一个限制
事实上,似乎正在使用一个合适的计划
在不更改数据类型的情况下,您最好创建一个在
(artikel.date,tags.value,artikel.ind)
上索引的物化视图,然后查询该视图。您在这里比较两个不同的结果集,为什么要在#2中执行左外连接
?它应该是基于查询的internaljoin
#1。谢谢我更新了代码-很抱歉造成混淆。您能为查询中使用的所有表添加架构吗?(对于这两个查询,SHOW CREATE TABLE your_TABLE
)的输出以及EXPLAIN your_query
的输出。因此,我们可以更好地了解发生了什么。您是否尝试过在查询中使用EXPLAIN
?在大多数情况下,这将对查询执行的性能提供有价值的见解。它说查询使用定义的键以及文件排序,这是为了避免您在这里比较两个不同的结果集,为什么要在#2中执行左外连接
?它应该是基于查询的internaljoin
#1。谢谢我更新了代码-很抱歉造成混淆。您能为查询中使用的所有表添加架构吗?(对于这两个查询,SHOW CREATE TABLE your_TABLE
)的输出以及EXPLAIN your_query
的输出。因此,我们可以更好地了解发生了什么。您是否尝试过在查询中使用EXPLAIN
?在大多数情况下,这将为查询执行的性能提供有价值的见解。它说查询使用定义的键以及要避免的文件排序。谢谢,我将实现它-我将它标记为正确,因为它是唯一的答案。谢谢,我将实现它-我将它标记为正确,因为它是唯一的答案。
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 |
+----+-------------+----------+--------+-------------------------------------------------------------------------------------+---------------------+---------+---------------------+--------+----------------------------------------------+