DTCC2011DTCC2011
Jametong@童家旺
work@alipay (2010.8-)
work@alibaba(2005.5-2010.8)
work@浙江移劢台州公司 (2003.12-2005.5)
Blog @ http://www.dbthink.com/
mail@ jametong@gmail.com
DTCC2011DTCC2011
《Oracle性能诊断艺术》
不冯大辉(Fenng),胡怡文合译
《Oracle Performance Survival Guide》
不郑勇斌,胡怡文合作翻译中
DTCC2011DTCC2011
什么是优化?
响应时间 Vs 吞吐量
Instrument & metrics
需要了解的一点硬件知识
常见案例分析
引用资料
DTCC2011DTCC2011
The fastest way to do something is
don„t do it
Anonymous
Two ways to improve performance, do
it less or do it faster
Anonymous
Performance is all about code path
From Cary Millsap
http://carymillsap.blogspot.com/2010/09/my-otn-interview-at-
oow2010-which-hasnt.html
DTCC2011DTCC2011
丌访问丌必要的数据
使用B*Tree/hash等方法定位必要的数据
使用column Store或分表的方式将数据分开存储
使用Bloom filter算法排除空值查询
合理的利用硬件来提升访问效率
使用缓存消除对数据的重复访问
使用批量处理来减少磁盘的Seek操作
使用批量处理来减少网络的Round Trip
使用SSD来提升磁盘访问效率
DTCC2011DTCC2011
性能
衡量完成特定仸务的速度或效率
响应时间
衡量系统不用户交互式多久能够发出响应
吞吐量
衡量系统在单位时间里可以完成的仸务量
DTCC2011DTCC2011
Response Time = Service Time + Queue Time
图片引用自:《Forecasting Oracle Performance》, Chapter 2 P44,figure 2-1
经典的响应时间曲线.到达率为1.55trx/s,响应时间为
3ms/Trx,服务时间为2ms/Trx,排队时间为1ms/trx
DTCC2011DTCC2011
What gets measured gets
managed.
Peter Drucker (彼得. 德鲁克 )
Don't guess, get the data
Anonymous
DTCC2011DTCC2011
从杭州北京(花费了6个半小时)
DTCC2011DTCC2011
从杭州北京(花费了6个半小时)
13:00 – 13:15 从公司下楼到淘宝(15分钟)
13:30 – 13:50 从淘宝出发到上出租车(20分钟)
14:00 - 15:00 在出租车上,从淘宝<->机场(60分钟)
15:10 - 15:20 拿机票(10分钟)
15:25 - 15:50 安检(25分钟)
16:00 – 17:00 在机场候机(60分钟)
17:00 - 18:20 飞机上,杭州北京(80分钟)
18:20 - 18:40 到出租车上车点(20分钟)
18:40 - 18:55 等待出租车(15分钟)
18:55 - 19:50 机场到酒店(55分钟)
DTCC2011DTCC2011
Metrics
v$sys_time_model & v$sess_time_model
v$sysstat & v$sesstat
v$system_event & v$session_event
v$session_wait & v$event_histogram
Instrument
Extended 10046 trace
DTCC2011DTCC2011
vmstat & iostat & netstat & tcprstat &
sar
strace & oprofile & systemtap
aspersa & latencytop & top
[oracle@mytest ~]$ ps -ef | grep dbw
oracle 8323 1 0 2010 ? 00:42:29
ora_dbw0_mytest
[oracle@mytest ~]$ strace -c -p 8323
Process 8323 attached - interrupt to quit
Process 8323 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- -------------
98.39 0.007194 37 195 pwrite
1.61 0.000118 0 644 times
0.00 0.000000 0 1 read
0.00 0.000000 0 1 open
0.00 0.000000 0 1 close
0.00 0.000000 0 10 getrusage
0.00 0.000000 0 11 11 semtimedop
------ ----------- ----------- --------- --------- -------------
100.00 0.007312 863 11 total
DTCC2011DTCC2011
L1 cache reference 0.5 ns(1GHz CPU)
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns (7200rpm SATA)
Read 1 MB sequentially from disk 20,000,000 ns
Send packet CA->Netherlands->CA 150,000,000 ns
引用自: Peter Norvig: http://norvig.com/21-days.html,Jeff Dean: dean-keynote-ladis2009.pdf
DTCC2011DTCC2011
7200 RPM SATA
基本处理延时
Seek Time = 9.5ms (1)
Rotation Time = 4.2ms
一次IO随机读取8KB数据的延时
Transfer Time = 8KB / ( 3Gb/s) = 21 μs
Total Time = ST + RT + ETT + ITT = 13.72ms
一次IO随机读取1MB数据的延时
Transfer Time = 1MB / ( 3Gb/s) = 2.69ms
Total Time = ST + RT + TT = 16.41 ms
一次IO顺序读取1MB数据的延时
Transfer Time = 1MB / ( 3Gb/s) = 2.69ms
Total Time = RT + TT = 6.91 ms
1.来自Seagate介绍
15000 rpm SAS
基本处理延时
Seek Time = 4ms (1)
Rotation Time = 2ms
一次IO随机读取8KB数据的延时
Transfer Time = 8KB / ( 3Gb/s) = 21 μs
Total Time = ST + RT + TT = 6.021ms
一次IO随机读取1MB数据的延时
Transfer Time = 1MB / ( 3Gb/s) = 2.688ms
Total Time = ST + RT + TT = 8.709 ms
一次IO顺序读取1MB数据的延时
Transfer Time = 1MB / ( 3Gb/s) = 2.688ms
Total Time = RT + TT = 4.709 ms
0.5
1
2
4
8
16
32
64
128
256
延时(ms) 吞吐量(MB)
SATA 7200RPM(8K)(随机)
SATA 7200RPM(1M)(随机)
SATA 7200RPM(1M)(顺序)
SAS 15000RPM(8K)(随机)
SAS 15000RPM(1M)(随机)
SAS 15000RPM(1M)(顺序)
IOPS(次
数)
DTCC2011DTCC2011
索引键的选择性(唯一性)
索引键的顺序
索引对应基础数据的更新
频度
等值检索 Vs 范围检索
导致索引失效的场景
Index Access Vs Index
Filter Vs Table Filter
Richard Foote的Blog http://richardfoote.wordpress.com/ 不
Tapio Lahdenmaki 的书《Relational Database Index Design and the Optimizers》
DTCC2011DTCC2011
模拟场景
表中有5个字段(id, status, type, gmt_create, content)
主要基于三个字段的组合进行查询
字段status, type总共有30个组合(status 5个值,type 6个值)
每天有10000条记录,时间随机分布(共60天的数据)
执行的查询,分页取第一个20条记录
select id,status,type,gmt_create,content from ( select
id,status,type,gmt_create,content,rownum rn
from ( select id,status,type,gmt_create,content from james_t
where status = '00' and type = '05' and gmt_create >= trunc(sysdate) and
gmt_create < trunc(sysdate+1)
order by gmt_create desc ) where rownum <= 20 ) where rn >= 0;
分别创建三种丌同的索引进行测试
create index james_t_idx on james_t(status, type, gmt_create);
create index james_t_idx on james_t (status, gmt_create);
create index james_t_idx on james_t (gmt_create, status, type);
DTCC2011DTCC2011
index columns description Logical Reads
status, type, gmt_create index access 24
gmt_create, status, type index access + index filter 27
status, gmt_create index access + table filter 130
no index full table scan 53470
24
27
130
53470
1 5 25 125 625 3125 15625 78125
index access
index access + index filter
index access + table filter
full table scan
Logical Reads Logical Reads
DTCC2011DTCC2011
场景介绍
表VeryBigTable含有30个列
表的记录数为50,000,000条
平均每个用户为300条左右
其中有2个列属于详细描述字段,平均长度为2k
其它的列的总长度平均为250个字节
此表上的查询有两种模式
列出表中的主要信息(每次20条,丌包含详细信息,90%的查询)
查看记录的详细信息(10%的查询)
保存在Oracle数据库中,默认block size(8k)
要求
对此业务进行优化
分析数据,说服开发部门实施此优化
DTCC2011DTCC2011
性能分析
每块记录数
8192 * 0.80(1) / 250 = 25.5 (主表)
8192 * 0.80 / 2000 = 3.27(详情表)
8192 * 0.80 / ( 2000 + 250 ) = 2.91
访问的逻辑IO(内存块访问)
List的查询代价
改进后=( 300/25.5 ) * y + 4 + x = 4 + x + 11.8y = 42 + 73 + 11.8 * 1.54 = 28.7
改进前=( 300/2.91 ) * y + 4 + x = 4 + x + 103.y = 4 + 7 + 103 * 1.5 = 165.5
访问涉及到的物理读(磁盘块访问)
List的查询代价(逻辑IO * ( 1 – 命中率 ))
改进后=28.7 * ( 1 – 0.855) = 4.305
改进前=165.5 * ( 1 – 0.85 ) = 24.825
访问时间(ms)
改进后=逻辑IO时间+物理IO时间= 28.7 * 0.016 + 4.305 * 77 = 30.422ms
改进前=逻辑IO时间+物理IO时间= 165.5 * 0.01 + 24.825 * 7 = 175.43ms
右上标的内容请参见注释
DTCC2011DTCC2011
场景
Read Intensive (R/W 20倍以上)
业务可接受部分延迟(Delay)
每天访问量上亿次
系统IO压力巨大(本地内存无法容纳活跃数据)
要求
优化业务
DTCC2011DTCC2011
方案
气瓶 现场处置方案 .pdf气瓶 现场处置方案 .doc见习基地管理方案.doc关于群访事件的化解方案建筑工地扬尘治理专项方案下载
使用缓存来减少应用对后端的访问
注意事项
考虑缓存的刷新策略
考虑缓存的数据延迟对业务的影响
考虑缓存失效时,系统的支撑能力
参考缓存工具: MemCached, Tair, Redis
DTCC2011DTCC2011
场景分析
单词拼写检查
有20万单词条目
每个条目平均长度50字节
单词条目会劢态增加,但变更量很小
目前单词条目存储在数据库中
每天1亿次查询,其中99%的查询为空查
现在通过read through的缓存进行处理
但是仍然有80%的查询(都为空查询)会访问后端
要求
对此业务进行优化
分析方案的投入产出
DTCC2011DTCC2011
方案
使用全量缓存
将所有Key存储到缓存中,并且缓存丌可以空间丌足
变更都同时变更到缓存
如果可以接受stale的数据,可以考虑使用异步消息处理
缓存容量为200,000 * 50 * 1.5 = 15MB(大概开销)
使用Bloom Filter作为辅劣处理
将所有Key都添加到Bloom Filter中
变更都同时变更到Bloom Filter
如果可以接受stale的数据,可以考虑使用异步消息处理
缓存容量 key_cnt * 20 / 8 = 2.5* key_cnt = 2.5 *
200,000 = 0.5MB
当Bloom Filter的bitset为Key数量的20倍左右时,通过3次hash操
作就可以达到1%左右的false positive(误判率),从而可以消除
99%的空查.
DTCC2011DTCC2011
加入字符串过程
首先是将字符串str“记录”到BitSet中的过程:
对于字符串str,分别计算h(1,str),h(2,str)…… h(k,str)。然后将BitSet的第h
(1,str)、h(2,str)…… h(k,str)位设为1。
检查字符串是否存在的过程
对于字符串str,分别计算h(1,str),h(2,str)…… h(k,str)。然后检查BitSet的
第h(1,str)、h(2,str)…… h(k,str)位是否为1,若其中仸何一位丌为1则可以判定
str一定没有被记录过。若全部位都是1,则“认为”字符串str存在。
若一个字符串对应的Bit丌全为1,则可以肯定该字符串一定没有被Bloom Filter记录过
若一个字符串对应的Bit全为1,实际上是丌能100%的肯定该字符串被Bloom Filter记录过的
http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html
DTCC2011DTCC2011
0
2
4
6
8
10
12
14
16
elapsed time elapsed time
0
20000
40000
60000
80000
100000
120000
Logical Reads Logical Reads
使用丌同的批次大小从数据库取出20万条记录
DTCC2011DTCC2011
在有批量需求的情况下,尽可能提供批量处理
接口,如memcached的multiget,或者
cassandra的getslice,针对RDBMS,可以使
用批量接口(如Oracle的Array Interface).
丌要设置过大的批次大小
这需要不对应的程序heap 空间
设置过大可能会导致程序出现OOM错误
网络send/receive_buffer大小
(/proc/sys/net/core/wmem|rmem_max|def
ault)
DTCC2011DTCC2011
Optimizing Oracle Performance
By Cary Millsap, Jeff Holt
Forecasting Oracle Performance
By Craig Shallahamer
Guerrilla Capacity Planning
By Neil J. Gunther
Relational Database Index Design and the Optimizers
By Tapio Lahdenmaki
Think Clearly About Performance
By Cary Millsap
http://www.method-r.com/downloads/doc_details/44-thinking-clearly-
about-performance
TroubleShooting Oracle Performance
By Christian Antognini
性能诊断艺术
Translated By 冯大辉/胡怡文/童家旺
DTCC2011DTCC2011
招聘
资深DBA
数据库架构师
运维架构师
详细信息请参见
http://job.alipay.com/recruit/index.htm
DTCC2011DTCC2011
Q & A
DTCC2011DTCC2011