我有两个分离的查询具有相同的输出。现在我试着去了解哪一个更好?了解MySQL中EXPLAIN的结果
查询1:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|-------|------|---------------|--------|---------|--------|------|----------------------------------------------------|
| 1 | SIMPLE | t1 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
| 1 | SIMPLE | t2 | ALL | (null) | (null) | (null) | (null) | 9 | Using where; Using join buffer (Block Nested Loop) |
QUERY2:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|--------------------|-------|------|---------------|--------|---------|--------|------|-------------|
| 1 | PRIMARY | t1 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
| 2 | DEPENDENT SUBQUERY | t2 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
那么哪一个更好,为什么?
我读到EXPLAIN
here,但我仍然不知道哪些参数是重要的?或者哪个参数显示我这样的列需要被索引,或者我的查询需要被优化?
在上面这两个解释的结果中,所有列都是相同的,除了:select_type
和extra
。那么哪一个更好:
-
SIMPLE
,SIMPLE
PRIMARY
,DEPENDENT SUBQUERY
-
Using where
,Using where; Using join buffer (Block Nested Loop)
Using where
,Using where
编辑:下面是这两个疑问:
查询1:
SELECT t2.color FROM mytable t1
JOIN mytable t2 ON t1.related = t2.id
WHERE t1.id = '4'
QUERY2:
SELECT t1.color FROM mytable t1
WHERE exists (select 1 from mytable t2
where t1.id = t2.related
and t2.id ='4')
你能否向我们展示两个查询,假设他们能合理地解决这个问题?你的两个查询的当前运行时间是多少? –
创建一个大型测试数据集并运行查询器并查看哪个运行速度更快。 – Shadow
@TimBiegeleisen我已经将这两个查询添加到了我的问题中。 – Shafizadeh