I have a couple of questions regarding MySQL explain.
- In the first step of the evaluation, it utilizes a REF, for join type. However, upon my research on ref it states the following:
All rows with matching index values are read from this table for each combination of rows from the previous tables.
What is this Previous table? How can there be a previous table if its the initial step? - I created an index on S.E, why does it state
Using where?
at the Extra column instead ofUsing Index
? And it specifically states it uses the index by looking at theKEY column : SE
- In terms of the order of operations, does MySQL do the process everything in this order? S.E = 5 using index, R.Rid = S.Rid using the records from the previous step, R.B = 5 using the records from the previous step?
INDEX on S.E
mysql> Create index SE on S(E);
Query OK, 0 rows affected (1.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT COUNT(R.RID) FROM R, S WHERE R.RID=S.RID AND R.B=5 AND S.E=5;
+----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| 1 | SIMPLE | S | ref | RID,SE | SE | 5 | const | 6 | Using where |
| 1 | SIMPLE | R | eq_ref | PRIMARY | PRIMARY | 4 | project2.S.RID | 1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
DATASET
Table R has 100,000 rows and the following fields:
- RID (primary key): an integer from 1 to 100,000
- B: a random uniformly distributed integer from 1 to 10
- C: a random uniformly distributed integer from 1 to 1,000
Table S has 500,000 rows and the following fields:
- SID (primary key): an integer from 1 to 500,000
- RID (foreign key to R): a random uniformly distributed integer from 1 to 100,000
- D: a random uniformly distributed integer from 1 to 100
- E: a random uniformly distributed integer from 1 to 100,000
Query
SELECT COUNT(R.RID) FROM R, S WHERE R.RID=S.RID AND R.B=5 AND S.E=5;