用相关数据库命令对MySQL进行优化


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

▲ show

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

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

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

▲ explain

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

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

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

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
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);

现在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  1

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

shell > myisamchk --analyze path_to_mysql_database/tt
shell > mysqladmin refresh

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

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

▲ 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'


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3