mysql id in 效率_MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

1楼

支持楼主  呵呵  有才人啊

1d91e27eee5fc0b4ad99d7838d1088be.pngliangCK06-26 16:55

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

2楼

先接个分..再看.

1ec09dbd758869b0216beb28f9ab6b28.pngapple_818006-26 16:56

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

3楼

将100已知ID写入一个临时表,然后用联结试试

ae634e09736e95bfeec9c554c40f716c.gifConry06-26 16:56

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

4楼

引用 2 楼 liangCK 的回复:

先接个分..再看.

up

d4ffc25c6882174161f9fd34dbad19ab.pngnetxuning06-26 16:59

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

5楼

支持

1d91e27eee5fc0b4ad99d7838d1088be.pngliangCK06-26 16:59

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

6楼

我认为分两步.

先将提供的ID转成行集.

ID

-----

38068

238833

308799

..

然后与表做JOIN操作.

1ec09dbd758869b0216beb28f9ab6b28.pngapple_818006-26 17:05

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

7楼

引用 6 楼 liangCK 的回复:

我认为分两步.

先将提供的ID转成行集.

ID

-----

38068

238833

308799

..

然后与表做JOIN操作.

我3楼就是这个意思。

实际还要对比 Join 和 In 在本例中那个效率高

95c521e29d6d1fe32f8fb445512b9fb0.gifWWWWA06-26 17:11

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

8楼

1、

直接用IN

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

2、用LSBTEST

DROP TABLE IF EXISTS `lsbtest`;

CREATE TABLE `lsbtest` (

`id1` bigint(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `lsbtest` */

LOCK TABLES `lsbtest` WRITE;

insert  into `lsbtest`(`id1`) values (1),(2),(38068),(238833),(308799),(274344),(299910),(309823),(337486),(290812),(56393),(64413),(492511),(260426),(58626),(450987),(499220),(187731),(365665),(212799),(227788),(255724),(384636),(465766),(417310),(313148),(483987),(328761),(402876),(237274),(249183),(174185),(28187),(189155),(259940),(67800),(60065),(340172),(311667),(354861),(182),(305523),(115981),(365082),(213915),(47894),(131301),(198754),(358852),(112496),(404423),(486725),(233123),(322936),(325337),(125932),(299260),(128791),(295663),(469897),(120580),(347766),(34859),(364204),(37597),(268974),(351155),(256955),(214013),(309192),(412394),(216800),(30315),(411242),(16678),(233247),(359013),(401666),(30792),(452394),(408649),(14159),(5519),(91705),(227648),(120966),(319599),(351170),(68129),(368701),(233566),(144256),(156172),(41972),(499687),(390955),(6549),(298079),(498230),(196397),(239493),(242037);

UNLOCK TABLES;

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

95c521e29d6d1fe32f8fb445512b9fb0.gifWWWWA06-26 17:21

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

9楼

1、

select * from t_06 where id in (38068,238833,308799,274344,299910,309823,337486,290812,56393,64413,492511,260426,58626,450987,499220,187731,365665,212799,227788,255724,384636,465766,417310,313148,483987,328761,402876,237274,249183,174185,28187,189155,259940,67800,60065,340172,311667,354861,182,305523,115981,365082,213915,47894,131301,198754,358852,112496,404423,486725,233123,322936,325337,125932,299260,128791,295663,469897,120580,347766,34859,364204,37597,268974,351155,256955,214013,309192,412394,216800,30315,411242,16678,233247,359013,401666,30792,452394,408649,14159,5519,91705,227648,120966,319599,351170,68129,368701,233566,144256,156172,41972,499687,390955,6549,298079,498230,196397,239493,242037);

(100 row(s) returned)

Execution Time : 00:00:00:000

Transfer Time  : 00:00:00:000

Total Time     : 00:00:00:000

2、

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

lsbtest:没有索引

(102 row(s) returned)

Execution Time : 00:00:00:000

Transfer Time  : 00:00:00:000

Total Time     : 00:00:00:000

如果直接运行1、2则两者在速度上基本相近

但2如果加上建表、插入数据,则

DROP TABLE IF EXISTS `lsbtest`;

CREATE TABLE `lsbtest` (

`id1` bigint(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `lsbtest` */

insert  into `lsbtest`(`id1`) values (1),(2),(38068),(238833),(308799),(274344),(299910),(309823),(337486),(290812),(56393),(64413),(492511),(260426),(58626),(450987),(499220),(187731),(365665),(212799),(227788),(255724),(384636),(465766),(417310),(313148),(483987),(328761),(402876),(237274),(249183),(174185),(28187),(189155),(259940),(67800),(60065),(340172),(311667),(354861),(182),(305523),(115981),(365082),(213915),(47894),(131301),(198754),(358852),(112496),(404423),(486725),(233123),(322936),(325337),(125932),(299260),(128791),(295663),(469897),(120580),(347766),(34859),(364204),(37597),(268974),(351155),(256955),(214013),(309192),(412394),(216800),(30315),(411242),(16678),(233247),(359013),(401666),(30792),(452394),(408649),(14159),(5519),(91705),(227648),(120966),(319599),(351170),(68129),(368701),(233566),(144256),(156172),(41972),(499687),(390955),(6549),(298079),(498230),(196397),(239493),(242037);

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

Total Time     : 00:00:01:094

Total Time     : 00:00:00:250

Total Time     : 00:00:00:015

大约1.359秒

ab3dca43e2b98810aa6b1abe7095c5f6.pnglastyang06-26 18:10

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

10楼

帮顶

e059f6d44e64074efbc44da895121358.gifnettman06-26 18:10

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

11楼

友情UP!

1c32a8d6c428fa87bca16c28a6242311.pngLongYongkai06-26 21:28

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

12楼

UP

89bcb63cf5daee771fd0d2211deeb730.gifkaifaye06-27 02:01

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

13楼

看看~~~~~~

0f3bedc325e29d72eb7b2339299fcd52.pngwhy_java06-27 08:48

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

14楼

UP 学习了

59c14108067381fad0eb5e9303e90374.pngyhjhoo06-27 11:19

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

15楼

引用 8 楼 WWWWA 的回复:

1、

直接用IN

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

2、用LSBTEST

DROP TABLE IF EXISTS `lsbtest`;

CREATE TABLE `lsbtest` (

`id1` bigint(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `lsbtest` */

LOCK TABLES `lsbtest` WRITE;

insert  into `lsbtest`(`id1`) values (1),(2),(38068),(238833),(308799),(274344),(299910),(309823),(337…

感觉还是把这些数据放到一张表里面好

8a14b364b3764bb6b04aa439eea19e49.pngbllizard06-27 12:18

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

16楼

把这个放到一个INDEX中,可以吗?

比如:

create index index_id on yourtable (id)

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

17楼

学习了

03395ca160f2c25ed524e7d035b07a85.gifLIXG42506-27 17:15

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

18楼

jie fen ...

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

19楼

学习加接分

bebbae03c1ef55ebcd5b073f8756b624.pngjosy06-27 20:00

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

20楼

用临时表的话,应该把创建临时表和删除临时表的时间算上

试试这个不创建表的方法(没有环境测试,不知道性能如何):

select

a.*

from

t_06 as a

inner join

(select 38068 as id

union all select 238833   union all select 308799

union all select 274344

union all select 299910

union all select 309823

union all select 337486

union all select 290812

union all select 56393

union all select 64413

union all select 492511

union all select 260426

union all select 58626

union all select 450987

union all select 499220

union all select 187731

union all select 365665

union all select 212799

union all select 227788

union all select 255724

union all select 384636

union all select 465766

union all select 417310

union all select 313148

union all select 483987

union all select 328761

union all select 402876

union all select 237274

union all select 249183

union all select 174185

union all select 28187

union all select 189155

union all select 259940

union all select 67800

union all select 60065

union all select 340172

union all select 311667

union all select 354861

union all select 182

union all select 305523

union all select 115981

union all select 365082

union all select 213915

union all select 47894

union all select 131301

union all select 198754

union all select 358852

union all select 112496

union all select 404423

union all select 486725

union all select 233123

union all select 322936

union all select 325337

union all select 125932

union all select 299260

union all select 128791

union all select 295663

union all select 469897

union all select 120580

union all select 347766

union all select 34859

union all select 364204

union all select 37597

union all select 268974

union all select 351155

union all select 256955

union all select 214013

union all select 309192

union all select 412394

union all select 216800

union all select 30315

union all select 411242

union all select 16678

union all select 233247

union all select 359013

union all select 401666

union all select 30792

union all select 452394

union all select 408649

union all select 14159

union all select 5519

union all select 91705

union all select 227648

union all select 120966

union all select 319599

union all select 351170

union all select 68129

union all select 368701

union all select 233566

union all select 144256

union all select 156172

union all select 41972

union all select 499687

union all select 390955

union all select 6549

union all select 298079

union all select 498230

union all select 196397

union all select 239493

union all select 242037

) as b

on

a.id=b.id

95c521e29d6d1fe32f8fb445512b9fb0.gifWWWWA06-29 10:04

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

21楼

临时表:

DELIMITER $$

DROP PROCEDURE IF EXISTS `zz`.`cxzfnewa`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `cxzfnewa`(i varchar(5000))

BEGIN

DECLARE ai INT DEFAULT 1;

DROP TABLE IF EXISTS `t_06`;

create table t_06 (

id    int not null primary key,

c1    varchar(30),

i2    int

) engine = myisam;

WHILE ai 

insert into t_06 values (ai,concat('AA',ai),ai);

SET ai = ai + 1;

END WHILE;

DROP TABLE IF EXISTS `lsb2`;

CREATE TABLE `lsb2` (

`id` bigint(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

set ai=1;

while ai<=1000 do

insert into lsb2 values (ai);

set ai=ai+1;

end while;

DROP TABLE IF EXISTS `lsbtest`;

CREATE TABLE `lsbtest` (

`id1` bigint(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into lsbtest

select

mid(mid(i,id,length(i)),2,LOCATE(',',mid(i,id,length(i)),2)-2)

from lsb2 where length(i)>=id and mid(i,id,1)=','

and length(mid(i,id,length(i)))>=2;

select * from  t_06 a inner join lsbtest b on a.id=b.id1;

END$$

DELIMITER ;

Total Time     : 00:00:47:719

95c521e29d6d1fe32f8fb445512b9fb0.gifWWWWA06-29 10:17

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

22楼

上一个调用:

call cfzfnew(',38068,238833,308799,274344,299910,309823,337486,290812,56393,64413,492511,260426,58626,450987,499220,187731,365665,212799,227788,255724,384636,465766,417310,313148,483987,328761,402876,237274,249183,174185,28187,189155,259940,67800,60065,340172,311667,354861,182,305523,115981,365082,213915,47894,131301,198754,358852,112496,404423,486725,233123,322936,325337,125932,299260,128791,295663,469897,120580,347766,34859,364204,37597,268974,351155,256955,214013,309192,412394,216800,30315,411242,16678,233247,359013,401666,30792,452394,408649,14159,5519,91705,227648,120966,319599,351170,68129,368701,233566,144256,156172,41972,499687,390955,6549,298079,498230,196397,239493,242037,');

show warnings;

IN方法:

call cfzxin('38068,238833,308799,274344,299910,309823,337486,290812,56393,64413,492511,260426,58626,450987,499220,187731,365665,212799,227788,255724,384636,465766,417310,313148,483987,328761,402876,237274,249183,174185,28187,189155,259940,67800,60065,340172,311667,354861,182,305523,115981,365082,213915,47894,131301,198754,358852,112496,404423,486725,233123,322936,325337,125932,299260,128791,295663,469897,120580,347766,34859,364204,37597,268974,351155,256955,214013,309192,412394,216800,30315,411242,16678,233247,359013,401666,30792,452394,408649,14159,5519,91705,227648,120966,319599,351170,68129,368701,233566,144256,156172,41972,499687,390955,6549,298079,498230,196397,239493,242037');

show warnings;

Total Time     : 00:00:18:234

DELIMITER $$

DROP PROCEDURE IF EXISTS `zz`.`cfzxin`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `cfzxin`(i varchar(8000))

BEGIN

DECLARE ai INT DEFAULT 1;

DROP TABLE IF EXISTS `t_06`;

create table t_06 (

id    int not null primary key,

c1    varchar(30),

i2    int

) engine = myisam;

WHILE ai 

insert into t_06 values (ai,concat('AA',ai),ai);

SET ai = ai + 1;

END WHILE;

set @qq=concat('select * from t_06 where id in(',i,')');

select @qq;

prepare dd from @qq;

execute dd;

END$$

DELIMITER ;

95c521e29d6d1fe32f8fb445512b9fb0.gifWWWWA06-29 10:52

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

23楼

用临时表方法,去掉生成LSB2(即生成1-1000数字的代码),加入主键

Total Time     : 00:00:18:438

DELIMITER $$

DROP PROCEDURE IF EXISTS `zz`.`cfzfnewa`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `cfzfnewa`(i varchar(5000))

BEGIN

DECLARE ai INT DEFAULT 1;

DROP TABLE IF EXISTS `t_06`;

create table t_06 (

id    int not null primary key,

c1    varchar(30),

i2    int

) engine = myisam;

WHILE ai 

insert into t_06 values (ai,concat('AA',ai),ai);

SET ai = ai + 1;

END WHILE;

DROP TABLE IF EXISTS `lsbtest`;

CREATE TABLE `lsbtest` (

`id1` bigint(10) primary key

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into lsbtest

select

mid(mid(i,id,length(i)),2,LOCATE(',',mid(i,id,length(i)),2)-2)

from lsb2 where length(i)>=id and mid(i,id,1)=','

and length(mid(i,id,length(i)))>=2;

select a.* from  t_06 a inner join lsbtest b on a.id=b.id1;

END$$

DELIMITER ;

与IN方法时间相差不多。

88b12a7552aefd99f8522ebd82205fcf.pngclaro06-29 11:12

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

24楼

引用 2 楼 liangCK 的回复:

先接个分..再看.

f549251b5aeabc65fbcfe86204d3fafd.gifhkz031806-29 13:43

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

25楼

2411d8773207af3118c10a0f7ec12065.gif

学习ing

ee6e1eb689e724447a89e35755d45741.gifvinsonshen06-29 16:08

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

26楼

引用 23 楼 WWWWA 的回复:

用临时表方法,去掉生成LSB2(即生成1-1000数字的代码),加入主键

insert into lsbtest

select

mid(mid(i,id,length(i)),2,LOCATE(',',mid(i,id,length(i)),2)-2)

from lsb2 where length(i)>=id and mid(i,id,1)=','

and length(mid(i,id,length(i)))>=2;

妙!

我的看法是:如果前面要查询的源表的数据量很大时,则把in这部分放进临时表并建立索引来关联的速度是最快的(当然源查询表对应字段也有索引),如果要把

查询动态化的话,我的看法跟WWWWA 一样,就是用存储过程的方式,把in那部分的值清单作为参数传进去,若in的清单比较多,则在存储过程内部的最后

对临时表进行人工处理(先truncate,再drop)。

其实这种需求,关键还要看源查询表的数据量及in的值清单的数据量。

1bba9c380d532f8061c11a1c83561b7f.gifwwwwb06-29 16:10

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

27楼

引用 26 楼 vinsonshen 的回复:

引用 23 楼 WWWWA 的回复:

用临时表方法,去掉生成LSB2(即生成1-1000数字的代码),加入主键

insert into lsbtest

select

mid(mid(i,id,length(i)),2,LOCATE(',',mid(i,id,length(i)),2)-2)

from lsb2 where length(i)>=id and mid(i,id,1)=','

and length(mid(i,id,length(i)))>=2;

妙!

我的看法是:如果前面要查询的源表的数据量很大时,则把in这部分放进临时表并建立索引来关联的速度是…

对,我倾向于用临时表方法,除效率外,还有个人的习惯,呵呵。

46fd183d62e9ed71bab7f5f62ce4833a.pngsciland06-30 08:56

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

28楼

先把100个数组放入一张临时表中,利用存储过程进行量表的联合查询

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

29楼

没怎么看懂,不过,有学到东西,还要谢谢楼主今天的帮忙,顶一下!

e77437820c1a0f052208415f10e11893.gifduxianghe07-03 17:36

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

30楼

我认为直接用In 是最快的。

655f3b2ba69e2ea008a312af3389c20c.pngzgycsmb07-04 11:33

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

31楼

jf

bcdceb842f491c6d252587079850bb14.pngcode_killer07-04 23:27

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

32楼

引用 30 楼 duxianghe 的回复:

我认为直接用In 是最快的。

我也是这么想的

d21b1ced974d2fd135234da57475c11f.pngzf21307-06 10:11

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

33楼

学习一下

1436b199c823b5385c642832e74f74fa.pngMSTOP07-08 21:45

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

34楼

我习惯是这样,放哈稀表里,然后 INNER JOIN .

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

35楼

其实在特大的高压下,你会发现IN的效率很高!

最近在帮客户解决了一个问题,就是把自己内部的临时表直接转成IN了。能够承受的压力更大!

1bba9c380d532f8061c11a1c83561b7f.gifwwwwb07-09 11:13

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

36楼

引用 35 楼 yueliangdao0608 的回复:

其实在特大的高压下,你会发现IN的效率很高!

最近在帮客户解决了一个问题,就是把自己内部的临时表直接转成IN了。能够承受的压力更大!

版主测试过没有,数据量多大时,IN的效率>临时表?谢谢

ee6e1eb689e724447a89e35755d45741.gifvinsonshen07-09 11:43

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

37楼

引用 35 楼 yueliangdao0608 的回复:

其实在特大的高压下,你会发现IN的效率很高!

最近在帮客户解决了一个问题,就是把自己内部的临时表直接转成IN了。能够承受的压力更大!

你的源表的数据量多大?

ee6e1eb689e724447a89e35755d45741.gifvinsonshen07-09 11:45

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

38楼

如果你数据量比较少,那可能in的效率比临时表还要快了

其实想下就知道,少了索引扫描、临时表的IO部分、磁头的移动臂减少等等方面。

1436b199c823b5385c642832e74f74fa.pngMSTOP07-09 16:10

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

39楼

引用 35 楼 yueliangdao0608 的回复:

其实在特大的高压下,你会发现IN的效率很高!

最近在帮客户解决了一个问题,就是把自己内部的临时表直接转成IN了。能够承受的压力更大!

这也有可能. IN ( ...... ) 经MYSQL优化后可能变成有序的内存表或数组了.

2f0af7f59f40c230c4cf3c805cbb5350.pngACMAIN_CHM07-09 16:16

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

40楼

从8楼,9楼的试验结果上,似乎还看不出 mysql 用临时表做 join 会比 直接 IN 有效率上的提高。

各位可以在其它数据库上试试

2f0af7f59f40c230c4cf3c805cbb5350.pngACMAIN_CHM07-09 21:34

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

41楼

个人总结一下以上的方法有:

1。 直接使用 IN (38068,238833,308799 ... )

2。 将 (38068,238833,308799 ... ) 放入临时表,然后用 JOIN

3。 直接在程序中 执行多个 select * where id = 38068; select * from where id=238833; ...

4。 使用 inner join (select 38068 union all select 238833 union all ... )

8a14b364b3764bb6b04aa439eea19e49.pngbllizard07-10 14:26

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

42楼

为什么用index就不行咧?

8e328298c5834e9ae659868355b0bee8.pngliulcster07-10 17:11

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

43楼

学习一下

77d5e876dc4d8fcf4a6cf17cc0eeda09.pnghardwin07-13 22:18

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

44楼

支持楼主!学习了~~~

我一般用in

343984f46709ed1121d42554023f83c6.gifhuolin07-14 15:51

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

45楼

关注此帖子,也遇到这个问题

目前是直接用的in的,源数据表有20万数据,in后面的数字有一百多个,感觉有些吃不清,建临时表并join没有试过

1fe04921be576667d8b2631e99220484.pngwt321676707-14 17:03

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

46楼

学习一下

2e348ac1ad888242984e6455b448cddd.pngbigfoot00107-15 08:43

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

47楼

UP

2b0f40f13927987ea3c3e44cb2b3a1df.pngflairsky07-15 11:29

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

48楼

围观,支持in

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

49楼

引用 41 楼 ACMAIN_CHM 的回复:

个人总结一下以上的方法有:

1。 直接使用 IN (38068,238833,308799 ... )

2。 将 (38068,238833,308799 ... ) 放入临时表,然后用 JOIN

3。 直接在程序中 执行多个 select * where id = 38068; select * from where id=238833; ...

4。 使用 inner join (select 38068 union all select 238833 union all ... )

方法很对。

不过效率就第一种高,其他的反而更低。

除非针对特定的业务,这样,其他的方法有可能比第一种效率高!

88e19e1b798ab2642d3aba8f8e91e59e.pngljf_ljf07-15 18:50

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

50楼

1 觉得楼主要这种测试比较意义不太大,不同数据分布查询速度会有一点点不同。

2 在MySQL 中,应该只有 in 和 join 表之间比较。

3 join 表比较多的时候会比 in 慢,因为 in 是在 buffer 中寻找对应内容;

直接使用 【fill_record】函数就完成;join 是需要创建临时表的额外消耗。

869a563354abc9070debe7551e376c10.gifcxyy_07-16 15:46

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

51楼

又学习了

e0d225709387016694fe6af25be08cdb.gifdawnco08-25 11:53

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

52楼

学习了

e812c3376819b3fad245fee5ddc76858.pngzhoupuyue09-03 13:06

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

53楼

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

54楼

版主 还没结贴呢

不过还有人把这找出来 也挺狠的

7a737da2bcda33bf5475497e023baeba.gifdenniswwh09-03 14:22

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

55楼

既然挖出来了,我也来接分

40f809c9674a390ae01dcbed84ab9c2b.gifComgarden09-03 14:30

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

56楼

mark

627d084b72843fde343b8694cfb6389b.pngzuoxingyu03-31 02:18

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

57楼

MARK

7a21f15dbcd7af1edcb78d6c2caef4e8.pngfuyongjie04-01 11:14

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

58楼

越到后边越越倾向用in了,呵呵

de13772ec9242e28d7b97ba629da7478.pngdong30804-02 18:21

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

59楼

楼上的牛X了.学习中.接分来了.

6a3ebd838f8760cac89309597cc0c561.pngshrimpma06-24 08:54

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

60楼

学习了,没想到有这么的方法

5d51c4f8150140659d334e823666917d.gifwubai25006-30 09:41

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

61楼

我也想知道,目前也遇到了同样的问题,如果我的in里有5W个Id或更多,结果会怎样呢。

0bdb0d7e3ed6a2d7abbef3c4e6e55c7c.pngm58244567207-09 17:51

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

62楼

mark

34046e74d955a84336f82752feb3980d.gifvga12-10 07:34

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

63楼

如果我的in里有5W个Id或更多,结果会怎样呢?

用 join

b346f3f8a528a7411d81586e0bc85c08.gifpopviv12-17 00:49

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

67楼

顶一下,我比较喜欢用in。把贴顶起来,大家汇总下经验

8ca4409e48782ecc3aa7174b24f0472f.pngtuzwu01-05 23:23

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

68楼

in ,吼吼.更倾向于用原生的方法来解决.

371b138dd6223d9ccb8b8bf710d0cfab.giffengyqf01-20 11:54

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

69楼

引用 63 楼 vga 的回复:

如果我的in里有5W个Id或更多,结果会怎样呢?

用 join

同意

3892b5d109a1da53990cfe8848e4bd31.pngedwinboyan02-26 12:21

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

70楼

好帖!

csdn这种贴多一些的话会感觉很好。

c1d14cc6f2fbd5054b29b04eb370491a.gifa358073706-20 17:10

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

72楼

帮顶!!!!!!!!!!!!

17e9f836af8190b93b78f12965bb8580.gifgarykiller06-27 15:18

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

73楼

mark

eb1627308829d1c79369132cfd4c37df.pngkxn30809-06 15:51

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

74楼

好贴啊,这种讨论贴多有意义啊

学习学习

39d7421f8b7e081b8c0de8c5641e38e4.pngjsnjmayang11-03 09:03

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

75楼

咋没下文了啊。这就算结贴了吗?   有人测试好了,出结果了吗??

e76beeb59d59e3c1ccaad95ef4b5a794.pngPhpNewnew12-12 20:36

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

76楼

不小心发了个月经贴...以后多来此地撒尿...

5dd3cf23a9a285f5e425918efbf9b78a.gifkitewell12-21 10:34

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

77楼

我想知道in里多于多少个id了,需要换成join

希望下次搜到的时候,有人回答了。

26f5e3e9867b65d6190bd271637ef0a0.gifa43591100404-26 15:23

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

79楼

到20W个ID,in就吃不消了,5W以内没问题,10W的话,in会比left join慢

29648b340cb5eb55387538b029580a65.gifcsdncb05-15 07:56

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

81楼

测试结果:近30W条数据,in(小于100个的主键),响应时间在毫秒级。

cd180c534cc5b5d6f374038da65b4934.pngrrttyyui06-20 14:20

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

82楼

那要是同时查询的个数多呢 像这样

。。。。。。。。。。。。。。。。in(1,2,3,4,5,6,7,8,9,10);

。。。。。。。。。。。。。。。。in(12,22,32,42,52,62,72,82,92,102);

。。。。。。。。。。。。。。。。in(11,21,31,41,51,61,7,81,9,10);

。。。。。。。。。。。。。。。。in(13,2,3,4,53,6,7,8,9,10);

。。。。。。。。。。。。。。。。in(1,2,32,4,5,6,74,84,9,10);

。。。。。。。。。。。。。。。。in(15,25,3,4,5,6,7,8,69,10);

我这儿使用后的结果,效率很慢,慢慢的jsp页面就卡死了

这个问题怎么解决 用什么可以替换in

5b2e58494238301f438533016aa04095.gifhesaer0108-24 10:53

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

83楼

你的字段建立索引了么?

引用 82 楼  的回复:

那要是同时查询的个数多呢 像这样

。。。。。。。。。。。。。。。。in(1,2,3,4,5,6,7,8,9,10);

。。。。。。。。。。。。。。。。in(12,22,32,42,52,62,72,82,92,102);

。。。。。。。。。。。。。。。。in(11,21,31,41,51,61,7,81,9,10);

。。。。。。。。。。。。。。。。in(13,2,3,4,53,6,7,……

66f1242617302533c46bfd1fd7301319.gifft_201110-25 09:12

等级

e900ebe6ad40b88eca7b3185d1eab7e1.png

84楼

mark下学习用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值