Sep
18
2009
很长一段时间以来,总能听到或者看到“千万级”数据(库)这种叫法,无论是网上论坛帖子还是招聘要求里。这对我来说是个怪异的概念,猜不出是什么意思。看了网上多篇文章,似乎概念上大家约定俗成就是指数据量比较大的情况下,如何提高以保证返回结果的时间在用户可接受的范围内。恩,终于明了了,就是性能评测(benchmarking)中的响应时间(response time)嘛。套了个马甲。
第一份工作的时候在银行业,有些表每天的新增数据就有2.3亿条记录(拜大集中所赐),这还只是半个中国的数据。因此从一开始,对大规模数据的处理就是我工作的基本要求。前辈们总结处很多编程与操作的规定,以满足性能上的需要。这里的性能,更多的指在规定要求的时间内完成规定的事情。在这段时间,没有人会提“千万级数据”,因为它太少了。
后来进了DB2的老家IBM, 做起了DB2的技术支持,那些DB2的开发设计大牛来讲DB2的架构,各部分的实现原则与部分细节,终于从更深层次了解了数据库是如何操作数据的,如何计算,如何join, 优化器如何改写SQL并决定访问路径(Access Path - 话说这中文翻译还真是别扭).这背后其实是cost-based estimator, 一切皆以statistics统计数据作为输入,进过DB2优化器的数学模型,得到对结果集大小、CPU时间、I/O时间等等的预测,最后得到一个时间估计,选取估计值最小的方案。
每个人都希望自己的查询(query)有快又好,但这是要跟多种因素相关联的。除了优化器选择access path以外,跟DB系统参数,Bufferpool大小,Sortpool大小,CPU,IO性能都有关系。
当然,大部分个人用户不会考率为了数据库的性能而更换或者扩展CPU数量,换用高性能的企业级存储这样的要钱的方式,因此就省下压榨database系统了。因此在互联网建站和小型企业数据库上,才更多的提到“千万级”的概念 --世上本没这个词,提的人多了也就成了个概念吧。
网上一个不算新但是很详细和典型的帖子:http://www.xiaohui.com/dev/server/20070701-discuz-mysql-cpu-100-optimize.htm
一个典型的场景就是系统突然出现了一个状况(CPU high或者响应慢到不能忍受),于是要分步排除问题,首先系统IO方面时候遇到瓶颈--打开文件数量到上限了?cache出了问题?这个部分没问题了,再看看DB系统的配置参数如何?是不是bufferpool或者sortpool大小不合适所以导致了过多的page-in,page-out?
如果上两点都没有问题,初步可以排除环境方面的因素,接下来就可以关注在具体SQL query的性能了。
那么关注哪些SQL呢?常用的关系型数据库系统(DB2, Oracle也包括MySQL等等),都有工具或者命令抓到耗时最长的SQL query. 例如MySQL下你可以用show processlist;
找到了最慢的sql,有两个方向可以选择:A 从SQL Query涉及的object(table,view)出发分析结构上的欠缺如缺少必要的index等等;另一个方向是用explain得到slow SQL的access path, 看看是不是table scan了,发生在什么环节然后去做修改。
上面是最最通用的数据库调优(Performance tuning)的策略,基本宗旨还是我们创造条件去帮助优化器(DB optimizer)以得到最优解。相信大部分DBA都熟悉。接下来说一下一个极端情况,就是无论你怎么做,优化器认为的最优解在你的特例中实际上却是最糟糕的。我们不能神话优化器,没有一种算法是能够在所有情况下都是最好的,因此极少数的SQL在特定的data特性下,我们需要去“误导”甚至是“欺骗”优化器来的到我们想要的最优解--即便这个方案在优化器看来是很糟糕的。这就是access path hint, 名字可能略有差异但是DB2,Oracle等主流数据库系统都是支持的。我们可以用自己的大脑代替优化器来决定少数特殊query的access path.
啰嗦了这么多,其实是想说,被常常使用的“千万级数据(库)”的概念,其实跟技术没多大关系,只是一个模糊的对DB甚至是整个IT系统的性能要求,有时候可以理解为性能调优技术的要求。以后见到这个概念,知道背后是怎么一回事就行了--尤其是对HR和猎头这类技术盲来说。
P.S. 数据库还有另一个scaling方面的技术来提高性能,这个是架构方向,互联网企业用的比较多。以后有空再写一写。其实网上这样的文章很不少了。
no comments | tags: database, DB2, mysql, oracle, performance tuning | posted in 编程开发, 电脑网络, DB2
May
12
2008
这也是在电脑里的一个老图了。坦率地说我已经快忘记做过这个项目了。不过当我在看到这个图的时候,所有的记忆都恢复了。为什么呢?你自己看看嘛,思路和状态在这个图上在清晰不过了。
updated May 24,2008: 更新图片链接

2 comments | posted in 主机圈, DB2
May
12
2008
整理资料时候发现的很久之前做的东东。要不是换机器倒资料,不知道猴年马月才能重见天日了

no comments | tags: DB2, mindmap, Stored Procedure, z/os | posted in 主机圈, 编程开发, DB2
Aug
28
2007
明天在CRL 钻石大厦有Don Chamberlin的两个session. 一共2小时15分钟。Don Chamberlin何许人?他是SQL语言的两个发明者之一。现在的XQuery标准制定组里面也有他的一席之地。简单查了一下,他是IBM,IEEE,ACM的三料Fellow. 这么难得的机会,还是要去亲自听一听大师的现场的。
P.S. Wikipedia上面SQL的词条:
SQL, commonly expanded as Structured Query Language, is a computer language designed for the retrieval and management of data in relational database management systems, database schema creation and modification, and database object access control management.[1][2] SQL has been standardized by both ANSI and ISO.
The first version of SQL was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL, was designed to manipulate and retrieve data stored in IBM’s original relational database product, System R. The SQL language was later formally standardized by the American National Standards Institute in 1986. Subsequent versions of the SQL standard have been released as International Organization for Standardization (ISO) standards.
Originally designed as a declarative query and data manipulation language, variations of SQL have been created by SQL database management system (DBMS) vendors that add procedural constructs, control-of-flow statements, user-defined data types, and various other language extensions. With the release of the SQL:1999 standard, many such extensions were formally adopted as part of the SQL language via the SQL Persistent Stored Modules (SQL/PSM) portion of the standard.
Common criticisms of SQL include a percieved lack of cross-platform portability between vendors, inappropriate handling of missing data (see Null (SQL)), a complex three-valued logic system, and its complex and occasionally ambiguous language grammar and semantics.
no comments | posted in DB2, I公司I生活, 活在北京
May
25
2006
1.启动数据库
db2start
2.停止数据库
db2stop
3.连接数据库
db2 connect to o_yd user db2 using pwd
4.读数据库管理程序配置
db2 get dbm cfg
5.写数据库管理程序配置
db2 update dbm cfg using 参数名 参数值
6.读数据库的配置
db2 connect to o_yd user db2 using pwd
db2 get db cfg for o_yd
7.写数据库的配置
db2 connect to o_yd user db2 using pwd
db2 update db cfg for o_yd using 参数名 参数值
8.关闭所有应用连接
db2 force application all
db2 force application ID1,ID2,,,Idn MODE ASYNC
(db2 list application for db o_yd show detail)
9.备份数据库
db2 force application all
db2 backup db o_yd to d:
(db2 initialize tape on \\.\tape0)
(db2 rewind tape on \\.\tape0)
db2 backup db o_yd to \\.\tape0
10.恢复数据库
db2 restore db o_yd from d: to d:
db2 restore db o_yd from \\.\tape0 to d:
11.绑定存储过程
db2 connect to o_yd user db2 using pwd
db2 bind c:\dfplus.bnd
拷贝存储过程到服务器上的C:\sqllib\function目录中
12.整理表
db2 connect to o_yd user db2 using pwd
db2 reorg table ydd
db2 runstats on table ydd with distribution and indexes all
13.导出表数据
db2 export to c:\dftz.txt of del select * from dftz
db2 export to c:\dftz.ixf of ixf select * from dftz
14.导入表数据
import from c:\123.txt of del insert into ylbx.czyxx
db2 import to c:\dftz.txt of del commitcount 5000 messages c:\dftz.msg insert into dftz
import from vipmobiles.txt of del method p(1) insert into tgzvipmobiles(mobileno)
db2 import to c:\dftz.ixf of ixf commitcount 5000 messages c:\dftz.msg insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert_update into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 create into dftz (仅IXF)
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace_create into dftz (仅IXF)
15.执行一个批处理文件
db2 –tf 批处理文件名
(文件中每一条命令用 ;结束)
16.自动生成批处理文件
建文本文件:temp.sql
select ‘runstats on table DB2.’ || tabname || ‘ with distribution and detailed indexes all;’ from syscat.tables where tabschema=’DB2′ and type=’T';
db2 –tf temp.sql>runstats.sql
17.自动生成建表(视图)语句
在服务器上:C:\sqllib\misc目录中
db2 connect to o_yd user db2 using pwd
db2look –d o_yd –u db2 –e –p –c c:\o_yd.txt
18.其他命令
grant dbadm on database to user bb
19select * from czyxx fetch first 1 rows only
20db2look –d ylbx –u db2admin –w –asd –a –e –o a.txt21. 显示当前用户所有表
list tables
22.列出所有的系统表
list tables for system
23.查看表结构
db2 describe select * from user.tables
用于DB2数据移动的文件格式有四种:
1、ASC——非定界ASCII文件,是一个ASCII字符流。数据流中的行由行定界符分隔,而行中的每一列则通过起始和结束位置来定义。
2、 DEL——定界ASCII文件,也是一个ASCII字符流。数据流中的行由行定界符分隔,行中的列值由列定界符分隔。文件类型修饰符可用于修改这些定界符的默认值。
3、 WSF——(work sheet format)为工作表格式,用于与Lotus系列的软件进行数据交换。
4、PC/IXF——是集成交换格式(Integration Exchange Format,IXF)数据交换体系结构的改编版本,由一些列可变长度的记录构成,包括头记录、表记录、表中每列的列描述符记录以及表中每行的一条或多条数据记录。PC/IXF 文件记录由包含了字符数据的字段组成。
no comments | posted in 电脑网络, DB2