+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ 1.id |
mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b; +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | | system | NULL | NULL | NULL | NULL | 1 | | | 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ |
mysql> explain select * from t3 where id=3952602; +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ |
mysql> explain select * from (select * from t3 where id=3952602) a ; +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ |
mysql> explain select * from t3 where id=3952602 union all select * from t3 ; +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | | | 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | | |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ |
mysql> explain select * from t3 where id in (select id from t3 where id=3952602 union all select id from t3) ; +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ | 1 | PRIMARY | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | Using where | | 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index | | 3 | DEPENDENT UNION | t3 | eq_ref | PRIMARY,idx_t3_id | PRIMARY | 4 | func | 1 | Using where; Using index | |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------------+------------+--------+-------------------+---------+---------+-------+------+--------------------------+ |
mysql> explain select * from t3 where id=3952602 union all select * from t3 ; +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | | | 2 | UNION | t3 | ALL | NULL | NULL | NULL | NULL | 1000 | | |NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------+------------+-------+-------------------+---------+---------+-------+------+-------+ |
mysql> explain select * from t3 where id = (select id from t3 where id=3952602 ) ; +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+ | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | | | 2 | SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | Using index | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+ |
mysql> explain select id from t3 where id in (select id from t3 where id=3952602 ) ; +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+ | 1 | PRIMARY | t3 | index | NULL | PRIMARY | 4 | NULL | 1000 | Using where; Using index | | 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index | +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+ |
mysql> explain select * from (select * from t3 where id=3952602) a ; +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ |
mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b; +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | | system | NULL | NULL | NULL | NULL | 1 | | | 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ |
mysql> explain select * from (select * from t3 where id=3952602) a ; +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ | 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | | +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+ |
SELECT * from tbl_name WHERE primary_key=1; SELECT * from tbl_name WHERE primary_key_part1=1和 primary_key_part2=2; |
mysql> explain select * from t3 where id=3952602; +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | | +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+ |
SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.column AND ref_table.key_column_part2=1; |
mysql> create unique index idx_t3_id on t3(id) ; Query OK, 1000 rows affected (0.03 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> explain select * from t3,t4 where t3.id=t4.accountid; +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+ | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | | | 1 | SIMPLE | t3 | eq_ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | | +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+ |
SELECT * FROM ref_table WHERE key_column=expr; SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.column AND ref_table.key_column_part2=1; |
mysql> drop index idx_t3_id on t3; Query OK, 1000 rows affected (0.03 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> create index idx_t3_id on t3(id) ; Query OK, 1000 rows affected (0.04 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> explain select * from t3,t4 where t3.id=t4.accountid; +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | | | 1 | SIMPLE | t3 | ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | | +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ 2 rows in set (0.00 sec) |
SELECT * FROM ref_table WHERE key_column=expr OR key_column IS NULL; |
mysql> explain select * from t4 where id=3952602 or accountid=31754306 ; +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ | 1 | SIMPLE | t4 | index_merge | idx_t4_id,idx_t4_accountid | idx_t4_id,idx_t4_accountid | 4,4 | NULL | 2 | Using union(idx_t4_id,idx_t4_accountid); Using where | +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ 1 row in set (0.00 sec) |
value IN (SELECT primary_key FROM single_table WHERE some_expr) |
value IN (SELECT key_column FROM single_table WHERE some_expr) |
mysql> explain select * from t3 where id=3952602 or id=3952603 ; +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+ | 1 | SIMPLE | t3 | range | PRIMARY,idx_t3_id | idx_t3_id | 4 | NULL | 2 | Using where | +----+-------------+-------+-------+-------------------+-----------+---------+------+------+-------------+ 1 row in set (0.02 sec) (10).index |
欢迎光临 MariaDB社区 (http://123.56.88.72/) | Powered by Discuz! X3.2 |