Mysql日常開發注意要點

2021-08-18 01:46:04 字數 3139 閱讀 1237

1、mysql儲存引擎介紹

myisam:低版本mysql預設的mysql外掛程式式儲存引擎,儲存檔案易損壞,不支援事務。

innodb:目前預設的mysql儲存引擎,用於事務處理應用程式,具有眾多特性,包括acid事務支援

建表語句建議使用 engine=innodb 字段,例如:

create

table

`t_user` (

....

) engine

=innodb auto_increment=初始值 default charset=utf8;

2、explain執行計畫(mysql explain用法)

type: all 全表掃瞄

rows: 掃瞄的行數

extra: using temporary; using filesort

3、字段使用函式,將無法使用索引

例:select * from t where year(d) >= 2016;

由於mysql不像oracle那樣支援函式索引,即使d欄位有索引,也會直接全表掃瞄。

應改為-->select * from t where d >= '2016-01-01';

建議改為如下,reffereetime欄位建索引

reffereetime >= '2016-07-03' and reffereetime < '2016-07-10'

reffereetime >= '2016-07-03 00:00:00' and reffereetime < '2016-07-10 00:00:00'

4、以%開頭的like無法使用到索引

select * from t where name like '%de%'; #無法使用到索引

select * from t where name like 'de%'; #使用到索引

5、避免隱式轉換

列型別是字串,那麼一定記得在where條件中把字串常量值用引號引起來,否則即便這個列上有索引,mysql也不會用到

6、讓 group by 不排序

group by payment_date order by null 不排序,如統計count(*)

7、隨機數的寫法

select * from t1 where 1=1 order by rand() limit 4;

mysql不支援函式索引,會導致全表掃瞄

應改為select * from t1 where id >= ceil(rand()*1000) limit 4;

8、子查詢 關聯查詢比較9、select *

select與from語句之間只定義返回的欄位名,除非返回所有的字段,盡量不要使用*,欄位名應按照表的字段物理順序編寫

10、索引

經常用於where子句中使用的列考慮作索引的列。

經常用於sql語句中鏈結表的列考慮作為索引的列。

頻繁修改的列不建議作為索引列。

11、復合索引

使用者在一張表的三個列(a,b,c)建立一復合索引,該復合索引的順序為abc。

在sql語句的where條件中使用a,ab,abc等都可利用到索引,但是如果使用bc,c等就無法利用到索引。

復合索引 idx1(a, b, c),那麼下面的sql都可以完整用到索引:

select ... where b = ? and c = ? and a = ?; --注意到,where中欄位順序並沒有和索引字段順序一致

select ... where b = ? and a = ? and c = ?;

select ... where a = ? and b in (?, ?) and c = ?;

select ... where a = ? and b = ? order by c;

select ... where a = ? and b in (?, ?) order by c;

select ... where a = ? order by b, c;

select ... order by a, b, c; -- 可利用聯合索引完成排序

而下面幾個sql則只能用到部分索引,或者可利用到icp特性:

select ... where b = ? and a = ?; -- 只能用到 (a, b) 部分

select ... where a in (?, ?) and b = ?; -- explain顯示只用到 (a, b)部分索引,同時有icp

select ... where (a between ? and ?) and b = ?; -- explain顯示只用到 (a, b) 部分索引,同時有icp

select ... where a = ? and b in (?, ?); -- explain顯示只用到 (a, b) 部分索引,同時有icp

select ... where a = ? and (b between ? and ?) and c = ?; -- explain顯示用到 (a, b, c) 整個索引,同時有icp

select ... where a = ? and c = ?; -- explain顯示只用到 (a) 部分索引,同時有icp

select ... where a = ? and c >= ?; -- explain顯示只用到 (a) 部分索引,同時有icp

icp(index condition pushdown)是mysql 5.6的新特性,其機制會讓索引的其他部分也參與過濾,減少引擎層和server層之間的資料傳輸和回表請求,通常情況下可大幅提公升查詢效率。

下面的幾個sql完全用不到該索引:

select ... where b = ?;

select ... where b = ? and c = ?;

select ... where b = ? and c = ?;

select ... order by b;

select ... order by b, a;

日常開發注意篇心得總結。

最近工作有些忙,但不忘做些日常總結,溫故而知新嘛。盡量使用php內建方法,因為二維陣列使用了個 導致php版本不相容 git限制重要配置不要提交到版本庫,因為測試環境,本地環境,線上環境等都不相同,每個人電腦也不同 中不要寫死 因為涉及到負載均衡,等多web伺服器的時候,特別是多同事協作的時候,太難...

android日常開發記憶體優化注意點

1.盡量的少使用資源,多使用shape畫圖或者.9.png 3.context不要被長時間引用,例如 執行緒引用了context,並且在無限迴圈 雖然介面退出去,activity被摧毀,但context還在被執行緒引用,當前activity的資源就不會被 4.context沒被長時間引用情況下,只要...

日常開發需要規避的注意點

根據自己系統的領域模型去判斷需要那些pojo的規範 pojo 所處領域 說明do dao層 與資料庫表結構一一對應,通過 dao 層向上傳輸資料源物件 dtoservice層或manager層 資料傳輸物件,service 或 manager 向外傳輸的物件。boservice層 業務物件。由 se...