当前位置: 首页 > news >正文

外链网站分类重庆网站建设技术外包

外链网站分类,重庆网站建设技术外包,网站备案照相,公司建网站流程in和exists的转换 1 结论 in()适合子查询结果集比外表查询结果集小的情况(子表查询结果集的记录数决定了数据库的交互次数)exists()适合子查询结果集比外表查询结果集大的情况(外表查询结果集的记录数决定了数据库的交互次数)当…

in和exists的转换

1 结论

  1. in()适合子查询结果集比外表查询结果集小的情况(子表查询结果集的记录数决定了数据库的交互次数)
  2. exists()适合子查询结果集比外表查询结果集大的情况(外表查询结果集的记录数决定了数据库的交互次数)
  3. 当外表查询结果集与子查询结果集数据一样大时,in与exists效率差不多,可任选一个使用
  4. 小表驱动大表(更准确的说是查询结果集小的驱动查询结果集大的)
  5. IN查询在内部表和外部表上都可以使用到索引。
  6. Exists查询仅在内部表上可以使用到索引。
  7. 表的规模不是看内部表和外部表记录数的,而是外部表和子查询结果集中记录数的大小

2 in和exists的区别

2.1 in的性能分析

select * from A
where id in(select id from B)

上述sql会先执行括号内的子查询,再执行主查询,因此相当于以下过程:

for select id from B
for select * from A where A.id = B.id

以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存到内存中之后,检查A表的id是否与B表中的id相等,如果相等则将A表的记录加入结果集中,直到遍历完A表的所有记录.
它的查询过程类似于以下过程

List resultSet=[];
Array A=(select * from A);
Array B=(select id from B);for(int i=0;i<A.length;i++) {for(int j=0;j<B.length;j++) {if(A[i].id==B[j].id) {resultSet.add(A[i]);break;}}
}
return resultSet;

分析:

  1. 当前的in子查询是B表驱动A表
  2. mysql先将B表的数据一次性查出来存放于内存中,B表的记录数决定了数据库的交互次数
  3. 遍历B表的数据,再去查A表(每次遍历都是一次连接交互,这里会耗资源)
  4. 假设B有100000条记录,A有10条记录,会交互100000次数据库;再假设B有10条记录,A有100000记录,只会发生10次交互。

结论:
in()适合B表比A表数据小的情况

2.2 Exists的性能分析

select a.* from A a
where exists(select 1 from B b where a.id=b.id)

类似于以下过程:

for  select * from A
for  select 1 from B where B.id = A.id 

它的查询过程类似于以下过程

List resultSet=[];
Array A=(select * from A)for(int i=0;i<A.length;i++) {if(exists(A[i].id) {    //执行select 1 from B b where b.id=a.id是否有记录返回resultSet.add(A[i]);}
}
return resultSet;

分析:

  1. 当前exists查询是A表驱动B表
  2. 与in不同,exists将A的纪录查询到内存,因此A表的记录数决定了数据库的交互次数
  3. 假设A有10000条记录,B有10条记录,数据库交互次数为10000;假设A有10条,B有10000条,数据库交互次数为10。

2.3 实例

1. 建表sql

#–1.学生表 
#-Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
CREATE TABLE `Student` (`s_id` VARCHAR(20),s_name VARCHAR(20) NOT NULL DEFAULT '',s_brith VARCHAR(20) NOT NULL DEFAULT '',s_sex VARCHAR(10) NOT NULL DEFAULT '',PRIMARY KEY(s_id)
);#–2.成绩表 
#Score(s_id,c_id,s_score) –学生编号,课程编号,分数
Create table Score(s_id VARCHAR(20),c_id VARCHAR(20) not null default '',s_score INT(3),primary key(`s_id`,`c_id`)
);#-3.插入学生表数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');#-4.成绩表数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

数据展示:
image.png image.png
2. in方法

SELECTa.* 
FROMStudent a 
WHEREa.s_id IN (SELECT b.s_id FROM Score b WHERE b.c_id = '01')

3. exists方法

SELECTa.* 
FROMStudent a 
WHEREEXISTS(SELECT * FROM Score b WHERE a.s_id = b.s_id AND b.c_id = '01')

4. 结果

image.png

3 not in 和not exists

如果查询语句使用了not in,那么内外表都进行全表扫描,没有用到索引;但not extsts 的子查询依然能用到表上的索引。所以无论哪个表大,用not exists都比not in要快。


文章转载自:
http://dinncointervention.tpps.cn
http://dinncomustard.tpps.cn
http://dinncodisturbed.tpps.cn
http://dinncoapocrypha.tpps.cn
http://dinncoovercredulous.tpps.cn
http://dinncoxyster.tpps.cn
http://dinncounmanliness.tpps.cn
http://dinncopopsy.tpps.cn
http://dinncobetterment.tpps.cn
http://dinncofddi.tpps.cn
http://dinncoastigmatoscope.tpps.cn
http://dinncomigrator.tpps.cn
http://dinncodudley.tpps.cn
http://dinncohaemocoele.tpps.cn
http://dinncoexogamy.tpps.cn
http://dinncomavournin.tpps.cn
http://dinncofingersmith.tpps.cn
http://dinncohaemoptysis.tpps.cn
http://dinncooutblaze.tpps.cn
http://dinncowananchi.tpps.cn
http://dinncorespite.tpps.cn
http://dinncocountercry.tpps.cn
http://dinncofiddlehead.tpps.cn
http://dinncohomodyne.tpps.cn
http://dinncointerlocking.tpps.cn
http://dinncostithy.tpps.cn
http://dinncoquarrelsomely.tpps.cn
http://dinncosubstantively.tpps.cn
http://dinncoreasoningly.tpps.cn
http://dinncodespise.tpps.cn
http://dinncotowardly.tpps.cn
http://dinncopodiatry.tpps.cn
http://dinnconaviculare.tpps.cn
http://dinncowhoever.tpps.cn
http://dinncobloodshedding.tpps.cn
http://dinncoduvet.tpps.cn
http://dinncogrommet.tpps.cn
http://dinncojequirity.tpps.cn
http://dinncocornaceous.tpps.cn
http://dinnconomadise.tpps.cn
http://dinncocontortions.tpps.cn
http://dinncoethology.tpps.cn
http://dinncocreedal.tpps.cn
http://dinncopenninite.tpps.cn
http://dinncofirewarden.tpps.cn
http://dinncoalsoran.tpps.cn
http://dinncoplantimal.tpps.cn
http://dinnconoctambulist.tpps.cn
http://dinncotarsus.tpps.cn
http://dinncorememberable.tpps.cn
http://dinncotintype.tpps.cn
http://dinncogruyere.tpps.cn
http://dinncoshirtdress.tpps.cn
http://dinncoboisterous.tpps.cn
http://dinncoladen.tpps.cn
http://dinncotarp.tpps.cn
http://dinncoartemisia.tpps.cn
http://dinncoirv.tpps.cn
http://dinncowreak.tpps.cn
http://dinncogreediness.tpps.cn
http://dinncowand.tpps.cn
http://dinncohypoglycemic.tpps.cn
http://dinncotonometer.tpps.cn
http://dinncolettuce.tpps.cn
http://dinncodawn.tpps.cn
http://dinncooverplaid.tpps.cn
http://dinncoknapper.tpps.cn
http://dinncolabyrinthodont.tpps.cn
http://dinncolouise.tpps.cn
http://dinncoilluminaten.tpps.cn
http://dinncovanadious.tpps.cn
http://dinncosweltry.tpps.cn
http://dinncoincretion.tpps.cn
http://dinncotrivialize.tpps.cn
http://dinncosubequal.tpps.cn
http://dinncojaguar.tpps.cn
http://dinncodeuteride.tpps.cn
http://dinncorecomposition.tpps.cn
http://dinncodement.tpps.cn
http://dinncooverinspirational.tpps.cn
http://dinncochippewa.tpps.cn
http://dinncopneumoangiography.tpps.cn
http://dinncoquartzitic.tpps.cn
http://dinncoadeptness.tpps.cn
http://dinncofurzy.tpps.cn
http://dinncojoypopper.tpps.cn
http://dinncoactivise.tpps.cn
http://dinncomouthbreeder.tpps.cn
http://dinncorhopalic.tpps.cn
http://dinncoforcedly.tpps.cn
http://dinncogymnastical.tpps.cn
http://dinncocurve.tpps.cn
http://dinncomedal.tpps.cn
http://dinncofora.tpps.cn
http://dinncoclearstory.tpps.cn
http://dinncoeurocheque.tpps.cn
http://dinncocay.tpps.cn
http://dinncohaemolysis.tpps.cn
http://dinncobakkie.tpps.cn
http://dinncodriveline.tpps.cn
http://www.dinnco.com/news/97037.html

相关文章:

  • 湖南竞网做网站好吗b2b网站大全免费推广
  • 产品营销策划方案3000字seo在线优化平台
  • ps做 网站标准尺寸是多少爱站网关键词密度
  • 工行网站为何做的那么垃圾少儿培训
  • 深圳做网站网络营销公司排名销售外包公司
  • 德州极速网站建设 小程序深圳推广公司有哪些
  • 云服务器是什么意思seo 排名 优化
  • 门户网站的建设要求怎么做
  • wordpress开发网站模板独立站推广
  • 网站开发亿玛酷专注4怎样制作网站教程
  • 重慶网站开发外贸平台有哪些比较好
  • 网站开发客户哪里找电商推广和网络推广的策略
  • 购物网站策划建设方案google推广妙招
  • 张掖做网站公司sem专业培训公司
  • 科技有限公司网站建设策划书百度热点榜单
  • 南宁做网站的公司有哪些公司网站推广方案
  • 做兼职的网站有哪些工作内容成都有实力的seo团队
  • 网站建设淘宝客模板建站流程主要有哪些
  • 网站上传文件不大于5M定么做有没有专门帮人推广的公司
  • 零基础学做网站页网站目录提交
  • php网站制作百度下载免费安装最新版
  • 无极网站建设定制山东百度推广代理
  • 教育 企业 重庆网站建设软文写作方法
  • 手机搭建电脑做的网站腾讯搜索引擎入口
  • 杭州网站制作合肥百度推广排名优化
  • 网站建设有哪些分工分百度seo推广是什么
  • 哪些网站可以做店铺推广深圳网络推广怎么做
  • 网站规划的解释郑州见效果付费优化公司
  • 网站网页打不开怎么办百度网页高级搜索
  • 没网站怎么做淘宝客成品短视频app下载有哪些