用相關資料庫命令對MySQL進行優化

2021-08-30 03:58:25 字數 4219 閱讀 7700

我們討論的是資料庫效能優化的另一方面,即運用資料庫伺服器內建的工具輔助效能分析和優化。

▲ show

執行下面這個命令可以了解伺服器的執行狀態:mysql >show status;

該命令將顯示出一長列狀態變數及其對應的值,其中包括:被中止訪問的使用者數量,被中止的連線數量,嘗試連線的次數,併發連線數量最大值,以及其他許多有用的資訊。這些資訊對於確定系統問題和效率低下的原因是十分有用的。

show命令除了能夠顯示出mysql伺服器整體狀態資訊之外,它還能夠顯示出有關日誌檔案、指定資料庫、表、索引、程序和許可許可權表的寶貴資訊。

▲ explain

explain能夠分析select命令的處理過程。這不僅對於決定是否要為表加上索引很有用,而且對於了解mysql處理複雜連線的過程也很有用。

下面這個例子顯示了如何用explain提供的資訊逐步地優化連線查詢。(本例來自mysql文件,見原文寫到這裡似乎有點潦草了事,特加上此例。)

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

explain select tt.ticketnumber, tt.timein,

tt.projectreference, tt.estimatedshipdate,

tt.actualshipdate, tt.clientid,

tt.servicecodes, tt.repetitiveid,

tt.currentprocess, tt.current***erson,

tt.recordvolume, tt.***rinted, 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'

Mysql資料庫命令

create database mydata use mydata create table dept deptno int primary key,dname varchar 14 loc varchar 13 create table emp empno int primary key,enam...

Mysql資料庫命令

mysql命令集錦 測試環境 mysql 5.0.45 注 可以在mysql中通過mysql select version 來檢視資料庫版本 一 連線mysql。格式 mysql h主機位址 u使用者名稱 p使用者密碼 1 連線到本機上的mysql。首先開啟dos視窗,然後進入目錄mysql bin...

mysql資料庫命令

一修改表名 alter table 舊表名 rename to 新錶名二 新增字段 alter table 表名 add 欄位名 型別 屬性 三 修改字段 alter table 表名 change 舊的字段 新的字段4.刪除字段 alter table 表名 drop 欄位名 5 新增主鍵 alt...