dkubb (owner)

Revisions

gist: 126364 Download_button fork
public
Public Clone URL: git://gist.github.com/126364.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    SELECT humans.id
         , humans.first_name
         , humans.middle_name
         , humans.last_name
      FROM humans
INNER JOIN ownerships ON ownerships.human_id = humans.id
INNER JOIN pets ON ownerships.pet_id = pets.id
     WHERE pets.name = 'Bo'
  GROUP BY humans.id
         , humans.first_name
         , humans.middle_name
         , humans.last_name
;
 
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+---------------------------------+
| 1 | SIMPLE | humans | ALL | PRIMARY | NULL | NULL | NULL | 9 | Using temporary; Using filesort |
| 1 | SIMPLE | ownerships | ref | PRIMARY | PRIMARY | 4 | dm_pets.humans.id | 1 | Using index |
| 1 | SIMPLE | pets | eq_ref | PRIMARY | PRIMARY | 4 | dm_pets.ownerships.pet_id | 1 | Using where |
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+---------------------------------+
 
 
SELECT id
     , first_name
     , middle_name
     , last_name
  FROM humans
 WHERE id IN(SELECT DISTINCT human_id FROM ownerships WHERE pet_id IN(SELECT id FROM pets WHERE name = 'Bo'))
;
 
+----+--------------------+------------+-----------------+---------------+---------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+-----------------+---------------+---------+---------+------+------+--------------------------+
| 1 | PRIMARY | humans | ALL | NULL | NULL | NULL | NULL | 9 | Using where |
| 2 | DEPENDENT SUBQUERY | ownerships | index_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using index; Using where |
| 3 | DEPENDENT SUBQUERY | pets | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |
+----+--------------------+------------+-----------------+---------------+---------+---------+------+------+--------------------------+
 
 
    SELECT DISTINCT humans.id
         , humans.first_name
         , humans.middle_name
         , humans.last_name
      FROM humans
INNER JOIN ownerships ON ownerships.human_id = humans.id
INNER JOIN pets ON ownerships.pet_id = pets.id
     WHERE pets.name = 'Bo'
;
 
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+-----------------------+
| 1 | SIMPLE | humans | ALL | PRIMARY | NULL | NULL | NULL | 9 | Using temporary |
| 1 | SIMPLE | ownerships | ref | PRIMARY | PRIMARY | 4 | dm_pets.humans.id | 1 | Using index; Distinct |
| 1 | SIMPLE | pets | eq_ref | PRIMARY | PRIMARY | 4 | dm_pets.ownerships.pet_id | 1 | Using where; Distinct |
+----+-------------+------------+--------+---------------+---------+---------+---------------------------+------+-----------------------+