我有两个具有相同输出的独立查询.现在我想了解哪一个更好?
查询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之外,所有列都是相同的.那么哪一个更好:
>
>简单,简单
>主要的,相关的子信息
>
>使用where,使用where;使用连接缓冲区(块嵌套循环)
>使用where,使用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')