要提高查詢速度,一般:
1.不需要刪除的欄位,建主鍵;有可能要被刪除的欄位,建索引。
2.假如一次提交5W個號碼,每個都要和資料庫裡90W號碼進行比較5W個號碼中哪些號碼是90W號碼中的。那麼將90W號碼建一個表,一個欄位就是號碼欄位,然後把該欄位設為主鍵即可。
update前100條為0,另外一個程式找狀態為0的,要提高速度,要將這100條(所有條)的ID建索引。
3.不管對什麼欄位建的什麼索引,該欄位只有放在where條件中使用時,索引才生效,假如放到not in或者其他條件中,索引無效。
另外,有個語句:
- select t1.code,t1.id,t1.content,t1.clid,wg.fnumber,t1.mlevel
- from (select * from t_busi_pre_mx mx
- where <span style="color:#ff0000;">mx.mainid='000' and mx.sjtdid='00' </span>and mx.stus='1' and mx.stype='1' ) t1
- left join T_CL_TD td
- on t1.sjtdid=td.tdid
- left join T_CON_WG wg
- on td.wgid=wg.wgid
- where rownum<=wg.fnumber order by mlevel
對於sql中紅色字型:(mainid和sjtdid在這裡的都是唯一的,但是在表中不一樣,不知道怎麼說,只想說where後的條件順序不一樣,速度不一樣,對某些欄位加索引,速度更快)
情況1.(此時mianid未建索引)
- where sjtdid='00' and mainid='000' 耗時900ms
情況2.(此時mianid未建索引)
- where mainid='000' and sjtdid='00' 耗時450ms,快了1倍
情況3.(對mainid建索引,mianid是另外一個表的id)
- create index t_busi_pre_mx_idx on t_busi_pre_mx (mainid);
- where mainid='000' and sjtdid='00' 耗時33ms,相對情況1,快了30倍
4.Oracle序列,類似sqlserver自增主鍵,是數字類型的。使用序列也可以加快查詢速度。
---建序列:
CREATE SEQUENCE seq_mxid
INCREMENT BY 1 -- 每次加幾個
START WITH 1 -- 從1開始計數
NOMAXVALUE -- 不設定最大值
NOCYCLE -- 一直累加,不迴圈
;
---使用序列
- insert into table(id) values(<span style="color:#6633ff;">seq_mxid.nextval;