RPM包搜索網(wǎng)站:http://rpmfind.net/linux/RPM/index.html、http://rpm.pbone.net/趾疚、https://pkgs.org/
Percona官網(wǎng):https://www.percona.com/
緩存優(yōu)化
1缨历、應(yīng)用的持久層(模型層)開啟緩存
2以蕴、使用Redis緩存
慢查詢分析
慢查詢:未使用索引的查詢 和 超過指定時間的查詢
慢查詢相關(guān)參數(shù)
slow_query_log:是否開啟慢查詢?nèi)罩?br>
slow_query_log_file:慢查詢?nèi)罩韭窂剑仨毚嬖谇襪ysql用戶有寫權(quán)限
long_query_time:慢查詢閾值時間
log_queries_not_using_indexes:未使用索引的查詢是否記入慢查詢?nèi)罩?/p>
查看參數(shù):show variables like 'slow_query_log';
查看全局參數(shù):show global variables like 'slow_query_log';
設(shè)置參數(shù):set slow_query_log=on;
設(shè)置全局參數(shù):set global slow_query_log=on;
慢日志格式
# Time: 執(zhí)行時間
# User@Host: 用戶信息
# Query_time: 查詢耗時 Lock_time: 鎖耗時 Rows_sent: 發(fā)送行數(shù) Rows_examined: 掃描行數(shù)
SET timestamp=執(zhí)行時間戳;
SQL語句
慢查詢分析工具mysqldumpslow
耗時統(tǒng)計前三:mysqldumpslow -t 3 /var/lib/mysql/slow.log
慢查詢分析工具pt-query-digest
pt-query-digest 包含在 Percona 公司提供的工具套裝 percona-toolkit 中戈二,依賴Perl
1舒裤、安裝Perl
yum install -y perl-DBI
yum install -y perl-DBD-MySQL
yum install -y perl-Time-HiRes
yum install -y perl-IO-Socket-SSL
yum install -y perl-Digest-MD5
yum install -y perl-TermReadKey
2、安裝percona-toolkit
到percona官網(wǎng)上找到 percona-toolkit
下載對應(yīng)操作系統(tǒng)的percona-toolkit.rpm
安裝:rpm -ivh percona-toolkit.rpm
前三耗時查詢:pt-query-digest --limit 3 /var/lib/mysql/slow.log
執(zhí)行計劃分析
explain 查詢語句; 得到該查詢語句的執(zhí)行計劃
1觉吭、table:涉及的表
2、type:const(唯一索引查找)仆邓、eq_rep(唯一索引范圍查找)鲜滩、ref(基于索引的連接查詢)、range(基于索引的范圍查找)节值、index(索引掃描)徙硅、ALL(表掃描),性能由高到低
3搞疗、possible_key:可用索引
4嗓蘑、key:實際使用索引
5、key_len:索引長度匿乃,越短越好
6桩皿、ref:使用索引具體的列
7、row:必須掃描的行數(shù)
查詢優(yōu)化
MySql的索引由B+樹實現(xiàn)
1幢炸、需要建立索引的字段
where從句泄隔、group by從句、order by從句宛徊、on從句中的字段
2佛嬉、索引字段數(shù)量不能太多,索引字段長度越小越好
3闸天、select max(age) from people;
age不是索引的話暖呕,需要掃描整張表,應(yīng)該給age建立索引
4苞氮、用連接查詢 代替 嵌套查詢(需要建立臨時表)
5湾揽、order by title limit 500,10
order by的字段必須是 索引字段,否則需要掃描整張表葱淳;
即使是索引字段排序钝腺,limit 500,10,前500條數(shù)據(jù)也需要索引掃描赞厕,可以改成 where title>前一頁的最后一個title order by title limit 0,10
6艳狐、多字段索引,離散度高(重復(fù)少)的字段放在前面
7皿桑、聯(lián)合索引最左匹配原則:key index_abc(a,b,c)毫目,查詢時提供最左字段才能用到該聯(lián)合索引蔬啡,例如(a>0);只提供右側(cè)字段則不會用到該聯(lián)合索引镀虐,例如(b>0)箱蟆、(b>0,c>0)
數(shù)據(jù)類型優(yōu)化
1、text類型放到附加表中刮便,需要的時候才查詢
2空猜、用int存儲時間,用unix_timestamp()恨旱、from_unixtime()來轉(zhuǎn)換
3辈毯、用bigint存IP,用inet_aton()搜贤、inet_ntoa()來轉(zhuǎn)換
表設(shè)計優(yōu)化
1谆沃、范式設(shè)計
2、增加冗余字段仪芒,來減少連接查詢
3唁影、垂直拆分
系統(tǒng)配置優(yōu)化
/etc/security/limits.conf
soft nofile 65535 # 最大打開文件數(shù),默認(rèn)為1024
hard nofile 65535
查看限制:ulimit -a
MySQL配置優(yōu)化
/etc/my.cnf
Innodb_buffer_pool_size=內(nèi)存的75% # 如果系統(tǒng)中只有Innodb表
Innodb_buffer_pool_instances=4 # 緩沖池個數(shù)
Innodb_flush_log_at_trx_commit=2 # 每2次提交掂名,從緩沖區(qū)刷新到磁盤据沈,默認(rèn)為1
Innodb_read_io_threads=4 # MySQL很多操作只能單線程,MySQL性能對CUP核數(shù)不敏感
Innodb_write_io_threads=4 # 根據(jù)CPU數(shù)和讀寫比率去掉
Innodb_file_per_table=on # 每個表使用獨(dú)立表空間铆隘,默認(rèn)為OFF(使用共享表空間)
Percona的MySQL配置生成器:https://tools.percona.com/wizard
預(yù)防SQL注入
1卓舵、JDBC防止SQL注入
使用 java.sql.Preparedstatement
string sql = "select * from people p where p.id = ? and p.name = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,id);
ps.setString(2,name);
ResultSet rs = ps.executeQuery();
原理
PreparedStatement使用了MySQL支持的預(yù)編譯功能,語句和參數(shù)分開提交
連接池要指定開啟預(yù)編譯:jdbc:mysql://localhost:3306/db1?&useServerPrepStmts=true&cachePrepStmts=true
MySQL預(yù)編譯
prepare statement_1 from 'select * from user where username like ?';
set @username='%小宋%';
execute statement_1 using @username;
2膀钠、MyBatis防止SQL注入
用 #{} 來引用參數(shù)掏湾, 而非 '${}'。使用'${}'時肿嘲,要過濾參數(shù)里的危險字符
#{} 下層調(diào)用了PreparedStatement