1.unix時間戳的使用css
unix_timesamp、from_unixtime 函數 和 datatime_format函數。mysql
// 從datetime 類型取作整形 unixtime時間戳;sql
select unix_timestamp( datetime ) from examplestables;併發
// 從整形轉換成datetime類型,時間格式函數
select from_unixtime( datetime ) from exampletables;spa
// 對unix 時間戳自定義時間段分組統計 distinct 不重複 dateformat 時間串自定義格式輸出命令行
select count(distinct(roleid)), dateformat( from_unixtime(datetime),"%Y-%m-%d" ) days from tmptable group by days;unix
// 表合併 重複鍵值的時候只更新不報錯rest
insert into tmptable2 select * from tmptable on duplicate key update set A=a;日誌
// 查詢結果字符串拼接
select concat("s1_", strName ) from tmptable;
2. sleep鏈接超時時間 --避免過多的sleep鏈接佔用資源
set global wait_timeout=305;
3.建立索引 表不加主鍵,不加索引的查詢很是慢
create index idx_datarow on exampletables(datarow);
4.myisam 引擎 和 innodb引擎
myisam在 update insert 使用表級鎖
innodb 在update ,insert時使用 的是按照索引和鍵值的行級鎖,併發性更高
innodb 在建立的錶行屬性 爲fixed的時候,blob類型字段過長,過多會報錯。
Row size too large. The maximum row size for the used table type, not counting BLOBs, is 1982. You have to change some columns to TEXT or BLOBs。
須要改爲dynamic。動態長度的行數據。
5.查詢某列數據重複的數據值 exampletables 表中,exrow行重複的列
select * from exampletables where exrow in ( select exrow from exampletables group by exrow having count(exrow) > 1 );
6.修改表的引擎,修改表的列類型
alter table exampletable ENGINE=InnoDB;
alter table exampletable modify column exrowname varchar(64);
7.查看正在執行的sql命令,show processlist
select * from information_schema.processlist where command <> 'Sleep';
8.查看慢查詢日誌,有助於捕捉耗時查詢,異常查詢
show variables like 'slow';
log-slow-queries=/data/mysqldata/slowquery。log
long_query_time=2
9.GTID(Global Transaction ID)是對於一個已提交事務的編號,而且是一個全局惟一的編號。
GTID其實是由UUID+TID組成的。其中UUID是一個MySQL實例的惟一標識。TID表明了該實例上已經提交的事務數量,而且隨着事務提交單調遞增。下面是一個GTID的具體形式
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
9.mysql 備份--set-gtid-purged=OFF 是忽略 變量中記錄的是本機上已經執行過,可是已經被purge binary logs to
命令清理的gtid_set
。
mysqldump -uexuser -p -h127.0.0.1 --databases --no-data dbname --set-gtid-purged=OFF >/data/bk.sql
附上備份和恢復腳本:命令行執行
mysqldump -uroot -p -h 11.111.111.111 -P3306 --default-character-set=utf8 --set-gtid-purged=OFF --password --databases test > /home/sqlbackup
mysql -h11.111.111.111 -uroot -P3306 -p --default-character-set=utf8 < /home/sqlbackup
注意,上述腳本中,備份的部分要加入--set-gtid-purged=OFF參數,防止在備份出的sql腳本中生成 SET @@global.gtid_purged 語句:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF.
官方文檔關於set-gtid-purged是這樣寫的:
This option enables control over global transaction ID (GTID) information written to the dump file, by indicating whether to add a SET @@global.gtid_purged statement to the output.
10.查詢目前的 執行非sleep命令
select * from information_schema.processlist where command <>'Sleep';