mysql sql最佳化

來源:互聯網
上載者:User

標籤:file   檔案   資料   提升   沒有   .com   targe   tar   style   

記錄一些經驗,主要是結論。建檢索等就不寫了,地球人都知道。

1. 對比2個join

select * from (select * from A where age > 10) inner join (select * from B where grade > 3) tb on A.b_id = tb.id;select * from (select * from A where age > 10) inner join B on A.b_id = B.id and B.grade > 3;

當B的id欄位為主鍵或索引時,如果資料量達到千萬級時,第二條比第一條sql可能快上10倍以上,因為第一個sql裡tb沒有索引。

 

2. jdbc串連mysql插入資料,使用批量插入可提升8倍以上效能。有兩個點需要關註:

(1)資料庫連接需要加上“rewriteBatchedStatements=true”,否則jdbc不會真正使用批量插入。

(2)如果mysql有主從,注意每次批量插入的數量不要太多,防止失敗。

 

3. 觀察mysql執行的sql

用tail -f查看即可,sql執行記錄檔路徑在my.cnf檔案中general_log_file定義。

 

4. 使用explain

詳解參見 http://database.51cto.com/art/200912/168453.htm

 

mysql sql最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.