I have two separated queries which have identical outputs. Now I'm trying to understand which one is better?
Query1:
| 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 |
So which one is better and why?
I read about EXPLAIN
here, But still I don't know which parameter is important? Or which parameter shows me such a column needs to be index, or my query needs to be optimized?
In those two explain's results above, all columns are identical except: select_type
and extra
. So which one is better:
SIMPLE
,SIMPLE
PRIMARY
,DEPENDENT SUBQUERY
Using where
,Using where; Using join buffer (Block Nested Loop)
Using where
,Using where
EDIT: Here is those two queries:
Query1:
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')