天天看點

伺服器架構之性能擴充-第五章(6)

Mysql>create table t2 like t1;          //複制表結構

Mysql&gt;insert into t2 select * from t1;   //複制資料内容<b></b>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/223738156.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224214503.png"></a>

索引是一種快速查詢的有效方法,可以通過alter增加索引或create語句建立。

mysql&gt;alter table t1 add ind_id (id);

mysql&gt;alter table t1 add unique/primary key (id);

mysql&gt;create index ind_id on table t1 (id);

mysql&gt;show index from t1;

mysql&gt;drop index ind_id on table t1;

mysql&gt;alter table t1 drop index ind_id;

Unique索引時指唯一索引,是沒有重複行的索引。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224239287.png"></a>

Alter方法建立和删除索引

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224402929.png"></a>

删除主鍵,如果索引列是自動增長的變量。首先要取消自動增長,然後才可以删除主鍵,因為自動增長序列預設為主鍵。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224430108.png"></a>

增加自動增長主鍵索引

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224501402.png"></a>

視圖:是存在于表之外的另一張表,符合條件的查詢結果

Mysql&gt;create view v_t1 as select * from t1 where id &gt;10;

Mysql&gt;drop view v_t1;

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224554764.png"></a>

視圖時随着主表而變化的表

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224622372.png"></a>

<b>字元串函數:</b>

Concat (string1,……) //連接配接字元串

Lcase(string1) //換成小寫

Ucase(string1)//換成大寫

Length(string1) //string1的長度

Rtrim(string1) //去除後端空格

Ltrim(string1)//取出前段空格

Repeat (string1,count)//重複count次

Replace(str,search_str,replace_str) //在str中,用replace_str代替search_str

Substring(str,position,length) //從str的position開始,取length個字元

Space(count) //生成count個空格

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224645430.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224710674.png"></a>

<b>數學函數:</b>

Bin(number)//十進制轉為二進制

Ceiling(string1)//向上取整

Floor(string)//向下取整

Max(col)//取最大值,聚合時使用

Min(col)//取最小值,聚合時使用

Sqrt(number)//開平方

Rand()//傳回0-1内的随即取值

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224727190.png"></a>

<b>日期函數:</b>

Curdate() //傳回目前日志

Curtime() //傳回目前時間

Now() //傳回目前日期時間

Week(date) //傳回這是多少周

Year(date) //傳回年份

Datediff(date1,date2) //傳回開始時間date1和結束時間date2間天數

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224744647.png"></a>

預處理是傳遞一個參數作為where判斷語句:

Mysql&gt;prepare hello from “select * from t1 where id &gt;?”;

Mysql&gt;set @i=1;

Mysql&gt;excute hello using @i;

Mysql&gt;drop prepare stml;

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224757316.png"></a>

mysql&gt;set autocommit=0;       //關閉自動送出

mysql&gt;delete from t1 where id=11; //

mysql&gt;savepoint p1;        //建立一個還原點

mysql&gt;delete from t1 where id=10;

mysql&gt;savepoint2;

mysql&gt;rollback to p1; //恢複到p1還原點,p2自動失效

mysql&gt;rollback; //退回到原始還原點

修改引擎,使用事處理功能,要使用innodb引擎才可以。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224809656.png"></a>

復原恢複資料

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224821636.png"></a>

設定復原點

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224835663.png"></a>

存儲是一種批量插入資料的方法,需要先将結尾符改為//,最後再改回來,然後通過call指令來調用存儲

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224852756.png"></a>

觸發器是一種當完成某項動作的同時,可以同時調用其它資料庫程序同時進行。通過create trigger建立。

提前存在的值則old,不存在則new。

<b>Insert</b><b>型觸發器:</b>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224909452.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224921949.png"></a>

<b>Delete</b><b>型觸發器</b>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224937325.png"></a>

<b>Update</b><b>型觸發器:</b>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/224951594.png"></a>

聯合檢視:可以使用聯合檢視來查詢觸發與否

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225008562.png"></a>

常用的删除資料表内容有如下兩種方法:

Mysql&gt;delete from t1; //清除表的内容,改變結構

Mysql&gt;truncate table t1; //清楚表的内容,不改變結構,速度快常用

Mysql&gt;alter table t1 auto_increment=1; //如果自動增長值不是從1開始可以這樣調整

<b>正規表達式:</b>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225024649.png"></a>

Mysql&gt;select name,email from t where email pegexp “@163[,.]com$”; //比對@163.com或@163,com郵箱

Mysql&gt;select name,email from t where emal like “@163.com” or email kike “@163,com”;

<b>Rand()</b><b>随機:</b>

Mysql&gt;select * from t order by rand() limit 3; //随機3條資料

<b>Group by</b><b>的排序擴充</b>

Mysql&gt;select cname,pname,count(pname) from t group by cname,pame with rollup;   //分别對個結果進行排序和統計

<b>建立外鍵:</b>

Mysql&gt;create table t3(id int,name char(20), foreign kye (id) references t2(id) on delete cascade on update cascade); //建立t3使用外鍵t2

<b>Mysql help</b><b>使用:</b>

Mysql&gt;? Create 檢視指令和用法

<b>首先檢視各種語句使用頻率</b>

Mysql&gt;show 【session/global】 status; //session目前連接配接,global表示資料啟動至今

登陸以來進行增删改查的次數

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225039353.png"></a>

Com_update,com_delete分别表示更新和删除次數。

對于innodb引擎可以使用以下查詢語句,innodb_rows_delete/update/select/insert

Innodb引擎是影響的行數,myisam引擎是影響的次數。

Innodb是影響的行數,myisam是影響的次數。

Connections代表連接配接數,uptime代表連接配接時間,slow_queries代表慢查詢次數。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225053750.png"></a>

<b>定位執行效率低的語句</b>

Mysql&gt;explain select * from t where id=1000;

Mysql&gt;desc select * from t where id=1000;

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225106713.png"></a>

我們重點看一下影響行數。

對一個表增加索引之後,查詢範圍由9變2,快多了。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225129789.png"></a>

索引是優化mysql資料庫最常用的方法,使用索引注意以下幾點。

1,              ,對于複合索引,一般隻對左邊的索引有效。

2,              使用通配符時,like左邊不能用通配符

例:mysql&gt;explain select * from t where name like “%3”\G //不會使用索引

3.對于null的搜尋is null

例:mysql&gt;desc select * from t where name is null \G

1.對于行數低于100的資料表,索引效果不是很好

2.聯合查詢語句語句要都使用索引,才會使用索引

3.如果索引列是字元串,則查詢時要加“”

4.對于handler_read_rnd_next參數較大的,應該建立索引

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225144868.png"></a>

<b>5.3.1</b><b>提高資料的導入效率</b>

常用導入資料的方法是通過outfile進行的。通過outfile導出資料,這時是表内容,非表結構。例如導出資料

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225707926.png"></a>

導入資料

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225156433.png"></a>

對于innodb引擎表可以通過以下方法提高速度:

1、通過set unique_checks=0,關閉唯一性效驗,導入資料完畢後再set unique_checks=1;恢複唯一性效驗。

2、關閉自動送出,提高導入效率。導入資料前執行set autocommit=0關閉自動送出;導入後set autocommit=1,恢複自動送出。

使用insert delayed可以使資料庫得到更高的效率。并且可以增加bulk_inser_buffer_size的變量值來提高速度

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225209844.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225224304.png"></a>

資料庫的優化一般可以通過拆分表提高表的通路效率,這也可以解決大存儲量;使用中間表技術提高查詢速度,中間表一般就是視圖技術。

資料庫鎖定可以使用lock table t read/write;

讀鎖,有一個人讀鎖,其他人可讀不可以寫。

寫鎖,隻有本人可以進行增删改查,其他人不能讀寫。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225236986.png"></a>

Mysql&gt;status;檢視系統字元集。它包括伺服器字元集,資料庫字元集,用戶端字元集,連結字元集

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230110497.png"></a>

字元集可以通過/etc/my.cnf配置檔案進行修改

【client】用于定義用戶端字元集和連結字元集

<a target="_blank" href="http://blog.51cto.com/attachment/201208/225253573.png"></a>

【mysqld】控制着伺服器字元集和資料庫字元集

下面的collation-server是校驗字元集

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230020702.png"></a>

Mysql&gt;show character set; //檢視校驗字元集

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230130231.png"></a>

Bin_log日志是系統進行恢複的重要日志

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230146269.png"></a>

通過修改/etc/my.cnf檔案,去掉#号即可

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230158899.png"></a>

慢查詢日志是進行資料庫優化的基礎。

Vi /etc/my.cnf

Log_slow_queries=slow.log    //開啟慢查詢

Log_query_time=5          //設定慢查詢的時間

<a target="_blank" href="http://blog.51cto.com/attachment/201208/230211299.png"></a>

Socket預設是位于/tmp/mysql.sock, mysql的啟動需要socket檔案,當然可以通過重新開機資料庫自動建立socket,也可以不用socket便實作登陸。可以使用

Mysql&gt;mysql –u root –p –protocol tcp –hlocalhost

Service mysqld stop

Mysql_safe –skip-grant-tables –user=mysql &amp;   //跳過授權表

Mysql –uroot

Mysql&gt;update user set password=password(“123”) where user=”root” and host=”localhost”;

或mysql&gt;set password for root@loaclhost=password(“123”);

或mysql&gt;set password=password(“123”);   //修改密碼 

本文轉自zsaisai 51CTO部落格,原文連結:http://blog.51cto.com/3402313/967194