mysql最佳化—–dml語句

來源:互聯網
上載者:User

author:skate
time:2012/07/02

 

mysql最佳化-----dml語句

 

一. insert語句

1. insert語句操作的流程:
1).connecting
2).發送query到server
3).分析query
4).insert row
5).insert indexes
6).關閉串連

2. insert語句最佳化思想是批量提交insert語句(同時也要平衡化整為零的原則)
例如,
  A. 使用帶有多值insert;
  B. 關閉autocommit功能,避免每個insert flush log到磁碟,批量多個insert語句,然後再commint。

注意
調整參數bulk_insert_buffer_size,增加批量處理的數量

 

3. “LOAD DATA INFILE”裝載資料速度比insert要快20倍

    mysql> load data infile '/tmp/t2.txt' into table t2 fields terminated by '"' enclosed by ','; 

4.在insert語句中,有預設值的列一定要用預設值,不要人為給值,較少sql解析的時間

5.對於innodb表,如果表上有unique和foreign約束,再倒入資料時候,最好關閉約束

 SET unique_checks=0; //disable unique約束
 SET foreign_key_checks=0; //disable foreign約束
  sql insert 語句;
 SET unique_checks=1; //enable unique約束
 SET foreign_key_checks=1; //enableforeign約束
 

二. update/delete
update/delete的速度取決於更新的資料量,也可以採用批次更新;在頻繁的update之後要執行optimize table來消除片段

 

-----end-----

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.