使用主鍵或者索引提高SQL語句效率的建議

2021-09-13 18:38:12 字數 730 閱讀 1579

執行delete事務時候如果發現效率很低,可以先檢視是否根據主鍵來查詢需要delete的語句,

如果有四個主鍵,但是delete時候只是根據三個主鍵來決定需要delete的資料的條件,將會使得sql語句的效率非常低

例子第一條sql語句:

string sql = "";

sql = "delete from `mmscm`.msisdn_detail where account_no=? and user_id=? and batch_id=? and msisdn_list_id=? "

+ "and recipient_msisdn=? ";

第二條sql語句:

string sql = "";

sql = "delete from `mmscm`.msisdn_detail where account_no=? and user_id=? and batch_id=? and msisdn_list_id=? "

+ "and recipient_msisdn=? and seq_no=?";

兩條語句相比只是少了乙個引數,但是當使用ps.addbatch()批量執行後,發現效率相差非常大,第一條sql語句在執行10000條資料的時候用了五分鐘,而第二條資料在執行10000條資料的時候只用了1265毫秒,就是1.2秒。

所以當sql語句效率非常低的時候,可以檢視是否是在查詢條件時候沒有用到主鍵或者索引

sql語句建立主鍵

對於有資料的表,在企業管理器裡面建立索引時,容易超時,使用sql語句只要在,工具 選項設定超時值為0,就不會超時。sql語句建立有聯合主鍵的表 create table tabcheck check id int not null,check no int not null,startdate da...

Mysql 增加主鍵或者修改主鍵的sql語句操作

alter table table1 add transactor varchar 10 not null alter table table1 add id int unsigned not null auto increment primary key alter table 表名稱 chang...

強制SQL語句使用某個索引

sql語句範例 select fromwith nolock,index ix bydate where billdate and billdate 注 ix bybilldate為此表中根據欄位billdate建立的字段索引。最近接到客戶反映,某個模組的查詢非常慢,經查,此表有8千萬多條記錄,查某...