php开源嘛
首页 | English | MySql数据库 | Php编程 | 建站杂烩 | 系统服务器 | 资源共享 | 专题 | 才桀网 | 支持论坛
  当前位置:主页>MySql数据库>文章内容
用数据库命令对MySQL数据库进行优化...
来源:网络 作者:本站整理 发布时间:2007-11-09  

用数据库命令对MySQL数据库进行优化...phpma.com

我们讨论的是数据库性能优化的另一方面,即运用数据库服务器内建的工具辅助性能分析和优化。

         ▲ optimize

optimize能够恢复和整理磁盘空间以及数据碎片,一旦对包含变长行的表进行了大量的更新或者删除,进行这个操作就非常有必要了。optimize当前只能用于myisam和bdb表。

结束语:

从编译数据库服务器开始、贯穿整个管理过程,能够改善mysql性能的因素实在非常多,本文只涉及了其中很小的一部分。尽管如此,我们希望本文讨论的内容能够对你有所帮助。

var yahoocnadconfig=new array(); yahoocnadconfig['adid']=710 yahoocnadconfig['wid']=50397 yahoocnadconfig['w']=468 yahoocnadconfig['h']=60 var yahoocustconfig=new array(); yahoocustconfig['ad_width']=468 yahoocustconfig['ad_height']=60 yahoocustconfig['default_keyword_number']=8 yahoocustconfig['keyword_bg_color']='b99cdd' yahoocustconfig['keyword_fr_color']='ffffff' yahoocustconfig['border_color']='9c73cf'

phpma.com▲ show

执行下面这个命令可以了解服务器的运行状态:mysql >show status; phpma.com

该命令将显示出一长列状态变量及其对应的值,其中包括:被中止访问的用户数量,被中止的连接数量,尝试连接的次数,并发连接数量最大值,以及其他许多有用的信息。这些信息对于确定系统问题和效率低下的原因是十分有用的。

show命令除了能够显示出mysql服务器整体状态信息之外,它还能够显示出有关日志文件、指定数据库、表、索引、进程和许可权限表的宝贵信息。

▲ explain

explain能够分析select命令的处理过程。这不仅对于决定是否要为表加上索引很有用,而且对于了解mysql处理复杂连接的过程也很有用。

下面这个例子显示了如何用explain提供的信息逐步地优化连接查询。(本例来自mysql文档,见http://www.mysql.com/doc/e/x/explain.html。原文写到这里似乎有点潦草了事,特加上此例。)

假定用explain分析的select命令如下所示: phpma.com

explain select tt.ticketnumber, tt.timein,
      tt.projectreference, tt.estimatedshipdate,
      tt.actualshipdate, tt.clientid,
      tt.servicecodes, tt.repetitiveid,
      tt.currentprocess, tt.currentdpperson,
      tt.recordvolume, tt.dpprinted, et.country,
      et_1.country, do.custname
    from tt, et, et as et_1, do
    where tt.submittime is null
      and tt.actualpc = et.employid
      and tt.assignedpc = et_1.employid
      and tt.clientid = do.custnmbr;

select命令中出现的表定义如下:

表定义

表          列           列类型 
tt          actualpc      char(10) 
tt          assignedpc    char(10) 
tt          clientid      char(10) 
et          employid      char(15) 
do          custnmbr      char(15)

索引

表  索引 
tt  actualpc 
tt  assignedpc 
tt  clientid 
et  employid (主键) 
do  custnmbr (主键)

tt.actualpc值分布不均匀

在进行任何优化之前,explain对select执行分析的结果如下:

table type possible_keys        key key_len ref rows extra
et  all primary           null null  null 74
do  all primary           null null  null 2135
et_1 all primary           null null  null 74
tt  all assignedpc,clientid,actualpc null null  null 3872
   range checked for each record (key map: 35)

每一个表的type都是all,它表明mysql为每一个表进行了完全连接!这个操作是相当耗时的,因为待处理行的数量达到每一个表行数的乘积!即,这里的总处理行数为74 * 2135 * 74 * 3872 = 45,268,558,720。

这里的问题之一在于,如果数据库列的声明不同,mysql(还)不能有效地运用列的索引。在这个问题上,varchar和char是一样的,除非它们声明的长度不同。由于tt.actualpc声明为char(10),而et.employid声明为char(15),因此这里存在列长度不匹配问题。

为了解决这两个列的长度不匹配问题,用alter table命令把actualpc列从10个字符扩展到15字符,如下所示:mysql > alter table tt modify actualpc varchar(15);

现在tt.actualpc和et.employid都是varchar(15)了,执行explain进行分析得到的结果如下所示:

table type  possible_keys  key   key_len ref     rows  extra phpma.com
tt  all  assignedpc,clientid,actualpc null null null 3872  where used
do  all  primary     null  null  null    2135
   range checked for each record (key map: 1)
et_1 all  primary     null  null  null    74
   range checked for each record (key map: 1)

et  eq_ref primary     primary 15   tt.actualpc 1

这还算不上完美,但已经好多了(行数的乘积现在少了一个系数74)。现在这个sql命令执行大概需要数秒钟时间。 为了避免tt.assignedpc = et_1.employid以及tt.clientid = do.custnmbr比较中的列长度不匹配,我们可以进行如下改动:


mysql > alter table tt modify assignedpc varchar(15),
           modify clientid  varchar(15);phpma.com

现在explain显示的结果如下:

table type  possible_keys  key   key_len ref      rows   extra
et  all  primary     null  null  null      74
tt  ref  assignedpc,clientid,actualpc actualpc 15 et.employid 52 where used
et_1 eq_ref primary     primary 15   tt.assignedpc 1
do  eq_ref primary     primary 15   tt.clientid  1phpma.com

这个结果已经比较令人满意了。余下的问题在于,默认情况下,mysql假定tt.actualpc列的值均匀分布,而事实上tt表的情况并非如此。幸而,我们可以很容易地让mysql知道这一点:

shell > myisamchk --analyze path_to_mysql_database/tt
shell > mysqladmin refreshphpma.com

现在这个连接操作已经非常理想,explain分析的结果如下: phpma.com

table type  possible_keys  key   key_len ref      rows  extra
tt  all  assignedpc,clientid,actualpc null null null  3872  where used
et  eq_ref primary     primary 15   tt.actualpc  1
et_1 eq_ref primary     primary 15   tt.assignedpc 1
do  eq_ref primary     primary 15   tt.clientid  1


(阅读次数:
上一篇:优化mysql数据库性能的十个参数...   下一篇:转移数据:MySQL数据导入到Sql Server中
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·用于SELECT和WHERE子句的SQL函数
·MySQL索引分析和优化
·MySQL数据库下.frm .MYD .MYI损
·怎么删除MySQL数据库中的重复数
·MySQL数据导入与导出
·MySQL数据库中SQL查询语句精华
·如何优化MySQL数据库性能
·MySQL查询优化之查询优化器
·怎么备份MySQL数据库
·如何导入导出*.sql文件到MySQL数
·MySQL数据导入导出方法与工具介
·MySQL索引分析和优化加速网站访
  相关文章
·优化mysql数据库性能的十个参数.
·转移数据:MySQL数据导入到Sql Se
·MySQL数据库中文模糊检索问题的
·尝试提高MySQL查询效率的方法
·MySQL和SQL Server的安全性分析
·phpmyadmin建立MYSQL数据库的过
·保护MySQL数据库中的重要数据的
·如何使MySQL数据库完全中文化呢
·掌握MySQL数据库中触发器的应用.
·MySQL的数据迁移到 Oracle的须注
·MySQL数据库学习:MySQL Join详解
·数据库基础:SQL导出到MYSQL数据

关于我们 | 本站声明 | 友情连接 | 诚邀加盟 | 网站地图
版权Power by DedeCms   技术支持QQ =>> 罗江游鱼   Jacking  后台登陆
Copyright @ 2007 公司地址:柳州市东环路268号 邮编:545006 电话:15920389818 桂ICP备07006725号