关于MySql explain 中的ID

root@test 10:56:20>explain SELECT T0.*
->   FROM 
-> PRODUCT_0000 T0,
-> PRODUCT_0003 T3
->  WHERE T0.ID = T3.id
->    AND T0.AUC_ID =5120001280;
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+-------------+
| id | select_type | table | type   | possible_keys           | key             | key_len | ref                | rows | Extra       |
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+-------------+
|  1 | SIMPLE      | T0    | ref    | PRIMARY,idx_aa_0000_aid | idx_aa_0000_aid | 9       | const              |    1 | Using where |
|  1 | SIMPLE      | T3    | eq_ref | PRIMARY                 | PRIMARY         | 8       | test.T0.id |    1 | Using index |
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+-------------+
2 rows in set (0.00 sec)

root@test 10:56:32>explain SELECT T0.*
->   FROM 
-> PRODUCT_0000 T0,
-> PRODUCT_0003 T3
->  WHERE T0.ID = T3.id
->    AND T3.AUC_ID =5120001283;
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+--------------------------+
| id | select_type | table | type   | possible_keys           | key             | key_len | ref                | rows | Extra                    |
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+--------------------------+
|  1 | SIMPLE      | T3    | ref    | PRIMARY,idx_aa_0003_aid | idx_aa_0003_aid | 9       | const              |    1 | Using where; Using index |
|  1 | SIMPLE      | T0    | eq_ref | PRIMARY                 | PRIMARY         | 8       | test.T3.id |    1 |                          |
+----+-------------+-------+--------+-------------------------+-----------------+---------+--------------------+------+--------------------------+
2 rows in set (0.02 sec)

--根据查询条件,相同的ID显示的话,执行顺序是从上往下



root@test 11:00:44>explain SELECT T0.*
->     FROM PRODUCT_0000 T0
->    WHERE T0.ID = (SELECT T1.ID
->                     FROM PRODUCT_0001 T1
->                    WHERE T1.ID = (SELECT T2.ID
->                                     FROM PRODUCT_0002 T2
->                                    WHERE T2.AUC_ID = 5120002050));
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+--------------------------+
| id | select_type | table | type  | possible_keys   | key             | key_len | ref   | rows | Extra                    |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+--------------------------+
|  1 | PRIMARY     | T0    | const | PRIMARY         | PRIMARY         | 8       | const |    1 |                          |
|  2 | SUBQUERY    | T1    | const | PRIMARY         | PRIMARY         | 8       |       |    1 | Using index              |
|  3 | SUBQUERY    | T2    | ref   | idx_aa_0002_aid | idx_aa_0002_aid | 9       |       |    1 | Using where; Using index |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+--------------------------+
3 rows in set (0.04 sec)

--如果是子查询,ID的序号会递增,在一组循环中id越大优先级越高,越早被执行


root@test 11:02:38>explain select s2.* from (
->       SELECT T3.id 
->         FROM  PRODUCT_0003 T3
->        WHERE   T3.AUC_ID = 5120002051) s1, 
->        PRODUCT_0003 s2
->        where s1.id = s2.id;
+----+-------------+------------+--------+-----------------+-----------------+---------+-------+------+--------------------------+
| id | select_type | table      | type   | possible_keys   | key             | key_len | ref   | rows | Extra                    |
+----+-------------+------------+--------+-----------------+-----------------+---------+-------+------+--------------------------+
|  1 | PRIMARY     | <derived2> | system | NULL            | NULL            | NULL    | NULL  |    1 |                          |
|  1 | PRIMARY     | s2         | const  | PRIMARY         | PRIMARY         | 8       | const |    1 |                          |
|  2 | DERIVED     | T3         | ref    | idx_aa_0003_aid | idx_aa_0003_aid | 9       |       |    1 | Using where; Using index |
+----+-------------+------------+--------+-----------------+-----------------+---------+-------+------+--------------------------+
3 rows in set (0.00 sec)


--结论:
mysql执行计划中的ID
ID如果相同,可以认为是一组,从上往下顺序执行
在每组中,其中ID越大,优先级越高,越早执行

--
mysql的explain参考
http://hi.baidu.com/dbaeyes/blog/item/4e527fb321f5c1abd9335a85.html

------------------------------
附oracle执行计划:
--oracle--

1.
explain plan for SELECT t1.* FROM FEED_RECEIVE_01 t1, FEED_RECEIVE_02 t2
WHERE t1.id=t2.id AND t2.AUC_NUM_ID = :1;

Explained.

Elapsed: 00:00:00.01
10:27:16 FBADMIN@ feel>select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1684331209

----------------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name                      | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                           |   149 | 41720 |   121   (0)| 00:00:02 |
|   1 |  NESTED LOOPS                |                           |   149 | 41720 |   121   (0)| 00:00:02 |
|   2 |   TABLE ACCESS BY INDEX ROWID| FEED_RECEIVE_02           |   149 |  1788 |    61   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | IDX_FEED_RECEIVE_02_AUCID |   149 |       |     2   (0)| 00:00:01 |
|   4 |   TABLE ACCESS BY INDEX ROWID| FEED_RECEIVE_01           |     1 |   268 |     1   (0)| 00:00:01 |
|*  5 |    INDEX UNIQUE SCAN         | PK_FEED_RECEIVE_01_ID     |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------------


2.
explain plan for SELECT /*+ ordered use_nl(t, ff) */
FF.*
FROM (SELECT RID
FROM (SELECT RID, ROWNUM AS RN
FROM (SELECT ROWID RID
FROM FEED_RECEIVE_03
WHERE RATED_UID = :1
AND (SUSPENDED = 0 OR SUSPENDED = 3 OR
SUSPENDED = 7 OR SUSPENDED = 5 OR
SUSPENDED = 6)
AND RATER_TYPE = 0
ORDER BY FEEDBACKDATE DESC)
WHERE ROWNUM <= :2) T
WHERE RN >= :3) T,
FEED_RECEIVE_03 FF
WHERE T.RID = FF.ROWID;


select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2477898939

-------------------------------------------------------------------------------------------------------------
| Id  | Operation                       | Name                      | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |                           |   366 |   104K|   370   (0)| 00:00:05 |
|   1 |  NESTED LOOPS                   |                           |   366 |   104K|   370   (0)| 00:00:05 |
|*  2 |   VIEW                          |                           |   366 |  9150 |     4   (0)| 00:00:01 |
|*  3 |    COUNT STOPKEY                |                           |       |       |            |          |
|   4 |     VIEW                        |                           |   366 |  4392 |     4   (0)| 00:00:01 |
|*  5 |      INDEX RANGE SCAN DESCENDING| IDX_FEED_RECEIVE_03_RATED |   366 | 10614 |     4   (0)| 00:00:01 |
|   6 |   TABLE ACCESS BY USER ROWID    | FEED_RECEIVE_03           |     1 |   268 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter("RN">=TO_NUMBER(:3))
3 - filter(ROWNUM<=TO_NUMBER(:2))
5 - access("RATED_UID"=TO_NUMBER(:1) AND "RATER_TYPE"=0)
filter("RATER_TYPE"=0 AND ("SUSPENDED"=0 OR "SUSPENDED"=3 OR "SUSPENDED"=5 OR "SUSPENDED"=6
OR "SUSPENDED"=7))

--oracle的执行计划中的ID是顺序增大的,相同的缩进的话从上往下,每组缩进中执行先后根据缩进程序来判断,缩进越多越早执行。

你可能感兴趣的:(关于MySql explain 中的ID)