mysql基本介绍和优化技巧
一. mysql框架和基本介紹
1. 框架圖
更詳細:
2. 存儲引擎
MYISAM與INNODB對比:
MYISAM:mysql5.1及以前版本的默認存儲引擎。支持全文檢索,壓縮,表級鎖等,但不支持事務,行級鎖,崩潰后的數據恢復等
INNODB:mysql5.5及之后的默認存儲引擎。支持事務,行級鎖,數據恢復,mysql5.6 中的innodb(1.2)支持全文檢索。
如何選擇:innodb對于絕大多數的用戶都是最佳的選擇,除非某些存儲引擎能滿足特殊需求且使用者很了解這種存儲引擎。
二.?查詢性能的方法
1. 配置文件 my.cnf (linux),?
讀配置文件的一般順序:
? ? ?1) ? ? /etc/my.cnf
? ? ?2) ? ? ?DATADIR/my.cnf
? ? ?3) ? ? ?~/.my.cnf
可以通過命令獲知:
mysqld --verbose --help | grep -A 1 'Default options’
結果:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
2. 環境變量
從my,cnf讀取環境變量值,
查看變量值方法: show variables like?“”, (不清楚變量名時,用模糊匹配%)
例如
設置(全局)變量方法:
set (global) 變量名=變量值
例如設置 開啟慢查詢日志
set global slow_query_log=1;
設置后查看變量值
show variables like "%slow%”;
| slow_query_log??????????? | ON?
變量列表:http://dev.mysql.com/doc/refman/5.6/en/mysqld-option-tables.html
分析sql語句時一些有用且默認不開啟的變量:
1) 慢查詢日志: 開關 slow_query_log, 閾值:long_query_time (單位 秒)
2) sql剖析工具profile,開關:profiling,歷史記錄數:profiling_history_size(最大為100)
3) ? 記錄每條sql語句,開關:general_log, log文件位置:general_log_file
…?…
3. 查看sql服務狀態
語法?SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern' | WHERE expr]清除狀態 FLUSH STATUS;
清除表緩存?
reset query cache ;
FLUSH TABLE [TABLE NAME]
? ??
用法舉例
FLUSH STATUS;
SELECT ...;
SHOW SESSION STATUS LIKE 'Handler_read%’; ?(SHOW SESSION STATUS LIKE 'Handler_%’;)
EXPLAIN SELECT?…;
具體參數可以參考:
http://lxneng.iteye.com/blog/451985
http://hi.baidu.com/thinkinginlamp/item/8d038333c6b0674a3075a1d3
4.?查詢剖析工具?show profiles
開啟?
set profiling=1;
set profiling_history_size=50 ?最大為100
用法:
show profiles;
show profile; //展示最后一條query時間消耗
show profile for query ID; // 替換ID值 從show profiles 表里。
顯示更多內容
show profile cpu,block io for query 4;
5. 獲取查詢計劃的信息 explain?
用法 explain ?[sql查詢語句]
例如:
注意每列的含義
三.?數據類型優化
1. 選擇優化的數據類型原則
? ? ?1)盡量使用可以正確存儲數據的最小數據類型。例如: 只存0-200,用tinyint unsigned 更好。
? ? ?2)選擇簡單的數據類型。例如整形比字符操作代價更低,存儲時間用datetime而不是字符串,用整形存儲ip。
2. 數據類型(主要說整形和字符串)
? ? ?1)整型?
TINYINT 1 字節 (-128,127) (0,255) 小整數值?
SMALLINT 2 字節 (-32 768,32 767) (0,65 535) 大整數值?
MEDIUMINT 3 字節 (-8 388 608,8 388 607) (0,16 777 215) 大整數值?
INT或INTEGER 4 字節 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整數值?
BIGINT 8 字節 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 極大整數值?
思考:tinyint(1)和tinyint(2)存儲空間相比較?
? ? ?2)字符串
varchar:內容+長度,0-255字節。
char:長度不足時,空格補足,0-255字節。
如何選擇:
選擇用varchar:字符串的最大長度比平均長度大很多,列更新少。但是一定要估算好長度(排序時)。
text和blob,大數據類型。
? ? ? 3)浮點,時間,位 等
四. 高性能索引
1. 索引基礎
索引結果為B+樹。
B樹:
? ? ? ?
B-樹:
B+樹:
MYISAM 主索引(輔助索引結構與其相同):
INNODB 主索引:
INNODB輔助索引:
索引優點:
? ? ?1) 索引大大減少了服務器需要掃描的數據量;
? ? ?2) 索引可以幫助服務器避免排序和臨時表;
? ? ?3) 索引可以將隨機I/O變為順序I/O。
2. 高性能索引策略
1) 獨立的列
? ? ?將索引單獨放到比較符號的一側,否則無法利用索引。
2)前綴索引和索引選擇性
3)合適的索引順序
4)覆蓋索引
極大的提高性能。
5)使用索引掃描做排序
其他策略:
1)多條件過濾,盡量重復利用索引,(sex,country,age)有索引,現在有查詢條件 sex,country,region,age 或者sex,country,region,city,age需要再建索引嗎?
例如:(gender,name) ?gender,gender name
select * from staff where name like?“123”;
select * from staff where gender in (0,1) and name like?“123";
2) 避免多個范圍查詢
3)延遲關聯
select *? from table2 order by cnt,id limit 100000,10;
select *? from table2 join (select id from table2 order by cnt limit 100000,10) as x using(id) ;
五. 查詢性能優化
1) 查詢執行的基礎
2) 查詢優化器的局限性
union限制
(select first_name,last_name from actor order by last_name) union (select first_name,last_name from customer order by last_name) order by last_name limit 20;
(select first_name,last_name from actor order by last_name limit 20) union (select first_name,last_name? from customer order by last_name limit 20) order by last_name limit 20 ;
在同一個表上查詢和更新
update foo as outer set cnt= (select count(*) from foo as inner where inner.type=outer.type);
update foo join (select type,count(*) as cnt from foo group by type) as der using(type) set foo.cnt=der.cnt;
3)優化特定關聯查詢
優化關聯:
explain select * from film inner join (film_actor,actor) on (film.film_id = film_actor.film_id and film_actor.actor_id = actor.actor_id); explain select straight_join * from film inner join (film_actor,actor) on (film.film_id = film_actor.film_id and film_actor.actor_id = actor.actor_id);
優化limit
優化sql_calc_found_rows
?select sql_calc_found_rows *? from table2 order by cnt,id limit 100000,10;
?select found_rows();
selelt count(*) from table ..
轉載于:https://www.cnblogs.com/yunlong/p/3901383.html
總結
以上是生活随笔為你收集整理的mysql基本介绍和优化技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 瀑布流效果 保存地址
- 下一篇: SQL Server含逗号分隔的数据匹配