设计高性能MySQL应用

     邹德平(@半瓶)
  http://www.banping.com
 http://weibo.com/banping
About me
•   邹德平(半瓶 & banping)
•   架构师@热推网(RT.CN)
•   《Oracle DBA手记》作者之一
•   ITPUB论坛MySQL和NoSQL版版主
•   ACOUG Member / TechClub Founder
•   Blog : http://www.banping.com
About TechClub
• IT技术沙龙
• 兴趣、分享、公益
• http://www.tech-club.org
• https://groups.google.com/group/tech-club-
  org
• TechClub@weibo
• TechCluber@twitter
• QQ群:79207910
议程
•   基础工作
•   规划你的数据
•   程序员的工具
•   设计表结构
•   SQL和索引
•   讨论
基础工作
•   发行版
•   表的存储引擎
•   表的字符集
•   部署架构
•   事务的隔离级别 / binlog_format
•   充分利用服务器资源
规划你的数据
•   以用户体验为出发点
•   海量数据 VS 海量访问
•   数据的生命周期
•   数据的关联性
•   CAP理论之ACID与BASE
•   Scale up & Scale out
•   Sharding 策略
程序员的工具
• Explain
• Profiling
• procedure analyse()
设计表结构
• 表是什么
• 列的设计
• 数据类型
表是什么
列的设计
•   面向SQL
•   主键
•   使用频率
•   存储容量
•   冗余
数据类型
•   Simple is beauty
•   int优先
•   varchar(N)
•   decimal VS float & double
•   timestamp VS datetime
•   IP : inet_aton() & inet_ntoa()
•   enum & set
•   拒绝NULL
SQL和索引
• 制定SQL规范
• 设计高效的分页
制定SQL规范
•   绑定变量
•   尽量使用索引
•   只取你需要的数据
•   批量写入和更新
•   避免隐性数据类型转换
•   尽可能避免join
•   尽可能减少查询的次数
如何分页
• Select count(*) from message;
• Select * from message order by id desc limit
  1000,20;
这不仅仅是技术问题
Limit offset,N的弊端
去除offset
优化Count
一个例子
Procedure analyse()
索引
Cover index
归根到底都是存储
profiling
Show profile for query
设计更合理的Count




          Copyright timyang
讨论
•   前期的设计胜过后期的优化
•   不要在数据库的范畴内解决所有的问题
•   从数据库的设计中学习系统设计
•   提问及研讨

设计高性能mysql应用-TechClub技术沙龙