EXPLAIN SELECT vrl.list_id,vrl.ontology_id,vrl.position,l.name AS list_name, vrlih.position AS previous_position, vrl.moved_dateFROM `vote-ranked-listory` vrlINNER JOIN lists l ON l.list_id = vrl.list_idINNER JOIN `vote-ranked-list-item-history` vrlih ON vrl.list_id = vrlih.list_id AND vrl.ontology_id=vrlih.ontology_id AND vrlih.type='PREVIOUS_POSITION'INNER JOIN list_burial_state lbs ON lbs.list_id = vrl.list_id AND lbs.burial_score < 0.5WHERE vrl.position <= 15 AND l.status='ACTIVE' AND l.is_public=1 AND vrl.ontology_id < 1000000000AND (SELECT list_id FROM list_tag WHERE list_id=l.list_id AND tag_id=43) IS NULLAND (SELECT list_id FROM list_tag WHERE list_id=l.list_id AND tag_id=55) IS NULLAND (SELECT list_id FROM list_tag WHERE list_id=l.list_id AND tag_id=246403) IS NOT NULLORDER BY vrl.moved_date DESC LIMIT 200;
解释显示:
+----+--------------------+----------+--------+-----------------------------------------------------+--------------+---------+-------------------------------------------------+------+--------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+--------------------+----------+--------+-----------------------------------------------------+--------------+---------+-------------------------------------------------+------+--------------------------+| 1 | PRIMARY | vrl | index | PRIMARY | moved_date | 8 | NULL | 200 | Using where || 1 | PRIMARY | l | eq_ref | PRIMARY,status,ispublic,idx_lookup,is_public_status | PRIMARY | 4 | ranker.vrl.list_id | 1 | Using where || 1 | PRIMARY | vrlih | eq_ref | PRIMARY | PRIMARY | 9 | ranker.vrl.list_id,ranker.vrl.ontology_id,const | 1 | Using where || 1 | PRIMARY | lbs | eq_ref | PRIMARY,idx_list_burial_state,burial_score | PRIMARY | 4 | ranker.vrl.list_id | 1 | Using where || 4 | DEPENDENT SUBQUERY | list_tag | ref | list_tag_key,list_id,tag_id | list_tag_key | 9 | ranker.l.list_id,const | 1 | Using where; Using index || 3 | DEPENDENT SUBQUERY | list_tag | ref | list_tag_key,list_id,tag_id | list_tag_key | 9 | ranker.l.list_id,const | 1 | Using where; Using index || 2 | DEPENDENT SUBQUERY | list_tag | ref | list_tag_key,list_id,tag_id | list_tag_key | 9 | ranker.l.list_id,const | 1 | Using where; Using index |+----+--------------------+----------+--------+-----------------------------------------------------+--------------+---------+-------------------------------------------------+------+--------------------------+
与JOIN相同的查询是:
EXPLAIN SELECT vrl.list_id,vrl.ontology_id,vrl.position,l.name AS list_name, vrlih.position AS previous_position, vrl.moved_dateFROM `vote-ranked-listory` vrlINNER JOIN lists l ON l.list_id = vrl.list_idINNER JOIN `vote-ranked-list-item-history` vrlih ON vrl.list_id = vrlih.list_id AND vrl.ontology_id=vrlih.ontology_id AND vrlih.type='PREVIOUS_POSITION'INNER JOIN list_burial_state lbs ON lbs.list_id = vrl.list_id AND lbs.burial_score < 0.5LEFT JOIN list_tag lt1 ON lt1.list_id = vrl.list_id AND lt1.tag_id = 43LEFT JOIN list_tag lt2 ON lt2.list_id = vrl.list_id AND lt2.tag_id = 55INNER JOIN list_tag lt3 ON lt3.list_id = vrl.list_id AND lt3.tag_id = 246403WHERE vrl.position <= 15 AND l.status='ACTIVE' AND l.is_public=1 AND vrl.ontology_id < 1000000000AND lt1.list_id IS NULL AND lt2.tag_id IS NULLORDER BY vrl.moved_date DESC LIMIT 200;
输出是:
+----+-------------+-------+--------+-----------------------------------------------------+--------------+---------+---------------------------------------------+------+----------------------------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+--------+-----------------------------------------------------+--------------+---------+---------------------------------------------+------+----------------------------------------------+| 1 | SIMPLE | lt3 | ref | list_tag_key,list_id,tag_id | tag_id | 5 | const | 2386 | Using where; Using temporary; Using filesort || 1 | SIMPLE | l | eq_ref | PRIMARY,status,ispublic,idx_lookup,is_public_status | PRIMARY | 4 | ranker.lt3.list_id | 1 | Using where || 1 | SIMPLE | vrlih | ref | PRIMARY | PRIMARY | 4 | ranker.lt3.list_id | 103 | Using where || 1 | SIMPLE | vrl | ref | PRIMARY | PRIMARY | 8 | ranker.lt3.list_id,ranker.vrlih.ontology_id | 65 | Using where || 1 | SIMPLE | lt1 | ref | list_tag_key,list_id,tag_id | list_tag_key | 9 | ranker.lt3.list_id,const | 1 | Using where; Using index; Not exists || 1 | SIMPLE | lbs | eq_ref | PRIMARY,idx_list_burial_state,burial_score | PRIMARY | 4 | ranker.vrl.list_id | 1 | Using where || 1 | SIMPLE | lt2 | ref | list_tag_key,list_id,tag_id | list_tag_key | 9 | ranker.lt3.list_id,const | 1 | Using where; Using index |+----+-------------+-------+--------+-----------------------------------------------------+--------------+---------+---------------------------------------------+------+----------------------------------------------+
rows列的比较说明了差异,并且使用JOIN的查询使用Using temporary; Using filesort。
+------+----------+----------------+----------+| sID | cName | major | decision |+------+----------+----------------+----------+| 123 | Stanford | CS | Y || 123 | Stanford | EE | N || 123 | Berkeley | CS | Y || 123 | Cornell | EE | Y || 234 | Berkeley | biology | N || 345 | MIT | bioengineering | Y || 345 | Cornell | bioengineering | N || 345 | Cornell | CS | Y || 345 | Cornell | EE | N || 678 | Stanford | history | Y || 987 | Stanford | CS | Y || 987 | Berkeley | CS | Y || 876 | Stanford | CS | N || 876 | MIT | biology | Y || 876 | MIT | marine biology | N || 765 | Stanford | history | Y || 765 | Cornell | history | N || 765 | Cornell | psychology | Y || 543 | MIT | CS | N |+------+----------+----------------+----------+
让我们试着找到申请CS专业的学生的GPA分数(无论大学如何)
使用子查询:
select GPA from Student where sID in (select sID from Apply where major = 'CS');
+------+| GPA |+------+| 3.9 || 3.5 || 3.7 || 3.9 || 3.4 |+------+
此结果集的平均值为:
select avg(GPA) from Student where sID in (select sID from Apply where major = 'CS');
+--------------------+| avg(GPA) |+--------------------+| 3.6800000000000006 |+--------------------+
使用连接:
select GPA from Student, Apply where Student.sID = Apply.sID and Apply.major = 'CS';
+------+| GPA |+------+| 3.9 || 3.9 || 3.5 || 3.7 || 3.7 || 3.9 || 3.4 |+------+
此结果集的平均值:
select avg(GPA) from Student, Apply where Student.sID = Apply.sID and Apply.major = 'CS';
+-------------------+| avg(GPA) |+-------------------+| 3.714285714285714 |+-------------------+
select id,name from table1 ajoin table2 b on a.name=b.namewhere id='123'
Try,
select id,name from table1 ajoin table2 b on a.name=b.name and a.id='123'