1. undo log
undo log分離到獨立的表空間,並放到單獨的文件目錄下;這給我們部署不同IO類型的文件位置帶來便利,對於併發寫入型負載,我們可以把undo文件部署到單獨的高速存儲設備上。
1.1. 設置
vim /etc/my.cnf
[mysqld]
innodb_undo_directory=/data/undolog
innodb_undo_log=128
innodb_undo_tablespaces=16
1.2. 說明
innodb_undo_directory 用來指定undo日誌文件的路徑。如果需要轉移undo日誌文件的位置,只需要把undo文件拷貝過去並修改該配置項即可(需要修改新目錄的用戶爲mysql)。
innodb_undo_log 用來表示回滾段的個數(之前版本命名爲innodb_rollback_segments),該變量可以動態調整,默認爲128,但是物理回滾段不會減少,只會控制用到的回滾段的個數。
innodb_undo_tablespaces 用於設定創建的undo表空間的個數。默認值爲0,即不設置獨立的undo表空間,記錄到ibdata中。此處設定爲16,初始化中會自動創建命名爲undo001-undo016的16個文件,每個文件默認10M。在數據庫初始化後,這個配置項就無法再更改,否則會導致mysql啓動失敗。
2. GTID sql多進程
2.1. 確認GTID正常
mysql> show slave status\G;
*************************** 1. row***************************
Slave_IO_State: System lock
Master_Host: 10.90.5.143
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 237106479
Relay_Log_File: relay-log.000003
Relay_Log_Pos: 237090691
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 237092346
Relay_Log_Space: 237107184
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 4
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:eff879d0-3d3f-11e4-9797-080027019055
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for theslave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: eff879d0-3d3f-11e4-9797-080027019055:1-550
Executed_Gtid_Set: eff879d0-3d3f-11e4-9797-080027019055:1-522
Auto_Position: 1
1 row in set (1.10 sec)
2.2. 設置開啓多線程
vim /etc/my.cnf
[mysqld]
slave_parallel_workers=10
#默認爲0,即不開啓多線程,最大爲1024。
在mysql中進行查看,
mysql> show variables like"slave_parallel_workers";
+-------------------------------------+----------+
| Variable_name | Value |
+-------------------------------------+----------+
| slave_parallel_workers |10 |
+-------------------------------------+-----------+
3. 優化器
1.子查詢優化,select .. from… order by … limit ..;
2.使用Multi-Range Read。順序訪問代替隨機訪問。
3.使用ICP,whereindex優化。
4.explain可以解釋delete,insert,replace,update。
5.BKA算法。
6.優化器追蹤功能。
3.1. select子查詢
---mysql-5.5.38
select * from employees where emp_no in(select emp_no from titles where title='Senior Engineer');
……
| 499996 | 1953-03-07 | Zito | Baaz | M | 1990-09-27 |
| 499997 | 1961-08-03 | Berhard | Lenart | M | 1986-04-21 |
+-----------+-----------------+------------------------+---------------------------+--------------+----------------+
97750 rows in set(0.69 sec)
---mysql-5.6.16
select * from employees where emp_no in(select emp_no from titles where title='Senior Engineer');
……
| 499996 | 1953-03-07 | Zito | Baaz | M | 1990-09-27 |
| 499997 | 1961-08-03 | Berhard | Lenart | M | 1986-04-21 |
+-----------+-----------------+------------------------+----------------------------+-------------+----------------+
97750 rows in set(0.52 sec)
---mysql-5.5.38
select * from salaries where emp_no in (selectemp_no from titles where title='Senior Engineer');
……
| 499997 | 82750 | 2000-08-26 | 2001-08-26 |
| 499997 | 83441 | 2001-08-26 | 9999-01-01 |
+-----------+-------------+----------------+-----------------+
1137450 rows in set (6.21sec)
---mysql-5.6.16
select * from salaries where emp_no in(select emp_no from titles where title='Senior Engineer');
…….
| 499997 | 82750 | 2000-08-26 | 2001-08-26 |
| 499997 | 83441 | 2001-08-26 | 9999-01-01 |
+-----------+-------------+----------------+-----------------+
1137450 rows in set (2.12sec)
mysql-5.6 對於同一條命令總是開啓新查詢。
3.2. ICP MRR
3.2.1. 新增項
---mysql-5.5.38
mysql> show variables like"optimizer_switch";
+----------------------------------------+-------------------------------------------------------------------------+
| Variable_name | Value |
+----------------------------------------+-------------------------------------------------------------------------+
| optimizer_switch |index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on|
+----------------------------------------+--------------------------------------------------------------------------+
---mysql-5.6.16
mysql> show variables like"optimizer_switch";
+-----------------------------+-----------------------------------------------------------------------------------+
| Variable_name |Value |
+-----------------------------+------------------------------------------------------------------------------------+
| optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,subquery_materialization_cost_based=on,use_index_extensions=on |
+-----------------------------+------------------------------------------------------------------------------------+
3.2.2. 驗證
---5.5.38
mysql> explain select log_time fromperson where nick_name = 'Lucy';
+----+-----------------+-----------+--------+--------------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+---------------+---------+-------+------+-------------+
| 1| SIMPLE | person | ref | idx_nick_name | idx_nick_name | 122 | const | 1 | Using where|
+----+-----------------+-----------+--------+--------------------+---------------+---------+-------+------+-------------+
mysql> explain select log_time fromperson where nick_name = 'Lucy';
+----+-------------+--------+------+---------------+---------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+---------------+---------+-------+------+-----------------------+
| 1 |SIMPLE | person | ref | idx_nick_name | idx_nick_name | 122 | const | 1 | Using indexcondition |
+----+-------------+--------+------+---------------+---------------+---------+-------+------+-----------------------+
explain中顯示爲using index condition,則證明優化器使用indexcondition pushdown。
3.3. 執行計劃
在mysql-5.6中對explain解釋器新增對insert,delete,update,replace語句的支持。
---mysql-5.5.38
mysql> explain delete from person;
ERROR 1064 (42000): You have an error in your SQL syntax; check themanual that corresponds to your MySQL server version for the right syntax touse near 'delete from person' at line 1
---mysql-5.6.16
mysql> explain delete from person;
+----+----------------+---------+---------+--------------------+-----+-----------+-------+--------+-------------------+
| id | select_type | table | type | possible_keys | key| key_len | ref | rows | Extra |
+----+----------------+---------+---------+--------------------+-----+------------+-------+--------+-------------------+
| 1| SIMPLE | NULL | NULL | NULL |NULL | NULL | NULL | 5 | Deleting allrows |
+----+----------------+---------+---------+--------------------+-----+-------------+------+------+---------------------+
mysql> explain update person setnick_name='Joe' where id=5;
+----+-----------------+-----------+----------+-----------------+------+-----------+---------+--------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-----------------+-----------+----------+------------------+-----+-----------+---------+--------+------------------+
| 1| SIMPLE | person | range |PRIMARY | PRIMARY | 4 | const | 1 | Using where|
+----+-----------------+-----------+----------+------------------+-----+-----------+---------+---------+-----------------+
4. timestamp
4.1. 字段屬性
在MySQL5.5裏(或更早的MySQL5.1)timestamp類型一個表裏只允許一列字段擁有自動插入時間和自動更新時間、或只允許一列字段有自動插入時間,另一列字段不能有自動更新時間。
---mysql-5.5.38
mysql> create table time1(id int,t timestamp default current_timestamp on updatecurrent_timestamp,t2 timestamp default current_timestamp on updatecurrent_timestamp);
ERROR 1293 (HY000): Incorrect table definition; there can be onlyone TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATEclause
mysql> create table time(id int,ttimestamp,t2 timestamp);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into time(id)values(1),(2);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from time;
+---------+-----------------------------+-----------------------------+
| id | t | t2 |
+---------+-----------------------------+-----------------------------+
| 1| 2014-09-23 16:49:09 | 0000-00-00 00:00:00 |
| 2 | 2014-09-23 16:49:09 | 0000-00-00 00:00:00 |
+---------+-----------------------------+-----------------------------+
---mysql-5.6.16
MySQL5.6中的timestamp類型一個表裏可以有多列字段擁有自動插入時間和自動更新時間。
mysql> create table time(id int,t timestamp default current_timestamp on updatecurrent_timestamp,t2 timestamp default current_timestamp on updatecurrent_timestamp);
Query OK, 0 rows affected (0.10 sec)
mysql> insert into time(id)values(1),(2);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from time;
+---------+-----------------------------+-----------------------------+
| id | t | t2 |
+---------+-----------------------------+-----------------------------+
| 1 | 2014-09-23 16:52:16 | 2014-09-23 16:52:16 |
| 2 | 2014-09-23 16:52:16 | 2014-09-23 16:52:16 |
+---------+-----------------------------+-----------------------------+
2 rows in set (0.00 sec)
4.2. 數據精確位
mysql> create table time2(id int,t1timestamp(6));
Query OK, 0 rows affected (0.07 sec)
mysql> insert into time2(id) values(1);
Query OK, 1 row affected (0.02 sec)
mysql> select * from time2;
+---------+---------------------------------------+
| id | t1 |
+---------+----------------------------------------+
| 1 | 2014-09-23 16:57:03.886880 |
+---------+----------------------------------------+
4.3. 初始化數據警告處理
mysql-5.6啓動報警告:
[Warning] TIMESTAMP with implicit DEFAULTvalue is deprecated.
Please use --explicit_defaults_for_timestampserver option
(seedocumentation for more details).
在啓動文件中添加:
[mysqld]
explicit_defaults_for_timestamp=true
5. online DDL
1.LOCK語句的使用是在alter table中以逗號作爲分隔的。
2.在線DDL操作可能被推遲,直到任何訪問該表的現有事務被提交或回滾。
3.在線DDL操作可能要在其他任何訪問該表的併發事務被提交或回滾之後再完成。
4.當一條DDL操作在運行時,只要altertable語句使用了LOCK=NONE或者LOCK=SHARED,併發查詢是相對簡單的。
5.注意是否打開或關閉自動提交。如果它是關閉的,執行DDL操作之前要注意其他會話中結束事務(甚至只是查詢)的操作。
6.對於LOCK=SHARED的情況,混合查詢以及DML操作的併發事務遇到死鎖錯誤時,在DDL完成後必須重新啓動。
7.對於LOCK=NONE的情況,併發事務可以自由組合查詢和DML。DDL操作會等待併發事務的提交或者回滾。
8.對於LOCK=NONE的情況,併發事務可以自由混合查詢及DML,但是這些事務會等到DDL操作完成後才能去訪問表。
注:以下示例來自於mysql-5.6官方文檔section—14.11.5。