mysql關於select where in的效率

2021-07-27 09:18:08 字數 1396 閱讀 1482

很多大牛公司建議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

20.468s

30.456s

40.460s

50.451s

60.435s

70.427s

80.481s

90.482s

100.520s

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

第*次查詢耗時1

0.559s

20.565s

30.554s

40.549s

50.550s

60.550s

70.541s

80.493s

90.562s

100.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查詢 關於MYSQL 查詢

你想要的是這種效果嗎?建立表city cost create table city cost id int primary key auto increment,cost time date,city varchar 20 money int auto increment 1 插入資料 insert...

關於mysql索引 關於mysql的索引

我們先來了解mysql索引的概念 索引是一種特殊的檔案,他們包含著對資料表裡所有記錄的引用指標。更通俗地說,資料庫索引好比是是一本書前面的目錄,能加快資料庫的查詢速度,我們有了相應的索引後,資料庫會直接在索引中查詢符合條件的選項。索引分為聚簇索引和非聚簇索引兩種,聚簇索引是按照資料存放的物理位置為順...

mysql關於日期 關於mysql日期的一些例子

mysql中的月份計算 減少乙個月,比如 原來的subtime 2006 10 22 12 22 22 減少後變成 2006 9 22 12 22 22 update message set subtime date sub subtime,interval 1 month 增加乙個月 update...