1. 程式人生 > >mysql關於select where in的效率

mysql關於select where in的效率

很多大牛公司建議select where in中不要超過200,有的說不要超過500.
那麼問題來了,使用子查詢的時候如果結果級超過了500呢。
今天做了個測試

下面是測試結果,分別是兩個語句查詢10次所用的時間
select * from cp_bill_info where id in(select id from cp_bill_info where id< 501)

第*次查詢 耗時
1 0.434s
2 0.468s
3 0.456s
4 0.460s
5 0.451s
6 0.435s
7 0.427s
8 0.481s
9 0.482s
10 0.520s

select * from cp_bill_info where id in(1,2,3,……500)

第*次查詢 耗時
1 0.559s
2 0.565s
3 0.554s
4 0.549s
5 0.550s
6 0.550s
7 0.541s
8 0.493s
9 0.562s
10 0.531s

從結果來看用子查詢的效率要優於直接寫入查詢範圍。
另外還測試了

select * from cp_bill_info where id in(select id from cp_bill_info where id< 201)
select * from cp_bill_info where id in(select id from cp_bill_info where id< 101)
select * from cp_bill_info where id in(select id from cp_bill_info where id< 1001)

10次的耗時也維持在0.4**s也就是說子查詢對於範圍的影響很小。
但是使用寫入查詢範圍的方式,當範圍越多時,耗時會隨之增加。

select * from cp_bill_info where id in(1,2,3,……1000)

用時在0.8**s。

select * from cp_bill_info where id in(1,2,3,……100)
select * from cp_bill_info where id in(1,2,3,……200)

比500的範圍縮小用時在0.03左右。

總結:select where in使用子查詢時,子查詢中結果集的大小對查詢速度影響很小。但是直接寫入查詢範圍的時候最好控制在500個以內,越多效率越低

相關推薦

mysql關於select where in效率

很多大牛公司建議select where in中不要超過200,有的說不要超過500. 那麼問題來了,使用子查詢的時候如果結果級超過了500呢。 今天做了個測試 下面是測試結果,分別是兩個語句查詢10次所用的時間 select * from cp_bil

Sql Server參數化查詢之where in和like實現詳解

blog charindex 語句 pan 建議 ack rop for 臨時表 文章導讀 拼SQL實現where in查詢 使用CHARINDEX或like實現where in 參數化 使用exec動態執行SQl實現where in 參數化 為每一個參數生成一個參數

mysql where in 數組解決小tips

array php sql語法 arr bsp 字符串 tip tips ring 由於sql語法要求,不可在in後面直接連數組,若數組形式下,則需要轉換成逗號隔開的字符串 <?php$arr = array(1,2,3,4,5);$arr_string= join(

mysql 按照 where in 排序

ssg rom end ext sga xtend extend sel uniq select * from user_extend where `unique` in(‘[email protected]‘,‘[email protected]‘,‘

學習筆記11 EF查詢相當於sql 中的 where in

lec sql blog contains 學習筆記 [] HERE var sel 兩種寫法 1、 int[] Ids={1,2,3} DBContainer db=new DBContainer(); var list=db.表明.where(a=>Ids.

mysql中 where in 用法詳解

MySQL這裏分兩種情況來介紹 1、in 後面是記錄集,如: select * from table where uname in(select uname from user); 2、in 後面是字符串,如: select * from table where uname

Oracle數據庫查詢用 where in 查詢的項超過1000條的解決方案

ont rac ipc ram take list() nbsp con || 眾所周知,如果我們的用SQL查詢語句時,如果用where in帶的參數超過1000條的話,oracle是會報錯的。 因為項目中遇到這樣的問題,所以找到了接下來我要說的這個辦法。 因為用的地方很多

mysql查詢時,查詢結果按where in陣列排序

使用情況:當我們在進行where id in () 查詢的時候,如果where in的id查詢陣列為[2,7,1,4,3],正常情況查詢出來的結果順序為[1,2,3,4,7],這可能不是我們想要的結果,我們期望查出來的結果順序與where in的順序一致,這裡介紹兩個方式:

MySql中 where IN 字串自動新增單引號問題

mysql查詢語句where條件in 正常情況需要查詢的語句: select *from temp where id in ('1','2','3','4','5') 在實際過程中發現一直查不出資料,實際上能夠查出資料,弄得一直很鬱悶,找不出原因。 通過各終嘗

MySQL 中 delete where in 語句的子查詢限制

場景一 delete from table1 where id = (select max(id) from table1 ); [Err] 1093 - You can’t specify target table 'table1 ’ for updat

ibatis/mybatis出現 sql 語句 where in(引數1,引數2……引數1000) 超過1000個引數時候的解決方案

公司專案出現bug,查詢後是由於sql 語句in()內參數查過1000引起的 由於專案比較大,無法輕易修改或重構。 公司框架比較老用了ibates  無法從xml配置裡進行修改。所以本人寫了段程式碼

mysql中 where in

SQL 語句中In 和 Where 的含義不同。 應用解釋如下: 1、如需有條件地從表中選取、刪除、更新資料時,使用Where; 2、In只作為Where條件子句下的一個運算子,除了In之外還有Between、Like、=、>、>=、<、<=等運算子。 下面舉例說明

SQL中 exec sp_executesql 執行 where in 引數方法

在公司專案中,需要用到一個方法,就是批量傳入記錄編號 通過 where jlbh in()的方法查詢記錄; 平時直接用 select * from  JDZS where Jlbh in(‘001’,‘002’,‘003’) 完全沒有問題。 但是我在用到的Fastrepo

sql where in字串問題

在pycharm中執行 select * from value in(1,2); 會提醒: No statement found under the caret. Execute all statements in the file or just the ones after th

where in遇到null時的解決方法1

參考:https://www.2cto.com/database/201109/104960.html;http://ask.csdn.net/questions/6800061 :  SELECT* FROM華東 WHERE公司程式碼 IN ( SELECT 公司代 FRO

SQL中where in的用法以及mybatis執行查詢語句,可以在plsql中查詢資料,但是在程式中查詢不到

首先我們要知道where是什麼:一個判斷符。在SQL操作中,控制只選擇指定的行。 in的其實歸類於特殊的比較運算子 expr1 between expr2 and expr3:表示expr1的值在expr2和expr3之間 expr in(expr2,expr3,expr4,

mysql怎麼使用 where in

in作為查詢條件,一般典型有兩種用法: 一是IN常量,例如下面語句查詢一、三年級的學生: SELECT * FROM student WHERE grade IN ('一','三'); 二是使用子查詢,也就是IN(SQL語句),例如下面的語句查詢不及格的班級的所有學生:

sql語句中當條件的數量非常大時where...in條件子句用什麼更好的方法代替?

When you need to pass a list of parameters into a MySQL, there are at least two method to do this: Issue a dynamical query which would fill an IN list wi

python mysql where in 對列表(list,,array)問題

例如有這麼一個查詢語句: select * from server where ip in (....) 同時一個存放ip 的列表 :['1.1.1.1','2.2.2.2','2.2.2.2'] 我們希望在查詢語句的in中放入這個Ip列表,這裡我們首先會想到的是用join

資料量對where in語句的索引影響

開發十年,就只剩下這套架構體系了! >>>