SlideShare a Scribd company logo
Optimzing mysql
I feel the need....the need for speed. 壮志凌云( top gun )
性能监控  10 分 性能优化  20 分 常见的架构  10 分 Tools and tips 10 分
性能监控 操作系统级别的 Mysql 级别
操作系统 vmstat  iostat free top sar mpstat uptime, netstat strace
mysql Show processlist  Mysqlreport  http://hackmysql.com/mysqlreportguide Mysqladmin -uxxx –p -i 1 -r extendedstatus|grep -v “| 0” Innotop  http://www.xaprb.com/blog/2006/07/02/innotop-mysql-innodb-monitor/ Show global status,show inondb status show session status; Mytop  Explain Profiling Mysqldumpslow mysqlsla sysbench
vmstat 基本的物理和虚拟内存的使用和一些基本的系统统计信息 Vmstat –S M Vmstat 5 5 Swap 颠簸现象 Top  ps axl
iostat 度量磁盘 i/o,cpu 和设备 io 使用 Iostat  -c  iostat –dx  5 %iowait 并发请求的数量  并发 = ( r/s+w/s ) *(svctm/1000)( 从头到尾服务请求时间 )
Cpu 密集型机器
Io 密集型
Swap idle
谁可能导致高的 cpu usage query  , joins,every 进程切换, 锁表 内存排序 临时表 加密算法 谁可能导致高的 disk usage 临时表 硬盘上排序 start with an idea ,then look for infromation support it
netstat 服务链接状态 netstat -nat |awk '{print $6}' | sort | uniq -c | sort -n  是否被 dos : netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n  /bin/netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -rn|grep -v -E '172.16|127.0'|awk '{if ($2!=null && $1>10) {print $1,$2}}'
其它 Top : Ps :  ps aux|awk '{ if ($6 >(1024*15)){print $2}}'|grep -v PID ………………
分析服务器 Mysqlreport Mysqladmin  mysqladmin extended -r -i 10| grep –v “| 0 ” -uroot –p Innotop …. Show global status Show innodb status
分析查询 Show processlist Flush status Show session status like ‘Select%’ Show session status like ‘Handler%’ Show session status like ‘Sort%’ Show session status like ‘Create%’ Set profiling
分析 mysql 连接 netstat -ntp| grep :40327  netstat -ntp| grep 10940/mysqld grep 3306 /etc/services ps -eaf| grep 'UID\|10940' lsof -i -P| grep 10942
性能优化 操作系统  mysql 应用程序
操作系统 不要交换区。如果内存不足,增加更多的内存或配置你的系统使用较少内存。 flick:echo 0 > /proc/sys/vm/swappiness  innodb_flush_method=O_DIRECT 不要使用 NFS 磁盘 ( 会有 NFS 锁定的问题 ) 。  增加系统和 MySQL 服务器的打开文件数量。 ( 在 safe_mysqld 脚本中加入 ulimit -n #) 。  增加系统的进程和线程数量。  选择使用哪种文件系统。在 Linux 上的 Reiserfs 对于打开、读写都非常快。文件检查只需几秒种。
Mysql 配置 Mysiam  缓存 key_buffer_size key_buffer_1.key_buffer_size=1G show variables like 'key_buffer_size';  show global status like 'key_read%';  key_cache_miss_rate  =  Key_reads / Key_read_requests * 100%  du –sch `find /mysqldatapath/ -name  “*.MYI”` Innodb  缓冲池 innodb_bufer_pool_size 80% show status like 'Innodb_buffer_pool_read%' Innodb_buffer_pool_read_requests/Innodb_buffer_pool_reads show innodb status\G   BUFFER POOL AND MEMORY Total memory allocated 4668764894;
查询缓存   show global status like 'Qcache%'; show variables ‘like query_cache%';  查询缓存利用率  = (query_cache_size – Qcache_free_memory) / query_cache_size * 100%  查询缓存命中率  = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100% http://www.day32.com/MySQL/
innodb 刷写日志缓冲 Innodb_flush_log_at_trx_commit 0 1 2   打开并清写日志和文件 innodb_flush_method
myisam 初始化:  initial =key_buffer_size + query_cache_size 连接 per_connection=(sort_buffer_size +read_rnd_buffer_size + join_buffer_size + read_buffer_size + thead_stack ) 总和: initial + max_connections * per_connection  initial + ((max_connections * per_connection)/3)
innodb 初始化:  initial =innodb_buffer_pool_size   + query_cache_size 连接 per_connection = (sort_buffer_size + join_buffer_size + thead_stack + binlog_cache_size + read_buffer_size + read_rnd_buffer_size) 总和: initial + max_connections * per_connection  initial + ((max_connections * per_connection)/3)
数据类型选择 更小通常更好 , 慷慨并不明智 简单就好 使用 mysql 内建的类型保存日期和时间,使用 timestamp  保存,空间是 datetime 一半 使用整数保存 ip 15 bytes for char(15) vs. 4 bytes for the integer  ip2long()  和  long2ip()  inet_aton 3  尽量避免 null 4  Char /varcha 的选择 对于 MyISAM 而言,如果没有 VARCHAR , TEXT 等变长类型,那么每行数据所占用的空间都是定长的( Fixed ),俗称静态表,相对应的就是动态表。当执行一次查询时, MySQL 可以通过索引文件找到所需内容的实际行号,此时,由于每行数据所占用的空间都是定长的( Fixed ),所以可以通过查询到的实际行号直接定位到数据文件的具体位置, 对于 InnoDB 而言,数据行是没有所谓定长与否的概念的,这是由其结构所决定的:在 InnoDB 中,数据就位于 Primary Key 的 B-Tree 叶节点之上而除 Primary Key 之外的索引被称为 Secondary Index ,它们直接指向 Primary Key 。 用 char 来代替 varchar , MyISAM 是这样, InnoDB 则相反 5  使用 enum  代替字符串类型  select internet + 0 from  hotel_info group by internet;
索引 隔离列 select * from tablename where id+1=5 Select * where TO_DAYS(CURRENT_DATE) – TO_DAYS(data_col) < =10 Select * where data_col >=date_sub(current_date,interval 10 day)  Select * where data_col >= date_sum(‘2010-04-12’,interval 10 day) EXPLAIN SELECT * FROM film WHERE title LIKE 'Tr%'\G EXPLAIN SELECT * FROM film WHERE LEFT(title,2) = 'Tr' \G 组合索引 前缀索引 覆盖索引 合并索引 去除多余索引和重复索引  create table test (id int  not null primary key, unique(id), index(id) )
合并索引 索引合并 方法用于通过 range 扫描搜索行并将结果合成一个。合并会产 生并集、交集或者正在进行的扫描的交集的并集。 在 EXPLAIN 输出中,该方法表现为 type 列内的 index_merge 。 在这种情况下, key 列包含一列使用的索引, key_len 包含这些索引的最长的关键元素 SELECT * FROM  tbl_name  WHERE  key_part1  = 10 OR  key_part2  = 20;
前缀索引 Key(a,b,c) Order by a ,order by a,b order by a,b,c order by a desc ,b desc,c desc  Where a = const order by b,c,where a=const and b =const order by c Where a = const order by b,c  where a = const  and b > const order by,c Order by a asc,b desc,c desc  Where g = const oder by  b ,c Where a = const order by c Where a = const order by a ,d  Where a>’xx’ order by b,c Where a>’xx’ order by a,b Where a=const order by b desc ,a asc
覆盖索引 Select * from products where actor=‘sean carrey’ and and title like ‘%apollo%’ Select * from products join (select prod_id from products where actor= ‘sean carrey’ and title like ‘%apollo%’) as t1 on (t1.prod_id = products.pro_id)
逆范式化 适当的冗余 分拆表
产品设计 产品设计 -> 数据交互 ->mysql Antoine de Saint—Exupery 是法国作家兼飞机设计师,他曾经说过:“设计者确定其设计已经达到了完美的标准不是不能再增加任何东西,而是不能再减少任何东 西。”   分页的实现
http://www.douban.com/group/BigBangTheory/discussion?start=25
1  explain select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10; +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra  | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ |  1 | SIMPLE  | page_test | ref  | idx_b_c  | idx_b_c | 4  | const | 2222 | Using where | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ 2 mysql> explain select SQL_NO_CACHE * from page_test, (select SQL_NO_CACHE id from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10) temp where page_test.id=temp.id; +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ | id | select_type | table  | type  | possible_keys | key  | key_len | ref  | rows | Extra  | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ |  1 | PRIMARY  | <derived2> | ALL  | NULL  | NULL  | NULL  | NULL  |  10 |  | |  1 | PRIMARY  | page_test  | eq_ref | PRIMARY  | PRIMARY | 8  | temp.id |  1 |  | |  2 | DERIVED  | page_test  | ref  | idx_b_c  | idx_b_c | 4  |  | 2222 | Using where; Using index | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ 3  explain select SQL_NO_CACHE * from page_test force index(idx_b_id) where b=1 and id<187796 order by id desc limit 10; +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ | id | select_type | table  | type  | possible_keys | key  | key_len | ref  | rows | Extra  | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ |  1 | SIMPLE  | page_test | range | idx_b_id  | idx_b_id | 12  | NULL |  190 | Using where | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ 5 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc 870,10 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c asc 9120,10 ./tuning-primer.sh  all
优化 count(*) Mysiam: select sql_no_cache count(*) from  statistic_go where id > 10; select sql_no_cache (select count(*) from statistic_go) - count(*) from statistic_go where  id <=10; Innodb   : 在缺少 where 字句的情况下, InnoDB 不对 count(*) 查询进行优化–这个是事实。  SELECT COUNT(*) FROM sbtest1 WHERE id>=0;   建立计数器触发器 对同一个表的 select  和 update update hotel_image inner join (select count(*) as cnt from hotel_image) as der set hotel_image.size = der.cnt;
 
其它一些 Group by  不进行排序,可以 ordery by null 能够快速缩小结果集的 WHERE 条件写在前面,如果有恒量条件,也尽量放在前面 使用  UNION  来取代  IN  和  OR 定期执行 optimize / analyze table 往 innoDB 表导入数据时,先关闭 autocommit 模式,否则会实时刷新数据到磁盘 对于频繁更改的 MyISAM 表 , 应尽量避免更新所有变长字段 (VARCHAR 、 BLOB 和 TEXT) 分表 分库 汇总表 十大热门话题  create table hotel_infonew like hotel_info; rename table hotel_info to hotel_info_old ,hotel_infonew to hotel_info 放弃关系型数据库  key=>value  ,计数表
常见架构方案 Replication 双 master 集群。。。。。
replication 1  数据分发  ,scale out , sacle up 2  负载均衡  load balance 3  备份,一般不会用作备份,一旦执行 delete 操作, replication 也不会保留 4  高可用  5  可以在不同的主从库上使用不同的存储引擎
 
原理
简单的讲就是 master 记录其变化到 binlog , slave 接收到变化后会记录到他的 Relay log , slave 通过重放 relay log ,然后就写进自己的 log 1) 、 Slave 上面的 IO 进程连接上 Master ,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; 2) 、 Master 接收到来自 Slave 的 IO 进程的请求后,通过负责复制的 IO 进程根据请求信息读取制定日志指定位置之后的日志信息,返回给 Slave  的 IO 进程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到 Master 端的 bin-log 文件的名称以及 bin-log 的位置; 3) 、 Slave 的 IO 进程接收到信息后,将接收到的日志内容依次添加到 Slave 端的 relay-log 文件的最末端,并将读取到的 Master 端的  bin-log 的文件名和位置记录到 master-info 文件中,以便在下一次读取的时候能够清楚的高速 Master“ 我需要从某个 bin-log 的哪 个位置开始往后的日志内容,请发给我”; 4) 、 Slave 的 Sql 进程检测到 relay-log 中新增加了内容后,会马上解析 relay-log 的内容成为在 Master 端真实执行时候的那些可执行的内容,并在自身执行
常见问题 延迟 主从不同步 命令复制 基于行复制
高可用 通过 Heartbert2  让 Mysql Replication  具有 HA
双 master
工具 Innotop Maatkit  mk-table-checksum mk-table-sync   mk-parallel-dump   mk-parallel-restore mk-find Optimize table  脚本
Tools and  tips \h 命令  大批量数据的导入 load data infile ‘/path/to/file’ into table tbl_name; alter table tbl_name disable keys; alter table tbl_name enable keys;  快速复制表结构: create table clone_tbl select * from tbl_name limit  0 Gdb 更改 mysql 配置  show variables like 'log_slave_updates';  set global log_slave_updates=1; system gdb -p $(pidof mysqld) -ex &quot;set opt_log_slave_updates=1&quot; -batch  gdb -p $(pidof mysqld) \ -ex &quot;set max_connections=5000&quot; \  -ex &quot;call resize_thr_alarm(5030)&quot; -batch truncate table  不能复制到从库 perl -ne ‘m/^([^#][^\s=]+)\s*(=.*|)/ && printf(“%-35s%s\n”, $1, $2)’ /etc/mysql/my.cnf  美化 my.cnf 加快 alter table Dump 文件中找出 create table sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `hotel_info`/!d;q'  hotel_new2010-3-1117.sql
同一个团队,同一个梦想  Thanks!

More Related Content

What's hot (20)

PPTX
从问题开始,谈前端架构
裕波 周
 
KEY
realtime-twitter-search
亚军 汪
 
PDF
Advanced heap exploitaion
Angel Boy
 
PDF
【Maclean liu技术分享】深入理解oracle中mutex的内部原理
maclean liu
 
KEY
微博实时搜索
亚军 汪
 
PDF
程式人雜誌 -- 2014 年11月號
鍾誠 陳鍾誠
 
PDF
JavaScript 快速複習 2017Q1
Sheng-Han Su
 
PDF
Ch7 教學
hungchiayang1
 
PDF
快速了解PostgreSQL
正中 周
 
PDF
Return to dlresolve
Angel Boy
 
PPT
如何学习Bash Shell
LI Daobing
 
PDF
Maintainable Javascript
Haixu (Hector) Guo
 
PPTX
Sql基础培训
Ji ZHANG
 
PDF
Ch7 範例
hungchiayang1
 
PPS
Hadoop基础及hive入门
haiwang
 
PDF
Ch12 教學
hungchiayang1
 
PDF
Ch10 教學
hungchiayang1
 
PPTX
Python入門:5大概念初心者必備 2021/11/18
Derek Lee
 
PPTX
Python入門:5大概念初心者必備
Derek Lee
 
DOC
mysql的字符串函数
wensheng wei
 
从问题开始,谈前端架构
裕波 周
 
realtime-twitter-search
亚军 汪
 
Advanced heap exploitaion
Angel Boy
 
【Maclean liu技术分享】深入理解oracle中mutex的内部原理
maclean liu
 
微博实时搜索
亚军 汪
 
程式人雜誌 -- 2014 年11月號
鍾誠 陳鍾誠
 
JavaScript 快速複習 2017Q1
Sheng-Han Su
 
Ch7 教學
hungchiayang1
 
快速了解PostgreSQL
正中 周
 
Return to dlresolve
Angel Boy
 
如何学习Bash Shell
LI Daobing
 
Maintainable Javascript
Haixu (Hector) Guo
 
Sql基础培训
Ji ZHANG
 
Ch7 範例
hungchiayang1
 
Hadoop基础及hive入门
haiwang
 
Ch12 教學
hungchiayang1
 
Ch10 教學
hungchiayang1
 
Python入門:5大概念初心者必備 2021/11/18
Derek Lee
 
Python入門:5大概念初心者必備
Derek Lee
 
mysql的字符串函数
wensheng wei
 

Viewers also liked (6)

PDF
Estudio Zoom a la Tecnología (versión pública)
Visión Humana
 
DOC
Epgp term v mos group assignment april 2010
Rajendra Inani
 
DOC
Küreselleşme ve milliyetçilik
Yusuf Ertem
 
PPTX
Presentacion iva
eachicaiza
 
PDF
Vinhbt datn
Vinh Bui
 
DOCX
Annova test
dushu_mail
 
Estudio Zoom a la Tecnología (versión pública)
Visión Humana
 
Epgp term v mos group assignment april 2010
Rajendra Inani
 
Küreselleşme ve milliyetçilik
Yusuf Ertem
 
Presentacion iva
eachicaiza
 
Vinhbt datn
Vinh Bui
 
Annova test
dushu_mail
 
Ad

Similar to Optimzing mysql (20)

PPTX
Mysql调优
ken shin
 
PPSX
MySQL应用优化实践
mysqlops
 
PPSX
浅谈 My sql 性能调优
thinkinlamp
 
PPT
Sina my sq概述及优化
pigso
 
PDF
MySQL运维那些事
Leo Zhou
 
PDF
My sql数据库开发的三十六条军规
isnull
 
PDF
MySQL数据库开发的三十六条军规
mysqlops
 
PDF
Mysql数据库开发的三十六条军规 石展_完整
Yousri Yan
 
PPT
Mysql introduction-and-performance-optimization
isnull
 
PPT
mysql总结
haiwang
 
PDF
对MySQL应用的一些总结
Lixun Peng
 
PDF
MySQL InnoDB 源码实现分析(一)
frogd
 
PPTX
My sql 5.6新特性深入剖析——innodb引擎
frogd
 
PDF
浅谈 MySQL 性能调优
Sky Jian
 
PDF
MySQL技术分享:一步到位实现mysql优化
Jinrong Ye
 
PPSX
Mysql遇到的一些问题
wang tongchao
 
KEY
111030 gztechparty-小路-云时代的mysql
Zoom Quiet
 
PDF
MySQL查询优化浅析
frogd
 
PDF
Oracle&mysql数据库模式设计
Beenyoung Lee
 
PDF
Mysql fast share
rfyiamcool
 
Mysql调优
ken shin
 
MySQL应用优化实践
mysqlops
 
浅谈 My sql 性能调优
thinkinlamp
 
Sina my sq概述及优化
pigso
 
MySQL运维那些事
Leo Zhou
 
My sql数据库开发的三十六条军规
isnull
 
MySQL数据库开发的三十六条军规
mysqlops
 
Mysql数据库开发的三十六条军规 石展_完整
Yousri Yan
 
Mysql introduction-and-performance-optimization
isnull
 
mysql总结
haiwang
 
对MySQL应用的一些总结
Lixun Peng
 
MySQL InnoDB 源码实现分析(一)
frogd
 
My sql 5.6新特性深入剖析——innodb引擎
frogd
 
浅谈 MySQL 性能调优
Sky Jian
 
MySQL技术分享:一步到位实现mysql优化
Jinrong Ye
 
Mysql遇到的一些问题
wang tongchao
 
111030 gztechparty-小路-云时代的mysql
Zoom Quiet
 
MySQL查询优化浅析
frogd
 
Oracle&mysql数据库模式设计
Beenyoung Lee
 
Mysql fast share
rfyiamcool
 
Ad

More from liufabin 66688 (20)

PDF
人大2010新生住宿指南
liufabin 66688
 
PDF
中山大学2010新生住宿指南
liufabin 66688
 
PDF
武汉大学2010新生住宿指南
liufabin 66688
 
PDF
天津大学2010新生住宿指南
liufabin 66688
 
PDF
清华2010新生住宿指南
liufabin 66688
 
PDF
南京大学2010新生住宿指南
liufabin 66688
 
PDF
复旦2010新生住宿指南
liufabin 66688
 
PDF
北京师范大学2010新生住宿指南
liufabin 66688
 
PDF
北大2010新生住宿指南
liufabin 66688
 
PDF
Mysql Replication Excerpt 5.1 En
liufabin 66688
 
PDF
Drupal Con My Sql Ha 2008 08 29
liufabin 66688
 
PDF
Application Partitioning Wp
liufabin 66688
 
PDF
090507.New Replication Features(2)
liufabin 66688
 
DOC
Mysql Replication
liufabin 66688
 
PPT
High Performance Mysql
liufabin 66688
 
PDF
high performance mysql
liufabin 66688
 
PDF
Lecture28
liufabin 66688
 
PDF
Refactoring And Unit Testing
liufabin 66688
 
PDF
Refactoring Simple Example
liufabin 66688
 
PDF
Refactoring Example
liufabin 66688
 
人大2010新生住宿指南
liufabin 66688
 
中山大学2010新生住宿指南
liufabin 66688
 
武汉大学2010新生住宿指南
liufabin 66688
 
天津大学2010新生住宿指南
liufabin 66688
 
清华2010新生住宿指南
liufabin 66688
 
南京大学2010新生住宿指南
liufabin 66688
 
复旦2010新生住宿指南
liufabin 66688
 
北京师范大学2010新生住宿指南
liufabin 66688
 
北大2010新生住宿指南
liufabin 66688
 
Mysql Replication Excerpt 5.1 En
liufabin 66688
 
Drupal Con My Sql Ha 2008 08 29
liufabin 66688
 
Application Partitioning Wp
liufabin 66688
 
090507.New Replication Features(2)
liufabin 66688
 
Mysql Replication
liufabin 66688
 
High Performance Mysql
liufabin 66688
 
high performance mysql
liufabin 66688
 
Lecture28
liufabin 66688
 
Refactoring And Unit Testing
liufabin 66688
 
Refactoring Simple Example
liufabin 66688
 
Refactoring Example
liufabin 66688
 

Optimzing mysql

  • 2. I feel the need....the need for speed. 壮志凌云( top gun )
  • 3. 性能监控 10 分 性能优化 20 分 常见的架构 10 分 Tools and tips 10 分
  • 5. 操作系统 vmstat iostat free top sar mpstat uptime, netstat strace
  • 6. mysql Show processlist Mysqlreport http://hackmysql.com/mysqlreportguide Mysqladmin -uxxx –p -i 1 -r extendedstatus|grep -v “| 0” Innotop http://www.xaprb.com/blog/2006/07/02/innotop-mysql-innodb-monitor/ Show global status,show inondb status show session status; Mytop Explain Profiling Mysqldumpslow mysqlsla sysbench
  • 8. iostat 度量磁盘 i/o,cpu 和设备 io 使用 Iostat -c iostat –dx 5 %iowait 并发请求的数量 并发 = ( r/s+w/s ) *(svctm/1000)( 从头到尾服务请求时间 )
  • 12. 谁可能导致高的 cpu usage query , joins,every 进程切换, 锁表 内存排序 临时表 加密算法 谁可能导致高的 disk usage 临时表 硬盘上排序 start with an idea ,then look for infromation support it
  • 13. netstat 服务链接状态 netstat -nat |awk '{print $6}' | sort | uniq -c | sort -n 是否被 dos : netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n /bin/netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -rn|grep -v -E '172.16|127.0'|awk '{if ($2!=null && $1>10) {print $1,$2}}'
  • 14. 其它 Top : Ps : ps aux|awk '{ if ($6 >(1024*15)){print $2}}'|grep -v PID ………………
  • 15. 分析服务器 Mysqlreport Mysqladmin mysqladmin extended -r -i 10| grep –v “| 0 ” -uroot –p Innotop …. Show global status Show innodb status
  • 16. 分析查询 Show processlist Flush status Show session status like ‘Select%’ Show session status like ‘Handler%’ Show session status like ‘Sort%’ Show session status like ‘Create%’ Set profiling
  • 17. 分析 mysql 连接 netstat -ntp| grep :40327 netstat -ntp| grep 10940/mysqld grep 3306 /etc/services ps -eaf| grep 'UID\|10940' lsof -i -P| grep 10942
  • 18. 性能优化 操作系统 mysql 应用程序
  • 19. 操作系统 不要交换区。如果内存不足,增加更多的内存或配置你的系统使用较少内存。 flick:echo 0 > /proc/sys/vm/swappiness innodb_flush_method=O_DIRECT 不要使用 NFS 磁盘 ( 会有 NFS 锁定的问题 ) 。 增加系统和 MySQL 服务器的打开文件数量。 ( 在 safe_mysqld 脚本中加入 ulimit -n #) 。 增加系统的进程和线程数量。 选择使用哪种文件系统。在 Linux 上的 Reiserfs 对于打开、读写都非常快。文件检查只需几秒种。
  • 20. Mysql 配置 Mysiam 缓存 key_buffer_size key_buffer_1.key_buffer_size=1G show variables like 'key_buffer_size'; show global status like 'key_read%'; key_cache_miss_rate = Key_reads / Key_read_requests * 100% du –sch `find /mysqldatapath/ -name “*.MYI”` Innodb 缓冲池 innodb_bufer_pool_size 80% show status like 'Innodb_buffer_pool_read%' Innodb_buffer_pool_read_requests/Innodb_buffer_pool_reads show innodb status\G BUFFER POOL AND MEMORY Total memory allocated 4668764894;
  • 21. 查询缓存 show global status like 'Qcache%'; show variables ‘like query_cache%'; 查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100% 查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100% http://www.day32.com/MySQL/
  • 22. innodb 刷写日志缓冲 Innodb_flush_log_at_trx_commit 0 1 2 打开并清写日志和文件 innodb_flush_method
  • 23. myisam 初始化: initial =key_buffer_size + query_cache_size 连接 per_connection=(sort_buffer_size +read_rnd_buffer_size + join_buffer_size + read_buffer_size + thead_stack ) 总和: initial + max_connections * per_connection initial + ((max_connections * per_connection)/3)
  • 24. innodb 初始化: initial =innodb_buffer_pool_size + query_cache_size 连接 per_connection = (sort_buffer_size + join_buffer_size + thead_stack + binlog_cache_size + read_buffer_size + read_rnd_buffer_size) 总和: initial + max_connections * per_connection initial + ((max_connections * per_connection)/3)
  • 25. 数据类型选择 更小通常更好 , 慷慨并不明智 简单就好 使用 mysql 内建的类型保存日期和时间,使用 timestamp 保存,空间是 datetime 一半 使用整数保存 ip 15 bytes for char(15) vs. 4 bytes for the integer ip2long() 和 long2ip() inet_aton 3 尽量避免 null 4 Char /varcha 的选择 对于 MyISAM 而言,如果没有 VARCHAR , TEXT 等变长类型,那么每行数据所占用的空间都是定长的( Fixed ),俗称静态表,相对应的就是动态表。当执行一次查询时, MySQL 可以通过索引文件找到所需内容的实际行号,此时,由于每行数据所占用的空间都是定长的( Fixed ),所以可以通过查询到的实际行号直接定位到数据文件的具体位置, 对于 InnoDB 而言,数据行是没有所谓定长与否的概念的,这是由其结构所决定的:在 InnoDB 中,数据就位于 Primary Key 的 B-Tree 叶节点之上而除 Primary Key 之外的索引被称为 Secondary Index ,它们直接指向 Primary Key 。 用 char 来代替 varchar , MyISAM 是这样, InnoDB 则相反 5 使用 enum 代替字符串类型 select internet + 0 from hotel_info group by internet;
  • 26. 索引 隔离列 select * from tablename where id+1=5 Select * where TO_DAYS(CURRENT_DATE) – TO_DAYS(data_col) < =10 Select * where data_col >=date_sub(current_date,interval 10 day) Select * where data_col >= date_sum(‘2010-04-12’,interval 10 day) EXPLAIN SELECT * FROM film WHERE title LIKE 'Tr%'\G EXPLAIN SELECT * FROM film WHERE LEFT(title,2) = 'Tr' \G 组合索引 前缀索引 覆盖索引 合并索引 去除多余索引和重复索引 create table test (id int not null primary key, unique(id), index(id) )
  • 27. 合并索引 索引合并 方法用于通过 range 扫描搜索行并将结果合成一个。合并会产 生并集、交集或者正在进行的扫描的交集的并集。 在 EXPLAIN 输出中,该方法表现为 type 列内的 index_merge 。 在这种情况下, key 列包含一列使用的索引, key_len 包含这些索引的最长的关键元素 SELECT * FROM tbl_name WHERE key_part1 = 10 OR key_part2 = 20;
  • 28. 前缀索引 Key(a,b,c) Order by a ,order by a,b order by a,b,c order by a desc ,b desc,c desc Where a = const order by b,c,where a=const and b =const order by c Where a = const order by b,c where a = const and b > const order by,c Order by a asc,b desc,c desc Where g = const oder by b ,c Where a = const order by c Where a = const order by a ,d Where a>’xx’ order by b,c Where a>’xx’ order by a,b Where a=const order by b desc ,a asc
  • 29. 覆盖索引 Select * from products where actor=‘sean carrey’ and and title like ‘%apollo%’ Select * from products join (select prod_id from products where actor= ‘sean carrey’ and title like ‘%apollo%’) as t1 on (t1.prod_id = products.pro_id)
  • 31. 产品设计 产品设计 -> 数据交互 ->mysql Antoine de Saint—Exupery 是法国作家兼飞机设计师,他曾经说过:“设计者确定其设计已经达到了完美的标准不是不能再增加任何东西,而是不能再减少任何东 西。” 分页的实现
  • 33. 1 explain select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10; +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ | 1 | SIMPLE | page_test | ref | idx_b_c | idx_b_c | 4 | const | 2222 | Using where | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ 2 mysql> explain select SQL_NO_CACHE * from page_test, (select SQL_NO_CACHE id from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10) temp where page_test.id=temp.id; +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | | | 1 | PRIMARY | page_test | eq_ref | PRIMARY | PRIMARY | 8 | temp.id | 1 | | | 2 | DERIVED | page_test | ref | idx_b_c | idx_b_c | 4 | | 2222 | Using where; Using index | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ 3 explain select SQL_NO_CACHE * from page_test force index(idx_b_id) where b=1 and id<187796 order by id desc limit 10; +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ | 1 | SIMPLE | page_test | range | idx_b_id | idx_b_id | 12 | NULL | 190 | Using where | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ 5 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc 870,10 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c asc 9120,10 ./tuning-primer.sh all
  • 34. 优化 count(*) Mysiam: select sql_no_cache count(*) from statistic_go where id > 10; select sql_no_cache (select count(*) from statistic_go) - count(*) from statistic_go where id <=10; Innodb  : 在缺少 where 字句的情况下, InnoDB 不对 count(*) 查询进行优化–这个是事实。 SELECT COUNT(*) FROM sbtest1 WHERE id>=0; 建立计数器触发器 对同一个表的 select 和 update update hotel_image inner join (select count(*) as cnt from hotel_image) as der set hotel_image.size = der.cnt;
  • 35.  
  • 36. 其它一些 Group by 不进行排序,可以 ordery by null 能够快速缩小结果集的 WHERE 条件写在前面,如果有恒量条件,也尽量放在前面 使用 UNION 来取代 IN 和 OR 定期执行 optimize / analyze table 往 innoDB 表导入数据时,先关闭 autocommit 模式,否则会实时刷新数据到磁盘 对于频繁更改的 MyISAM 表 , 应尽量避免更新所有变长字段 (VARCHAR 、 BLOB 和 TEXT) 分表 分库 汇总表 十大热门话题 create table hotel_infonew like hotel_info; rename table hotel_info to hotel_info_old ,hotel_infonew to hotel_info 放弃关系型数据库 key=>value ,计数表
  • 37. 常见架构方案 Replication 双 master 集群。。。。。
  • 38. replication 1 数据分发 ,scale out , sacle up 2 负载均衡 load balance 3 备份,一般不会用作备份,一旦执行 delete 操作, replication 也不会保留 4 高可用 5 可以在不同的主从库上使用不同的存储引擎
  • 39.  
  • 41. 简单的讲就是 master 记录其变化到 binlog , slave 接收到变化后会记录到他的 Relay log , slave 通过重放 relay log ,然后就写进自己的 log 1) 、 Slave 上面的 IO 进程连接上 Master ,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; 2) 、 Master 接收到来自 Slave 的 IO 进程的请求后,通过负责复制的 IO 进程根据请求信息读取制定日志指定位置之后的日志信息,返回给 Slave 的 IO 进程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到 Master 端的 bin-log 文件的名称以及 bin-log 的位置; 3) 、 Slave 的 IO 进程接收到信息后,将接收到的日志内容依次添加到 Slave 端的 relay-log 文件的最末端,并将读取到的 Master 端的 bin-log 的文件名和位置记录到 master-info 文件中,以便在下一次读取的时候能够清楚的高速 Master“ 我需要从某个 bin-log 的哪 个位置开始往后的日志内容,请发给我”; 4) 、 Slave 的 Sql 进程检测到 relay-log 中新增加了内容后,会马上解析 relay-log 的内容成为在 Master 端真实执行时候的那些可执行的内容,并在自身执行
  • 42. 常见问题 延迟 主从不同步 命令复制 基于行复制
  • 43. 高可用 通过 Heartbert2 让 Mysql Replication 具有 HA
  • 45. 工具 Innotop Maatkit mk-table-checksum mk-table-sync mk-parallel-dump mk-parallel-restore mk-find Optimize table 脚本
  • 46. Tools and tips \h 命令 大批量数据的导入 load data infile ‘/path/to/file’ into table tbl_name; alter table tbl_name disable keys; alter table tbl_name enable keys; 快速复制表结构: create table clone_tbl select * from tbl_name limit 0 Gdb 更改 mysql 配置 show variables like 'log_slave_updates'; set global log_slave_updates=1; system gdb -p $(pidof mysqld) -ex &quot;set opt_log_slave_updates=1&quot; -batch gdb -p $(pidof mysqld) \ -ex &quot;set max_connections=5000&quot; \ -ex &quot;call resize_thr_alarm(5030)&quot; -batch truncate table 不能复制到从库 perl -ne ‘m/^([^#][^\s=]+)\s*(=.*|)/ && printf(“%-35s%s\n”, $1, $2)’ /etc/mysql/my.cnf 美化 my.cnf 加快 alter table Dump 文件中找出 create table sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `hotel_info`/!d;q' hotel_new2010-3-1117.sql