IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

MySQL高级知识(十六)——小表驱动大表

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

前言:本来小表驱动大表的知识应该在前面就讲解的,但是由于之前并没有学习数据批量插入,因此将其放在这里。在查询的优化中永远小表驱动大表。


1.为什么要小表驱动大表呢

类似循环嵌套

for(int i=5;.......)
{
     for(int j=1000;......)
     {}
}

如果小的循环在外层,对于数据库连接来说就只连接5次,进行5000次操作,如果1000在外,则需要进行1000次数据库连接,从而浪费资源,增加消耗。这就是为什么要小表驱动大表。

2.数据准备

根据MySQL高级知识(十)——批量插入数据脚本中的相应步骤在tb_dept_bigdata表中插入100条数据,在tb_emp_bigdata表中插入5000条数据。

77_1.png

77_2.png

注:100个部门,5000个员工。tb_dept_bigdata(小表),tb_emp_bigdata(大表)。

3.案例演示

①当B表的数据集小于A表数据集时,用in优于exists。

select *from tb_emp_bigdata A where A.deptno in (select B.deptno from tb_dept_bigdata B)

B表为tb_dept_bigdata:100条数据,A表tb_emp_bigdata:5000条数据。

用in的查询时间为:

77_3.png

将上面sql转换成exists:

select *from tb_emp_bigdata A where exists(select 1 from tb_dept_bigdata B where B.deptno=A.deptno);

用exists的查询时间:

77_4.png

经对比可看到,在B表数据集小于A表的时候,用in要优于exists,当前的数据集并不大,所以查询时间相差并不多。

②当A表的数据集小于B表的数据集时,用exists优于in。

select *from tb_dept_bigdata A where A.deptno in(select B.deptno from tb_emp_bigdata B);

用in的查询时间为:

77_5.png

将上面sql转换成exists:

select *from tb_dept_bigdata A where exists(select 1 from tb_emp_bigdata B where B.deptno=A.deptno);

用exists的查询时间:

77_6.png

由于数据量并不是很大,因此对比并不是难么的强烈。

附上视频的结论截图:

77_7.png

4.总结

下面结论都是针对in或exists的。

in后面跟的是小表,exists后面跟的是大表。

简记:in小,exists大。

对于exists

select …..from table where exists(subquery);

可以理解为:将主查询的数据放入子查询中做条件验证,根据验证结果(true或false)来决定主查询的数据是否得以保留。

https://www.cnblogs.com/developer_chan/category/1020652.html

文章永久链接:https://tech.souyunku.com/?p=13754


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(76) 打赏



未经允许不得转载:搜云库技术团队 » MySQL高级知识(十六)——小表驱动大表

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367