Mysql insert語句的最佳化,mysqlinsert語句

來源:互聯網
上載者:User

Mysql insert語句的最佳化,mysqlinsert語句
1) 如果你同時從同一客戶插入很多行,使用多個值表的INSERT語句。這比使用分開INSERT語句快(在一些情況中幾倍)。
    Insert into test values(1,2),(1,3),(1,4)…

2) 如果你從不同客戶插入很多行,能通過使用INSERT DELAYED語句得到更高的速度。Delayed的含義是讓insert 語句馬上執行,其實資料都被放在記憶體的隊列中,並沒有真正寫入磁碟;這比每條語句分別插入要快的多;LOW_PRIORITY剛好相反,在所有其他使用者對錶的讀寫完後才進行插入。

3) 將索引檔案和資料檔案分在不同的磁碟上存放(利用建表中的選項)。

4) 如果進行批量插入,可以增加bulk_insert_buffer_size變數值的方法來提高速度,但是,這隻能對myisam表使用。

5) 當從一個文字檔裝載一個表時,使用LOAD DATA INFILE。這通常比使用很多INSERT語句快20倍。

6) 根據應用情況使用replace語句代替insert。

7) 根據應用情況使用ignore關鍵字忽略重複記錄。

對於mysql語句的INSERT

先查詢出來然後再 插入
 
Mysql 使用insert 語句的問題

你可用以下任一種方式:
insert problemprocess values(null,"it's a useful book");
insert problemprocess values(null,'it''s a useful book');
insert problemprocess values(null,'it\'s a useful book');

在資料庫應用程式中可對收集到的字串做替換操作,如:
sometext = sometext.replaceAll("\'","\'\'")
sometext = sometext.replaceAll("\"","\\\"")
sometext = sometext.replaceAll("\、","\\\\")
....

如果在java用jdbc操作資料庫,還可以用PreparedStatement ,如:
PreparedStatement pstmt =connection.prepareStatement(sql);
....
String sometext = textfield.getTest(); --取得使用者輸入的內容
pstmt.setInt(2, sometext );
pstmt.executeUpdate();
 

相關文章

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.