osc scott linux下的数据库优化for_postgresql

31
Linux下的数据库优化 for PostgreSQL 演讲:萧少聪 邮箱:[email protected]

Upload: opensourcecamp

Post on 18-May-2015

2.014 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Osc scott linux下的数据库优化for_postgresql

Linux下的数据库优化for PostgreSQL

演讲:萧少聪邮箱:[email protected]

Page 2: Osc scott linux下的数据库优化for_postgresql

内 容

自我介绍

Linux操作系统优化

PostgerSQL数据库优化

Postgres-XC项目预览

Page 3: Osc scott linux下的数据库优化for_postgresql

自我介绍

程序 -> 系统 -> 数据库

红帽认证架构师 RHCA

参与篇写《Linux系统案例精解》

EnterpriseDB数据库产品经理

凑备enterprisedb.org.cn社区

深信开源可以带来真正的IT革命

Page 4: Osc scott linux下的数据库优化for_postgresql

操作系统优化

Page 5: Osc scott linux下的数据库优化for_postgresql

操作系统优化思路

问题重现,理解运作过程

性能监控

top vmstat free ps iostat sar ...

nmon mrtg cacti nagios ...

分析及记录

输出重定向 日志 图表(Gnuplot、Excel)...

性能调整

对比 -> 性能调整

Page 6: Osc scott linux下的数据库优化for_postgresql

问题重现,理解运作过程

流程图

便于了解运作过程

各关键环节负责人

问题预测

问题重现

设计测试流程

Page 7: Osc scott linux下的数据库优化for_postgresql

性能监控及分析工具

ntp 时间服务

top free 实时的系统情况

iostat sar 系统运行记录

Gnuplot

nmon

Page 8: Osc scott linux下的数据库优化for_postgresql

iostat sar

[root@server1 ~]# ls –l /var/log/sa

总计 10428-rw-r--r-- 1 scott scott 580848 2008-11-01 sa01-rw-r--r-- 1 scott scott 577424 2008-11-02 sa02-rw-r--r-- 1 scott scott 580848 2008-11-03 sa03-rw-r--r-- 1 scott scott 580848 2008-11-04 sa04-rw-r--r-- 1 scott scott 578672 2008-10-28 sa28-rw-r--r-- 1 scott scott 580848 2008-10-29 sa29-rw-r--r-- 1 scott scott 580848 2008-10-30 sa30-rw-r--r-- 1 scott scott 580848 2008-10-31 sa31-rw-r--r-- 1 scott scott 633705 2008-11-01 sar01-rw-r--r-- 1 scott scott 614991 2008-11-02 sar02-rw-r--r-- 1 scott scott 633714 2008-11-03 sar03-rw-r--r-- 1 scott scott 628709 2008-10-27 sar27-rw-r--r-- 1 scott scott 620661 2008-10-28 sar28-rw-r--r-- 1 scott scott 633705 2008-10-29 sar29-rw-r--r— 1 scott scott 633705 2008-10-30 sar30-rw-r--r-- 1 scott scott 633705 2008-10-31 sar31

[root@server1 ~]# sar -b -f /var/log/sa/sa04Linux 2.6.18-92.el5 (rac1) 10/05/2008

12:00:01 PM tps rtps wtps bread/s bwrtn/s12:10:01 PM 423.63 29.29 394.34 637.68 3852.1212:20:01 PM 419.81 24.26 395.55 495.33 3881.8012:30:01 PM 421.92 33.01 388.90 777.04 3849.2812:40:01 PM 420.26 30.92 389.34 712.69 3879.17

Page 9: Osc scott linux下的数据库优化for_postgresql

Gnuplot (1)

[root@server1 ~]# LANG=C sar -P ALL -f /var/log/sa/sa04 | awk '/^[012]/ {print $0}' | grep all > cpu.dat[root@server1 ~]# LANG=C sar -b -f /var/log/sa/sa04 | awk '/^[012]/ {print $0}' > io.dat此处生成了两个规则的数据文件,以cpu.dat为例说明此组合命令:LANG=C 以24小时为时间统计标准sar -P ALL -f /var/log/sa/sa04 获得所有CPU负载数据awk '/^[012]/ {print $0}' 过滤出以0,1,2开始的行grep all 过滤出带有all字眼的行

[root@server1 ~]# gnuplot gnuplot> set xdata timegnuplot> set timefmt "%H:%M:%S"gnuplot> set xlabel "TIME"gnuplot> set ylabel "CPU/IO"set xdata time 配置当前X轴的数据为时间set timefmt “%H:%M:%S” 配置时间的显示格式set xlabel “TIME” 命名X轴为“TIME”set xlabel “CPU/IO” 命名X轴为“CPU/IO”

通过cpu.dat文件的第3列输入数据,图表名字为CPU,以直线方式连接数据点,并生成图表gnuplot> plot "cpu.dat" using 1:($3) title "CPU" with lines

Page 10: Osc scott linux下的数据库优化for_postgresql

Gnuplot (2)

通过io.dat文件的第6列并将值缩小10倍输入数据,图表名字为IO,以直线方式连接数据点,并生成图表叠加到原有图表中gnuplot> replot "io.dat" using 1:($3/10) title "IO" with lines

Page 11: Osc scott linux下的数据库优化for_postgresql

nmon

Page 12: Osc scott linux下的数据库优化for_postgresql

性能调整 (总则)

最常见的性能瓶颈

I/O,包括:磁盘、文件系统、网络

内存不足,导致swap

CPU处理

应用程序问题

Page 13: Osc scott linux下的数据库优化for_postgresql

性能调整 (磁盘I/O)

优化思路

处理能力:转速、缓存、带宽

运作方式:减少读写混合,减少寻道时间

Page 14: Osc scott linux下的数据库优化for_postgresql

性能调整 (磁盘I/O)

/sys/block/<device>/queue/scheduler

CFQ:公平算法,适用于大部份系统应用

Deadline:适用于数据库等块操作应用

Anticipaory:适用于文件服务器ftp/samba等

NOOP:适用于带有TCQ的磁盘

Page 15: Osc scott linux下的数据库优化for_postgresql

性能调整 (文件系统I/O)

noatime:不更新的访问时间

commit:数据同步间隔时间

data:日志模式

ordered

writeback

journal

mount /dev/sdaX -o noatime,commit=N,data=writeback

Page 16: Osc scott linux下的数据库优化for_postgresql

性能调整 (网络I/O)

Total buffers = Bandwidth * Delay TimeBandwidth 1000M网:1000(Mebibits) / 8(bit) = 125(MiB)

Delay Timeping网关:xxxxxxxxxxxx time=15 ms

125(MiB) * 0.015(s) = 1.875 (MiB) = 1966080(Bytes)

UDP优化参考修改文件/etc/sysctl.conf加入以下几行:net.ipv4.udp_rmem_min = 1966080net.core.rmem_default = 1966080net.core.rmem_max = 2949120net.ipv4.udp_wmem_min = 1966080net.core.wmem_default = 1966080net.core.wmem_max = 2949120

[root@server1 ~]# sysctl -p

TCP优化参考修改文件/etc/sysctl.conf加入以下几行:net.ipv4.tcp_rmem = 1966080 1966080 2949120net.ipv4.tcp_wmem = 1966080 1966080 2949120

[root@server1 ~]# sysctl -p

Page 17: Osc scott linux下的数据库优化for_postgresql

性能调整 (CPU、内存)

多核CPU性能优化

内存:chkconfig关闭不用的服务

IRQ[root@server1 ~]# cat /proc/interrupts

CPU0 CPU1 CPU2 CPU3……10: 22 0 0 203 IO-APIC-level eth0……[root@server1 ~]# chkconfig irqbalance off[root@server1 ~]# reboot[root@server1 ~]# cat /proc/interrupts

CPU0 CPU1 CPU2 CPU3……10: 210 0 0 0 IO-APIC-level eth0

Process[root@server1 ~]#taskset 0x00000001 -p 15748

Page 18: Osc scott linux下的数据库优化for_postgresql

PostgreSQL数据库优化

Page 19: Osc scott linux下的数据库优化for_postgresql

PostgreSQL优化思路

合理的参数配置

分散I/O操作

使用适合的索引

独特的数据类型

专业版本的增强

Page 20: Osc scott linux下的数据库优化for_postgresql

合理的参数配置

根据实际应用情况配置postgresql.conf

是否要求冗机时数据完全不丢失

在系统崩溃时可接受多少数据丢失

数据库的读写比例

……

不要期望由此可以得到巨大的性能提高

一个不错的优化说明:http://ruimemo.wordpress.com/2010/03/31/postgresql-performance-and-maintenance-(postgres-优化与维护/

Page 21: Osc scott linux下的数据库优化for_postgresql

分散I/O操作

独立的Data

独立的WAL

独立的index

独立的pg_log

表分区

Data WAL

Page 22: Osc scott linux下的数据库优化for_postgresql

使用适合的索引

B-tree、Hash、GiST、GIN

考虑以下问题你正在做短信系统,基于庞大的电话号码查询如:010-88888888

你在做财务系统,基于庞大的财务数据做查询

函数索引

对电话号码字段索引,md5(phone)

部份索引

只对6个月内的财务数据进行索引

Page 23: Osc scott linux下的数据库优化for_postgresql

独特的数据类型(1)

考虑以下问题

存储地区抵属关系/货品分类关系的表

存储IP数据的表

ltree数据类型

如:中国.北京.海淀

结合CLUSTER(簇)使用

网络数据类型

如:192.168.0.1/24 08:00:2b:01:02:03

Page 24: Osc scott linux下的数据库优化for_postgresql

独特的数据类型(2)

考虑以下问题

正在做物流系统,要判断各地路程及运费

正在做城市管理系统,要进行自定义规划

空间数据类型(GIS)

如:点、线、块、3D等

如:长度、面积、交点统计等

Page 25: Osc scott linux下的数据库优化for_postgresql

专业版本的增强--EnterpriseDB企业版

Infinite Cache

透明的数据库二级缓存(本服务器/添加节点)

可无限扩展二级缓存节点

性能可达近10倍提升

DynaTuneTM 自动性能调整

查询优化标记(Hints)select /*+ NO_INDEX */ empno from emp;

EDB*Loader导入

跳过SQL解析将格式数据直接导入数据库文件

Page 26: Osc scott linux下的数据库优化for_postgresql

Postgre-XC项目预览

Page 27: Osc scott linux下的数据库优化for_postgresql

什么是Postgres-XC

Multi-Master可扩展Postgres集群

NTT及EnterpriseDB公司合作开发(LGPL)

当前最新版本v0.9.2

性能:TPC-W(DBT-1)测试

测试环境:5台服务器

达到原PostgreSQL单机3.4X性能

V1.0版本预计2010年12月推出

Page 28: Osc scott linux下的数据库优化for_postgresql

Postgres-XC架构

Page 29: Osc scott linux下的数据库优化for_postgresql

Postgres-XC特性

Shared-nothing

读/写均可扩展,适用于各种OLTP环境

可通过任何Coordinator操作

支持PG-XC Servers间的分区及复制

可指定分区模式

Hash

Range(future)

Page 30: Osc scott linux下的数据库优化for_postgresql

性能表现

• 整体性能表现很好• 有很好的可扩展性能

- 当前版本可达10台Server以上• 相对单个PG-XC(PG-2)没有明显性能下将• 是什么影响了活加节点时的性能

- 添加服务器会使通信增加- 添加服务器会增加2阶段提交 (消耗极少)

* 以上的性能数据是最坏的情况,测试时PostgreSQL单机上的CPU使用率就达到了100%。如果在一般的测试场景下性能会更好。

Page 31: Osc scott linux下的数据库优化for_postgresql

谢谢!

演讲:萧少聪邮箱:[email protected]