1. 程式人生 > 其它 >oracle 大表線上刪除列操作(alter table table_name set unused )

oracle 大表線上刪除列操作(alter table table_name set unused )

慢查詢sql分析器explain

explain select * from student;
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
|  1 | SIMPLE      | student | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 9795274 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 1 warning (0.00 sec)

欄位描述:

id:選擇識別符號
select_type:表示查詢的型別。
table:輸出結果集的表
partitions:匹配的分割槽
type:表示表的連線型別
possible_keys:表示查詢時,可能使用的索引
key:表示實際使用的索引
key_len:索引欄位的長度
ref:列與索引的比較
rows:掃描出的行數(估算的行數)
filtered:按表條件過濾的行百分比
Extra:執行情況的描述和說明

id

SELECT識別符
1 id相同時,執行順序由上至下
2 如果是子查詢,id的序號會遞增,id值越大優先順序越高,越先被執行
3 id如果相同,可以認為是一組,從上往下順序執行;在所有組中,id值越大,優先順序越高,越先執行
explain select * from student left join class on class.id=student.class_id where class.id=1;
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | class   | NULL       | const | PRIMARY       | PRIMARY   | 8       | const |    1 |   100.00 | NULL  |
|  1 | SIMPLE      | student | NULL       | ref   | class_age     | class_age | 9       | const |  200 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)

select_type

select子句的型別
1 SIMPLE(簡單SELECT,不使用UNION或子查詢等)
2 PRIMARY(子查詢中最外層查詢,查詢中若包含任何複雜的子部分,最外層的select被標記為PRIMARY)
3 UNION(UNION中的第二個或後面的SELECT語句)
4 DEPENDENT UNION(UNION中的第二個或後面的SELECT語句,取決於外面的查詢)
5 UNION RESULT(UNION的結果,union語句中第二個select開始後面所有select)
6 SUBQUERY(子查詢中的第一個SELECT,結果不依賴於外部查詢)
7 DEPENDENT SUBQUERY(子查詢中的第一個SELECT,依賴於外部查詢)
8 DERIVED(派生表的SELECT, FROM子句的子查詢)
9 UNCACHEABLE SUBQUERY(一個子查詢的結果不能被快取,必須重新評估外連結的第一行)
explain select * from student;
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
|  1 | SIMPLE      | student | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 9795274 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 1 warning (0.00 sec)
explain select * from student where id in (1,2,3) union select * from student where id in(7,8,9);
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id | select_type  | table      | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra           |
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
|  1 | PRIMARY      | student    | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL |    3 |   100.00 | Using where     |
|  2 | UNION        | student    | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL |    3 |   100.00 | Using where     |
| NULL | UNION RESULT | <union1,2> | NULL       | ALL   | NULL          | NULL    | NULL    | NULL | NULL |     NULL | Using temporary |
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
3 rows in set, 1 warning (0.00 sec)
explain select * from student where class_id=(select id from class where id=1);
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key       | key_len | ref   | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------------+
|  1 | PRIMARY     | student | NULL       | ref   | class_age     | class_age | 9       | const |  200 |   100.00 | Using where |
|  2 | SUBQUERY    | class   | NULL       | const | PRIMARY       | PRIMARY   | 8       | const |    1 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

table

所訪問資料庫中表名稱
explain select * from student where id in (1,2);
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | student | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL |    2 |   100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

type

訪問型別
常用的型別有: ALL、index、range、 ref、eq_ref、const、system、NULL(從左到右,效能從差到好)
ALL:Full Table Scan, MySQL將遍歷全表以找到匹配的行
index: Full Index Scan,index與ALL區別為index型別只遍歷索引樹
range:只檢索給定範圍的行,使用一個索引來選擇行
ref: 表示上述表的連線匹配條件,即哪些列或常量被用於查詢索引列上的值
eq_ref: 類似ref,區別就在使用的索引是唯一索引,對於每個索引鍵值,表中只有一條記錄匹配,簡單來說,就是多表連線中使用primary key或者 unique key作為關聯條件
const、system: 當MySQL對查詢某部分進行優化,並轉換為一個常量時,使用這些型別訪問。如將主鍵置於where列表中,MySQL就能將該查詢轉換為一個常量,system是const型別的特例,當查詢的表只有一行的情況下,使用system
NULL: MySQL在優化過程中分解語句,執行時甚至不用訪問表或索引,例如從一個索引列裡選取最小值可以通過單獨索引查詢完成
explain select * from student;
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
|  1 | SIMPLE      | student | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 9795274 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 1 warning (0.00 sec)
explain select id from student;
+----+-------------+---------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key       | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | student | NULL       | index | NULL          | class_age | 11      | NULL | 9795274 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
explain select * from student where id <10;
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | student | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL |    9 |   100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
explain select * from student left join class on class.id=student.class_id where class.id=1;
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | class   | NULL       | const | PRIMARY       | PRIMARY   | 8       | const |    1 |   100.00 | NULL  |
|  1 | SIMPLE      | student | NULL       | ref   | class_age     | class_age | 9       | const |  200 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+-----------+---------+-------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)
explain select * from student inner join class on class.id=student.class_id where student.id in(1,2);
+----+-------------+---------+------------+--------+-------------------+---------+---------+------------------------------+------+----------+-------------+
| id | select_type | table   | partitions | type   | possible_keys     | key     | key_len | ref                          | rows | filtered | Extra       |
+----+-------------+---------+------------+--------+-------------------+---------+---------+------------------------------+------+----------+-------------+
|  1 | SIMPLE      | student | NULL       | range  | PRIMARY,class_age | PRIMARY | 8       | NULL                         |    2 |   100.00 | Using where |
|  1 | SIMPLE      | class   | NULL       | eq_ref | PRIMARY           | PRIMARY | 8       | school_info.student.class_id |    1 |   100.00 | Using where |
+----+-------------+---------+------------+--------+-------------------+---------+---------+------------------------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)
explain select * from student where id=1;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | student | NULL       | const | PRIMARY       | PRIMARY | 8       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

possible_keys

可能使用到的索引
explain select * from student where id=1;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | student | NULL       | const | PRIMARY       | PRIMARY | 8       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

Key

實際使用到的索引
explain select * from student where id=1;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | student | NULL       | const | PRIMARY       | PRIMARY | 8       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

ref

列與索引的比較,表示上述表的連線匹配條件,即哪些列或常量被用於查詢索引列上的值

rows

估算出結果集行數,表示MySQL根據表統計資訊及索引選用情況,估算的找到所需的記錄所需要讀取的行數

Extra

額外重要資訊
Using where:不用讀取表中所有資訊,僅通過索引就可以獲取所需資料,這發生在對錶的全部的請求列都是同一個索引的部分的時候,表示mysql伺服器將在儲存引擎檢索行後再進行過濾
Using temporary:表示MySQL需要使用臨時表來儲存結果集,常見於排序和分組查詢,常見 group by ; order by
Using filesort:當Query中包含 order by 操作,而且無法利用索引完成的排序操作稱為“檔案排序”
Using join buffer:改值強調了在獲取連線條件時沒有使用索引,並且需要連線緩衝區來儲存中間結果。如果出現了這個值,那應該注意,根據查詢的具體情況可能需要新增索引來改進能。
explain select * from student where id < 10 order by name;
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                       |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
|  1 | SIMPLE      | student | NULL       | range | PRIMARY       | PRIMARY | 8       | NULL |    9 |   100.00 | Using where; Using filesort |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
explain select count(*) from student where id<200 group by class_id order by class_id;
+----+-------------+---------+------------+-------+-------------------+---------+---------+------+------+----------+----------------------------------------------+
| id | select_type | table   | partitions | type  | possible_keys     | key     | key_len | ref  | rows | filtered | Extra                                        |
+----+-------------+---------+------------+-------+-------------------+---------+---------+------+------+----------+----------------------------------------------+
|  1 | SIMPLE      | student | NULL       | range | PRIMARY,class_age | PRIMARY | 8       | NULL |  199 |   100.00 | Using where; Using temporary; Using filesort |
+----+-------------+---------+------------+-------+-------------------+---------+---------+------+------+----------+----------------------------------------------+
1 row in set, 1 warning (0.00 sec)