SlideShare uma empresa Scribd logo
1 de 30
我对后端优化的一点想法
About Me 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
About Me(2) 《Oracle性能诊断艺术》 与冯大辉(Fenng),胡怡文合译 《Oracle Performance Survival Guide》 与郑勇斌,胡怡文合作翻译中
内容简介 什么是优化? 响应时间 Vs 吞吐量 Instrument & metrics 需要了解的一点硬件知识 常见案例分析 引用资料
什么是优化(1) 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
什么是优化(2) 不访问不必要的数据 使用B*Tree/hash等方法定位必要的数据 使用column Store或分表的方式将数据分开存储 使用Bloom filter算法排除空值查询 合理的利用硬件来提升访问效率 使用缓存消除对数据的重复访问 使用批量处理来减少磁盘的Seek操作 使用批量处理来减少网络的Round Trip 使用SSD来提升磁盘访问效率
响应时间 Vs 吞吐量(1) 性能 衡量完成特定任务的速度或效率 响应时间 衡量系统与用户交互式多久能够发出响应 吞吐量 衡量系统在单位时间里可以完成的任务量
响应时间 Vs 吞吐量 Response Time = Service Time + Queue Time 经典的响应时间曲线.到达率为1.55trx/s,响应时间为3ms/Trx,服务时间为2ms/Trx,排队时间为1ms/trx 图片引用自:《Forecasting Oracle Performance》, Chapter 2 P44,figure 2-1
Instrument & Metrics What gets measured gets managed. Peter Drucker (彼得. 德鲁克 ) Don't guess, get the data Anonymous
Instrument & Metrics@life 从杭州北京(花费了6个半小时)
Instrument & Metrics@life 从杭州北京(花费了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分钟)
Instrument & Metrics@Oracle 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
Instrument & Metrics@Linux 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
Numbers you  Should Know 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
传统磁盘的访问特性 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 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.709ms 一次IO顺序读取1MB数据的延时 Transfer Time = 1MB / ( 3Gb/s) = 2.688ms Total Time = RT + TT = 4.709 ms IOPS(次数) 1.来自Seagate介绍
B*Tree优化数据访问 索引键的选择性(唯一性) 索引键的顺序 索引对应基础数据的更新频度 等值检索 Vs 范围检索 导致索引失效的场景 Index Access Vs Index Filter Vs Table Filter Richard Foote的Blog http://richardfoote.wordpress.com/ 与 TapioLahdenmaki的书《Relational Database Index Design and the Optimizers》
B*Tree优化数据访问 模拟场景 表中有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,rownumrn 	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_createdesc ) 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);
B*Tree优化数据访问
分表存储来提高性能 场景介绍 表VeryBigTable含有30个列 表的记录数为50,000,000条 平均每个用户为300条左右 其中有2个列属于详细描述字段,平均长度为2k 其它的列的总长度平均为250个字节 此表上的查询有两种模式 列出表中的主要信息(每次20条,不包含详细信息,90%的查询) 查看记录的详细信息(10%的查询) 保存在Oracle数据库中,默认block size(8k) 要求 对此业务进行优化 分析数据,说服开发部门实施此优化
分表存储来提高性能(2) 性能分析 每块记录数 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 右上标的内容请参见注释
使用缓存优化重复查询 场景 Read Intensive (R/W 20倍以上) 业务可接受部分延迟(Delay) 每天访问量上亿次 系统IO压力巨大(本地内存无法容纳活跃数据) 要求 优化业务
使用缓存优化重复查询 方案 使用缓存来减少应用对后端的访问 注意事项 考虑缓存的刷新策略 考虑缓存的数据延迟对业务的影响 考虑缓存失效时,系统的支撑能力 参考缓存工具: MemCached, Tair, Redis
空查询优化 场景分析 单词拼写检查 有20万单词条目 每个条目平均长度50字节 单词条目会动态增加,但变更量很小 目前单词条目存储在数据库中 每天1亿次查询,其中99%的查询为空查 现在通过read through的缓存进行处理 但是仍然有80%的查询(都为空查询)会访问后端 要求 对此业务进行优化 分析方案的投入产出
空查询优化(2) 方案 使用全量缓存 将所有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%的空查.
Bloom filter技术介绍 加入字符串过程  首先是将字符串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
批量处理接口 使用不同的批次大小从数据库取出20万条记录
批量处理的注意事项 在有批量需求的情况下,尽可能提供批量处理接口,如memcached的multiget,或者cassandra的getslice,针对RDBMS,可以使用批量接口(如Oracle的Array Interface). 不要设置过大的批次大小 这需要与对应的程序heap 空间 设置过大可能会导致程序出现OOM错误 网络send/receive_buffer大小(/proc/sys/net/core/wmem|rmem_max|default)
Recommended Reading 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 TapioLahdenmaki 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 冯大辉/胡怡文/童家旺
We Are Hiring 招聘 资深DBA 数据库架构师 运维架构师 详细信息请参见 http://job.alipay.com/recruit/index.htm
谢谢 Q & A

Mais conteúdo relacionado

Mais procurados

Mongo db架构之优先方案
Mongo db架构之优先方案Mongo db架构之优先方案
Mongo db架构之优先方案Lucien Li
 
Oracle Data Buffer Cache
Oracle Data Buffer CacheOracle Data Buffer Cache
Oracle Data Buffer CacheSky Jian
 
淘宝前台系统优化实践“吞吐量优化”-Qcon2011
淘宝前台系统优化实践“吞吐量优化”-Qcon2011淘宝前台系统优化实践“吞吐量优化”-Qcon2011
淘宝前台系统优化实践“吞吐量优化”-Qcon2011Yiwei Ma
 
阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化guiyingshenxia
 
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410maclean liu
 
Http cache 的優化
Http cache 的優化Http cache 的優化
Http cache 的優化振揚 陳
 
高效能執行緒
高效能執行緒高效能執行緒
高效能執行緒Rick Wu
 
几种Nosql介绍
几种Nosql介绍几种Nosql介绍
几种Nosql介绍taotao1240
 
数据库极限性能测试
数据库极限性能测试数据库极限性能测试
数据库极限性能测试helbreathszw
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈Robbin Fan
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享晓锋 陈
 
Squid安装配置
Squid安装配置Squid安装配置
Squid安装配置Yiwei Ma
 
高性能No sql数据库redis
高性能No sql数据库redis高性能No sql数据库redis
高性能No sql数据库redispaitoubing
 
SSDB 入门基础
SSDB 入门基础SSDB 入门基础
SSDB 入门基础ideawu
 
Mysql handlersocket
Mysql handlersocketMysql handlersocket
Mysql handlersocketpwesh
 
基于linux-HA 的PG高可用性
基于linux-HA 的PG高可用性基于linux-HA 的PG高可用性
基于linux-HA 的PG高可用性babyyellow li
 
Redis 存储分片之代理服务twemproxy 测试
Redis 存储分片之代理服务twemproxy 测试Redis 存储分片之代理服务twemproxy 测试
Redis 存储分片之代理服务twemproxy 测试kaerseng
 

Mais procurados (18)

Optimzing mysql
Optimzing mysqlOptimzing mysql
Optimzing mysql
 
Mongo db架构之优先方案
Mongo db架构之优先方案Mongo db架构之优先方案
Mongo db架构之优先方案
 
Oracle Data Buffer Cache
Oracle Data Buffer CacheOracle Data Buffer Cache
Oracle Data Buffer Cache
 
淘宝前台系统优化实践“吞吐量优化”-Qcon2011
淘宝前台系统优化实践“吞吐量优化”-Qcon2011淘宝前台系统优化实践“吞吐量优化”-Qcon2011
淘宝前台系统优化实践“吞吐量优化”-Qcon2011
 
阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化
 
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410
【Maclean liu技术分享】开oracle调优鹰眼,深入理解awr性能报告 第二讲 正式版 20130410
 
Http cache 的優化
Http cache 的優化Http cache 的優化
Http cache 的優化
 
高效能執行緒
高效能執行緒高效能執行緒
高效能執行緒
 
几种Nosql介绍
几种Nosql介绍几种Nosql介绍
几种Nosql介绍
 
数据库极限性能测试
数据库极限性能测试数据库极限性能测试
数据库极限性能测试
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
 
Squid安装配置
Squid安装配置Squid安装配置
Squid安装配置
 
高性能No sql数据库redis
高性能No sql数据库redis高性能No sql数据库redis
高性能No sql数据库redis
 
SSDB 入门基础
SSDB 入门基础SSDB 入门基础
SSDB 入门基础
 
Mysql handlersocket
Mysql handlersocketMysql handlersocket
Mysql handlersocket
 
基于linux-HA 的PG高可用性
基于linux-HA 的PG高可用性基于linux-HA 的PG高可用性
基于linux-HA 的PG高可用性
 
Redis 存储分片之代理服务twemproxy 测试
Redis 存储分片之代理服务twemproxy 测试Redis 存储分片之代理服务twemproxy 测试
Redis 存储分片之代理服务twemproxy 测试
 

Destaque

数据库系统设计漫谈
数据库系统设计漫谈数据库系统设计漫谈
数据库系统设计漫谈james tong
 
Enqueue Lock介绍.ppt
Enqueue Lock介绍.pptEnqueue Lock介绍.ppt
Enqueue Lock介绍.pptjames tong
 
The right read optimization is actually write optimization
The right read optimization is actually write optimizationThe right read optimization is actually write optimization
The right read optimization is actually write optimizationjames tong
 
Understanding performance through_measurement
Understanding performance through_measurementUnderstanding performance through_measurement
Understanding performance through_measurementjames tong
 
My sql ssd-mysqluc-2012
My sql ssd-mysqluc-2012My sql ssd-mysqluc-2012
My sql ssd-mysqluc-2012james tong
 
Evaluating ha alternatives my sql tutorial2
Evaluating ha alternatives my sql tutorial2Evaluating ha alternatives my sql tutorial2
Evaluating ha alternatives my sql tutorial2james tong
 
HTML5 game dev with three.js - HexGL
HTML5 game dev with three.js - HexGLHTML5 game dev with three.js - HexGL
HTML5 game dev with three.js - HexGLThibaut Despoulain
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascriptjay li
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 
One minute manager
One minute managerOne minute manager
One minute managerDuy Do Phan
 
Qualities of good manager
Qualities of good managerQualities of good manager
Qualities of good managerLiven Varghese
 
Story of one minute manager
Story of one minute managerStory of one minute manager
Story of one minute managerShimranz Skillls
 
The one-minute-manager
The one-minute-managerThe one-minute-manager
The one-minute-managerVikram Singh
 
How To Be A Good Manager
How To Be A Good ManagerHow To Be A Good Manager
How To Be A Good Manageraumashankar
 
Key qualities and skills of a good manager
Key qualities and skills of a good managerKey qualities and skills of a good manager
Key qualities and skills of a good managerJerry Jia
 
QUALITIES OF A GOOD MANAGER
QUALITIES OF A GOOD MANAGERQUALITIES OF A GOOD MANAGER
QUALITIES OF A GOOD MANAGERMVR NIRANJAN
 
Checklist for the Agile Manager
Checklist for the Agile ManagerChecklist for the Agile Manager
Checklist for the Agile ManagerJurgen Appelo
 
The One Minute Manager by Muhammad Akram
The One Minute Manager by Muhammad AkramThe One Minute Manager by Muhammad Akram
The One Minute Manager by Muhammad AkramMuhammad Akram
 

Destaque (20)

数据库系统设计漫谈
数据库系统设计漫谈数据库系统设计漫谈
数据库系统设计漫谈
 
Enqueue Lock介绍.ppt
Enqueue Lock介绍.pptEnqueue Lock介绍.ppt
Enqueue Lock介绍.ppt
 
The right read optimization is actually write optimization
The right read optimization is actually write optimizationThe right read optimization is actually write optimization
The right read optimization is actually write optimization
 
Understanding performance through_measurement
Understanding performance through_measurementUnderstanding performance through_measurement
Understanding performance through_measurement
 
My sql ssd-mysqluc-2012
My sql ssd-mysqluc-2012My sql ssd-mysqluc-2012
My sql ssd-mysqluc-2012
 
Evaluating ha alternatives my sql tutorial2
Evaluating ha alternatives my sql tutorial2Evaluating ha alternatives my sql tutorial2
Evaluating ha alternatives my sql tutorial2
 
HTML5 game dev with three.js - HexGL
HTML5 game dev with three.js - HexGLHTML5 game dev with three.js - HexGL
HTML5 game dev with three.js - HexGL
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascript
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
One minute manager
One minute managerOne minute manager
One minute manager
 
Good Manager, True Leader
Good Manager, True LeaderGood Manager, True Leader
Good Manager, True Leader
 
Qualities of good manager
Qualities of good managerQualities of good manager
Qualities of good manager
 
Story of one minute manager
Story of one minute managerStory of one minute manager
Story of one minute manager
 
The one-minute-manager
The one-minute-managerThe one-minute-manager
The one-minute-manager
 
How To Be A Good Manager
How To Be A Good ManagerHow To Be A Good Manager
How To Be A Good Manager
 
Key qualities and skills of a good manager
Key qualities and skills of a good managerKey qualities and skills of a good manager
Key qualities and skills of a good manager
 
QUALITIES OF A GOOD MANAGER
QUALITIES OF A GOOD MANAGERQUALITIES OF A GOOD MANAGER
QUALITIES OF A GOOD MANAGER
 
One Minute Manager
One Minute ManagerOne Minute Manager
One Minute Manager
 
Checklist for the Agile Manager
Checklist for the Agile ManagerChecklist for the Agile Manager
Checklist for the Agile Manager
 
The One Minute Manager by Muhammad Akram
The One Minute Manager by Muhammad AkramThe One Minute Manager by Muhammad Akram
The One Minute Manager by Muhammad Akram
 

Semelhante a 我对后端优化的一点想法.pptx

阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化colderboy17
 
Osc scott linux下的数据库优化for_postgresql
Osc scott linux下的数据库优化for_postgresqlOsc scott linux下的数据库优化for_postgresql
Osc scott linux下的数据库优化for_postgresqlOpenSourceCamp
 
Mysql遇到的一些问题
Mysql遇到的一些问题Mysql遇到的一些问题
Mysql遇到的一些问题wang tongchao
 
淘宝前端优化
淘宝前端优化淘宝前端优化
淘宝前端优化锐 张
 
Performance Data Analyze
Performance Data AnalyzePerformance Data Analyze
Performance Data Analyzeanysql
 
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析maclean liu
 
Mybatis学习培训
Mybatis学习培训Mybatis学习培训
Mybatis学习培训flynofry
 
Basic oracle for developer&beginner
Basic oracle for developer&beginnerBasic oracle for developer&beginner
Basic oracle for developer&beginnermaclean liu
 
数据库性能诊断的七种武器
数据库性能诊断的七种武器数据库性能诊断的七种武器
数据库性能诊断的七种武器Leyi (Kamus) Zhang
 
Linux性能监控cpu内存io网络
Linux性能监控cpu内存io网络Linux性能监控cpu内存io网络
Linux性能监控cpu内存io网络lovingprince58
 
Java线上应用问题排查方法和工具(空望)
Java线上应用问题排查方法和工具(空望)Java线上应用问题排查方法和工具(空望)
Java线上应用问题排查方法和工具(空望)ykdsg
 
淘宝商品库MySQL优化实践
淘宝商品库MySQL优化实践淘宝商品库MySQL优化实践
淘宝商品库MySQL优化实践Feng Yu
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUGYingSiang Geng
 
Pptv lb日志实时分析平台
Pptv lb日志实时分析平台Pptv lb日志实时分析平台
Pptv lb日志实时分析平台drewz lin
 
【Ask maclean技术分享】oracle dba技能列表 z
【Ask maclean技术分享】oracle dba技能列表 z【Ask maclean技术分享】oracle dba技能列表 z
【Ask maclean技术分享】oracle dba技能列表 zmaclean liu
 
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構Etu Solution
 
MongoDB at Qihoo 360
MongoDB at Qihoo 360MongoDB at Qihoo 360
MongoDB at Qihoo 360MongoDB
 
腾讯大讲堂19 系统优化的方向
腾讯大讲堂19 系统优化的方向腾讯大讲堂19 系统优化的方向
腾讯大讲堂19 系统优化的方向George Ang
 

Semelhante a 我对后端优化的一点想法.pptx (20)

阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化阿里巴巴 叶正盛 数据库性能量化
阿里巴巴 叶正盛 数据库性能量化
 
Osc scott linux下的数据库优化for_postgresql
Osc scott linux下的数据库优化for_postgresqlOsc scott linux下的数据库优化for_postgresql
Osc scott linux下的数据库优化for_postgresql
 
Mysql遇到的一些问题
Mysql遇到的一些问题Mysql遇到的一些问题
Mysql遇到的一些问题
 
淘宝前端优化
淘宝前端优化淘宝前端优化
淘宝前端优化
 
Performance Data Analyze
Performance Data AnalyzePerformance Data Analyze
Performance Data Analyze
 
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析
【Ask maclean技术分享oracle数据库优化】awr鹰眼系列awr报告全面指标分析
 
Mybatis学习培训
Mybatis学习培训Mybatis学习培训
Mybatis学习培训
 
Basic oracle for developer&beginner
Basic oracle for developer&beginnerBasic oracle for developer&beginner
Basic oracle for developer&beginner
 
数据库性能诊断的七种武器
数据库性能诊断的七种武器数据库性能诊断的七种武器
数据库性能诊断的七种武器
 
Glider
GliderGlider
Glider
 
Linux性能监控cpu内存io网络
Linux性能监控cpu内存io网络Linux性能监控cpu内存io网络
Linux性能监控cpu内存io网络
 
Java线上应用问题排查方法和工具(空望)
Java线上应用问题排查方法和工具(空望)Java线上应用问题排查方法和工具(空望)
Java线上应用问题排查方法和工具(空望)
 
淘宝商品库MySQL优化实践
淘宝商品库MySQL优化实践淘宝商品库MySQL优化实践
淘宝商品库MySQL优化实践
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
 
Pptv lb日志实时分析平台
Pptv lb日志实时分析平台Pptv lb日志实时分析平台
Pptv lb日志实时分析平台
 
【Ask maclean技术分享】oracle dba技能列表 z
【Ask maclean技术分享】oracle dba技能列表 z【Ask maclean技术分享】oracle dba技能列表 z
【Ask maclean技术分享】oracle dba技能列表 z
 
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構
Track A-3 Enterprise Data Lake in Action - 搭建「活」的企業 Big Data 生態架構
 
MongoDB at Qihoo 360
MongoDB at Qihoo 360MongoDB at Qihoo 360
MongoDB at Qihoo 360
 
2016 nas 年會簡報
2016 nas 年會簡報2016 nas 年會簡報
2016 nas 年會簡報
 
腾讯大讲堂19 系统优化的方向
腾讯大讲堂19 系统优化的方向腾讯大讲堂19 系统优化的方向
腾讯大讲堂19 系统优化的方向
 

Mais de james tong

Migrating from MySQL to PostgreSQL
Migrating from MySQL to PostgreSQLMigrating from MySQL to PostgreSQL
Migrating from MySQL to PostgreSQLjames tong
 
Oracle 性能优化
Oracle 性能优化Oracle 性能优化
Oracle 性能优化james tong
 
Cap 理论与实践
Cap 理论与实践Cap 理论与实践
Cap 理论与实践james tong
 
Scalable system operations presentation
Scalable system operations presentationScalable system operations presentation
Scalable system operations presentationjames tong
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...james tong
 
Stability patterns presentation
Stability patterns presentationStability patterns presentation
Stability patterns presentationjames tong
 
Troubleshooting mysql-tutorial
Troubleshooting mysql-tutorialTroubleshooting mysql-tutorial
Troubleshooting mysql-tutorialjames tong
 
设计可扩展的Oracle应用
设计可扩展的Oracle应用设计可扩展的Oracle应用
设计可扩展的Oracle应用james tong
 
Oracle数据库体系结构简介.ppt
Oracle数据库体系结构简介.pptOracle数据库体系结构简介.ppt
Oracle数据库体系结构简介.pptjames tong
 
Cassandra简介.ppt
Cassandra简介.pptCassandra简介.ppt
Cassandra简介.pptjames tong
 

Mais de james tong (10)

Migrating from MySQL to PostgreSQL
Migrating from MySQL to PostgreSQLMigrating from MySQL to PostgreSQL
Migrating from MySQL to PostgreSQL
 
Oracle 性能优化
Oracle 性能优化Oracle 性能优化
Oracle 性能优化
 
Cap 理论与实践
Cap 理论与实践Cap 理论与实践
Cap 理论与实践
 
Scalable system operations presentation
Scalable system operations presentationScalable system operations presentation
Scalable system operations presentation
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...
 
Stability patterns presentation
Stability patterns presentationStability patterns presentation
Stability patterns presentation
 
Troubleshooting mysql-tutorial
Troubleshooting mysql-tutorialTroubleshooting mysql-tutorial
Troubleshooting mysql-tutorial
 
设计可扩展的Oracle应用
设计可扩展的Oracle应用设计可扩展的Oracle应用
设计可扩展的Oracle应用
 
Oracle数据库体系结构简介.ppt
Oracle数据库体系结构简介.pptOracle数据库体系结构简介.ppt
Oracle数据库体系结构简介.ppt
 
Cassandra简介.ppt
Cassandra简介.pptCassandra简介.ppt
Cassandra简介.ppt
 

我对后端优化的一点想法.pptx

  • 2. About Me 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
  • 3. About Me(2) 《Oracle性能诊断艺术》 与冯大辉(Fenng),胡怡文合译 《Oracle Performance Survival Guide》 与郑勇斌,胡怡文合作翻译中
  • 4. 内容简介 什么是优化? 响应时间 Vs 吞吐量 Instrument & metrics 需要了解的一点硬件知识 常见案例分析 引用资料
  • 5. 什么是优化(1) 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
  • 6. 什么是优化(2) 不访问不必要的数据 使用B*Tree/hash等方法定位必要的数据 使用column Store或分表的方式将数据分开存储 使用Bloom filter算法排除空值查询 合理的利用硬件来提升访问效率 使用缓存消除对数据的重复访问 使用批量处理来减少磁盘的Seek操作 使用批量处理来减少网络的Round Trip 使用SSD来提升磁盘访问效率
  • 7. 响应时间 Vs 吞吐量(1) 性能 衡量完成特定任务的速度或效率 响应时间 衡量系统与用户交互式多久能够发出响应 吞吐量 衡量系统在单位时间里可以完成的任务量
  • 8. 响应时间 Vs 吞吐量 Response Time = Service Time + Queue Time 经典的响应时间曲线.到达率为1.55trx/s,响应时间为3ms/Trx,服务时间为2ms/Trx,排队时间为1ms/trx 图片引用自:《Forecasting Oracle Performance》, Chapter 2 P44,figure 2-1
  • 9. Instrument & Metrics What gets measured gets managed. Peter Drucker (彼得. 德鲁克 ) Don't guess, get the data Anonymous
  • 10. Instrument & Metrics@life 从杭州北京(花费了6个半小时)
  • 11. Instrument & Metrics@life 从杭州北京(花费了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分钟)
  • 12. Instrument & Metrics@Oracle 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
  • 13. Instrument & Metrics@Linux 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
  • 14. Numbers you Should Know 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
  • 15. 传统磁盘的访问特性 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 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.709ms 一次IO顺序读取1MB数据的延时 Transfer Time = 1MB / ( 3Gb/s) = 2.688ms Total Time = RT + TT = 4.709 ms IOPS(次数) 1.来自Seagate介绍
  • 16. B*Tree优化数据访问 索引键的选择性(唯一性) 索引键的顺序 索引对应基础数据的更新频度 等值检索 Vs 范围检索 导致索引失效的场景 Index Access Vs Index Filter Vs Table Filter Richard Foote的Blog http://richardfoote.wordpress.com/ 与 TapioLahdenmaki的书《Relational Database Index Design and the Optimizers》
  • 17. B*Tree优化数据访问 模拟场景 表中有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,rownumrn 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_createdesc ) 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);
  • 19. 分表存储来提高性能 场景介绍 表VeryBigTable含有30个列 表的记录数为50,000,000条 平均每个用户为300条左右 其中有2个列属于详细描述字段,平均长度为2k 其它的列的总长度平均为250个字节 此表上的查询有两种模式 列出表中的主要信息(每次20条,不包含详细信息,90%的查询) 查看记录的详细信息(10%的查询) 保存在Oracle数据库中,默认block size(8k) 要求 对此业务进行优化 分析数据,说服开发部门实施此优化
  • 20. 分表存储来提高性能(2) 性能分析 每块记录数 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 右上标的内容请参见注释
  • 21. 使用缓存优化重复查询 场景 Read Intensive (R/W 20倍以上) 业务可接受部分延迟(Delay) 每天访问量上亿次 系统IO压力巨大(本地内存无法容纳活跃数据) 要求 优化业务
  • 22. 使用缓存优化重复查询 方案 使用缓存来减少应用对后端的访问 注意事项 考虑缓存的刷新策略 考虑缓存的数据延迟对业务的影响 考虑缓存失效时,系统的支撑能力 参考缓存工具: MemCached, Tair, Redis
  • 23. 空查询优化 场景分析 单词拼写检查 有20万单词条目 每个条目平均长度50字节 单词条目会动态增加,但变更量很小 目前单词条目存储在数据库中 每天1亿次查询,其中99%的查询为空查 现在通过read through的缓存进行处理 但是仍然有80%的查询(都为空查询)会访问后端 要求 对此业务进行优化 分析方案的投入产出
  • 24. 空查询优化(2) 方案 使用全量缓存 将所有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%的空查.
  • 25. Bloom filter技术介绍 加入字符串过程 首先是将字符串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
  • 27. 批量处理的注意事项 在有批量需求的情况下,尽可能提供批量处理接口,如memcached的multiget,或者cassandra的getslice,针对RDBMS,可以使用批量接口(如Oracle的Array Interface). 不要设置过大的批次大小 这需要与对应的程序heap 空间 设置过大可能会导致程序出现OOM错误 网络send/receive_buffer大小(/proc/sys/net/core/wmem|rmem_max|default)
  • 28. Recommended Reading 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 TapioLahdenmaki 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 冯大辉/胡怡文/童家旺
  • 29. We Are Hiring 招聘 资深DBA 数据库架构师 运维架构师 详细信息请参见 http://job.alipay.com/recruit/index.htm