查询效果 - '左连接为空' vs'不存在select'

时间:2015-06-18 13:33:40

标签: mariadb

我对我想要执行的查询有疑问,但我不知道什么是最好的性能。我需要让所有单词排除与表wordfilter有关系的单词。

查询的输出是正确的,但也许有更好的解决方案。我对查询计划几乎一无所知,我现在试图理解它。

SELECT CONCAT(SPACE(1), UCASE(stocknews.word.word), SPACE(1)) AS word, stocknews.word.language 
FROM stocknews.word 
WHERE NOT EXISTS (SELECT word_id FROM stocknews.wordfilter WHERE stocknews.word.id = word_id) 
AND user_id = 1

+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type  | table      | type  | possible_keys | key     | key_len | ref   | rows | extra       |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | PRIMARY      | word       | ref   | user_id       | user_id | 4       | const | 843  | Using where |
|  2 | MATERIALIZED | wordfilter | index | PRIMARY       | PRIMARY | 756     |       | 16   | Using index |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+

SELECT CONCAT(SPACE(1), UCASE(stocknews.word.word), SPACE(1)) AS word, stocknews.word.language 
FROM stocknews.word 
LEFT JOIN stocknews.wordfilter ON stocknews.word.id = stocknews.wordfilter.word_id 
WHERE stocknews.wordfilter.word_id IS NULL AND user_id = 1

+----+-------------+------------+------+---------------+---------+---------+---------+------+--------------------------------------+
| id | select_type | table      | type | possible_keys | key     | key_len | ref     | rows | extra                                |
+----+-------------+------------+------+---------------+---------+---------+---------+------+--------------------------------------+
|  1 | SIMPLE      | word       | ref  | user_id       | user_id | 4       | const   | 843  |                                      |
|  1 | SIMPLE      | wordfilter | ref  | PRIMARY       | PRIMARY | 4       | word.id | 1    | Using where; Using index; Not exists |
+----+-------------+------------+------+---------------+---------+---------+---------+------+--------------------------------------+

欢迎任何帮助!解释会很好。

修改

对于查询1:

+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_commit             | 1     |
| Handler_delete             | 0     |
| Handler_discover           | 0     |
| Handler_external_lock      | 0     |
| Handler_icp_attempts       | 0     |
| Handler_icp_match          | 0     |
| Handler_mrr_init           | 0     |
| Handler_mrr_key_refills    | 0     |
| Handler_mrr_rowid_refills  | 0     |
| Handler_prepare            | 0     |
| Handler_read_first         | 1     |
| Handler_read_key           | 1044  |
| Handler_read_last          | 0     |
| Handler_read_next          | 859   |
| Handler_read_prev          | 0     |
| Handler_read_rnd           | 0     |
| Handler_read_rnd_deleted   | 0     |
| Handler_read_rnd_next      | 0     |
| Handler_rollback           | 0     |
| Handler_savepoint          | 0     |
| Handler_savepoint_rollback | 0     |
| Handler_tmp_update         | 0     |
| Handler_tmp_write          | 215   |
| Handler_update             | 0     |
| Handler_write              | 0     |
+----------------------------+-------+
25 rows in set (0.00 sec)

对于查询2:

+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_commit             | 1     |
| Handler_delete             | 0     |
| Handler_discover           | 0     |
| Handler_external_lock      | 0     |
| Handler_icp_attempts       | 0     |
| Handler_icp_match          | 0     |
| Handler_mrr_init           | 0     |
| Handler_mrr_key_refills    | 0     |
| Handler_mrr_rowid_refills  | 0     |
| Handler_prepare            | 0     |
| Handler_read_first         | 0     |
| Handler_read_key           | 844   |
| Handler_read_last          | 0     |
| Handler_read_next          | 843   |
| Handler_read_prev          | 0     |
| Handler_read_rnd           | 0     |
| Handler_read_rnd_deleted   | 0     |
| Handler_read_rnd_next      | 0     |
| Handler_rollback           | 0     |
| Handler_savepoint          | 0     |
| Handler_savepoint_rollback | 0     |
| Handler_tmp_update         | 0     |
| Handler_tmp_write          | 0     |
| Handler_update             | 0     |
| Handler_write              | 0     |
+----------------------------+-------+

1 个答案:

答案 0 :(得分:0)

这两种配方之间的竞争似乎很紧密。 (其他一些例子可能会显示更清晰的赢家。)

根据HANDLER值:查询1执行了更多read_keys和一些写入(与MATERIALIZED一起使用)。其他数字大致相同。因此,我得出结论,查询1速度较慢 - 尽管可能还不够慢,无法做出太大的改变。

我投票赞成LEFT JOIN作为更好的查询模式(在本例中)