阿里规定超过3张表禁止JOIN,为啥?

开发 后端 开发工具
《阿里巴巴 Java 开发手册》里面写超过三张表禁止 join,这是为什么?

[[424463]]

图片来自 包图网

问题分析

对这个结论,你是否有怀疑呢?也不知道是哪位先哲说的不要人云亦云,今天我设计 sql,来验证这个结论。(实验没有从代码角度分析,目前达不到。可以把 MySQL 当一个黑盒,使用角度来验证这个结论)

验证结论的时候,会有很多发现,各位往后看。

实验环境

VMware 10+Centos 7.4+MySQL 5.7.22 ,Centos 7 内存 4.5G,4 核,50G 硬盘。MySQL 配置为 2G,特别说明硬盘是 SSD。

我的实验

有 4 张表,student 学生表,teacher 老师表,course 课程表,sc 中间关系表,记录了学生选修课程以及分数。

具体 sql 脚本,看文章结尾,我附上。中间我自己写了造数据的脚本,也在结尾。

实验是为解决一个问题的:查询选修“tname553”老师所授课程的学生中,成绩最高的学生姓名及其成绩 。

查询 sql 是:

  1. select Student.Sname,course.cname,score 
  2.     from Student,SC,Course ,Teacher 
  3.     where Student.s_id=SC.s_id and SC.c_id=Course.c_id  and sc.t_id=teacher.t_id 
  4.     and Teacher.Tname='tname553' 
  5.     and SC.score=(select max(score)from SC where sc.t_id=teacher.t_Id); 

我来分析一下这个语句:4 张表等值 join,还有一个子查询。算是比较简单的 sql 语句了(相比 ERP 动就 10 张表的哦,已经很简单了)。

我还会分解这个语句成 3 个简单的 sql:

  1. select max(score)  from SC ,Teacher where sc.t_id=teacher.t_Id and Teacher.Tname='tname553'
  2.   select sc.t_id,sc.s_id,score   from SC ,Teacher 
  3.   where sc.t_id=teacher.t_Id 
  4.   and score=590 
  5.   and Teacher.Tname='tname553'
  6.    select Student.Sname,course.cname,score 
  7.    from Student,SC ,course 
  8.    where Student.s_id=SC.s_id and  sc.s_id in (20769800,48525000,26280200) and course.c_id = sc.c_id; 

我来分析下:第一句,就是查询最高分,得到最高分 590 分。第二句就是查询出最高分的学生 id,得到:

  1. 20769800,48525000,26280200 

第三句就是查询出学生名字和分数。这样这 3 个语句的就可以查询出来成绩最高的学生姓名及其成绩。

接下来我会分别造数据:1 千万选课记录(一个学生选修 2 门课),造 500 万学生,100 万老师(一个老师带 5 个学生,挺高端的吧),1000 门课。

用上面查询语句查询。其中 sc 表我测试了下有索引和没有索引情况,具体见下表。

再接下来,我会造 1 亿选课记录(一个学生选修 2 门课)5000 万学生,1000 万老师,1000 门课。然后分别执行上述语句。最后我会在 oracle 数据库上执行上述语句。

下面两张表是测试结果:

仔细看上表,可以发现?

①步骤 3.1 没有在连接键上加索引,查询很慢,说明:“多表关联查询时,保证被关联的字段需要有索引”。

②步骤 6.1,6.2,6.3,换成简单 sql,在数据量 1 亿以上, 查询时间还能勉强接受。此时说明 MySQL 查询有些吃力了,但是仍然嫩查询出来。

③步骤 5.1,MySQL 查询不出来,4 表连接,对我本机 MySQL 来说,1.5 亿数据超过极限了(我调优过这个 SQL,执行计划和索引都走了,没有问题,show profile 显示在 sending data,这个问题另外文章详谈)。

④对比 1.1 和 5.1 步骤 sql 查询,4 表连接,对我本机 MySQL 来说 ,1.5 千万数据查询很流利,是一个 MySQL 数据量流利分水岭。(这个只是现象,不太准确,需要同时计算表的容量)。

⑤步骤 5.1 对比 6.1,6.2,6.3,多表 join 对 MySQL 来说,处理有些吃力。

⑥超过三张表禁止 join,这个规则是针对 MySQL 来说的。后续会看到我用同样机器,同样数据量,同样内存,可以完美计算 1.5 亿数据量 join。

针对这样一个规则,对开发来说 ,需要把一些逻辑放到应用层去查询。

总结:这个规则,超过三张表禁止 join,由于数据量太大的时候,MySQL 根本查询不出来,导致阿里出了这样一个规定。

其实如果表数据量少,10 张表也不成问题,你自己可以试试。而我们公司支付系统朝着大规模高并发目标设计的,所以,遵循这个规定。

在业务层面来讲,写简单 sql,把更多逻辑放到应用层,我的需求我会更了解,在应用层实现特定的 join 也容易得多。

让我们来看看 oracle 数据库的优秀表现:

看步骤 7.1,就是没有索引,join 表很多的情况下,oracle 仍然 26 秒查询出结果来。所以我会说 MySQL 的 join 很弱。

那么问题来了,为什么现在使用很多人使用 MySQL 呢?这是另外一个问题,我会另外说下我的思考。

看完本篇文章,另外我还附加赠送,所谓搂草打兔子。就是快速造数据。你可以自己先写脚本造数据,看看我是怎么造数据的,就知道我的技巧了。

附上部分截图:

附上 sql 语句和造数据脚本:

  1. use stu; 
  2. drop table if exists student; 
  3. create table student 
  4.   (  s_id int(11) not null auto_increment , 
  5.      sno    int(11), 
  6.      sname varchar(50), 
  7.      sage  int(11), 
  8.      ssex  varchar(8) , 
  9.      father_id int(11), 
  10.       mather_id int(11), 
  11.       note varchar(500), 
  12.      primary key (s_id), 
  13.    unique key uk_sno (sno) 
  14.   ) engine=innodb default charset=utf8mb4; 
  15. truncate table student; 
  16.   delimiter $$ 
  17. drop function if exists   insert_student_data $$ 
  18. create function insert_student_data() 
  19.  returns  int deterministic 
  20.     begin 
  21.     declare  i int
  22.       set i=1; 
  23.       while  i<50000000 do 
  24.       insert into student  values(i ,i, concat('name',i),i,case when floor(rand()*10)%2=0 then 'f' else 'm' end,floor(rand()*100000),floor(rand()*1000000),concat('note',i) ); 
  25.       set i=i+1; 
  26.       end while; 
  27.       return 1; 
  28.     end$$ 
  29. delimiter ; 
  30. select  insert_student_data(); 
  31. select count(*) from student; 
  32. use stu; 
  33. create table course 
  34.   ( 
  35.      c_id int(11) not null auto_increment , 
  36.      cname varchar(50) 
  37.      note varchar(500), primary key (c_id) 
  38.   )  engine=innodb default charset=utf8mb4; 
  39. truncate table course; 
  40.   delimiter $$ 
  41. drop function if exists   insert_course_data $$ 
  42. create function insert_course_data() 
  43.  returns  int deterministic 
  44.     begin 
  45.     declare  i int
  46.       set i=1; 
  47.       while  i<=1000 do 
  48.       insert into course  values(i , concat('course',i),floor(rand()*1000),concat('note',i) ); 
  49.       set i=i+1; 
  50.       end while; 
  51.       return 1; 
  52.     end$$ 
  53. delimiter ; 
  54. select  insert_course_data(); 
  55. select count(*) from course; 
  56. use stu; 
  57. drop table if exists sc; 
  58. create table sc 
  59.   ( 
  60.      s_id    int(11), 
  61.      c_id    int(11), 
  62.      t_id    int(11), 
  63.      score int(11) 
  64.   )  engine=innodb default charset=utf8mb4; 
  65. truncate table sc; 
  66.   delimiter $$ 
  67. drop function if exists   insert_sc_data $$ 
  68. create function insert_sc_data() 
  69.  returns  int deterministic 
  70.     begin 
  71.     declare  i int
  72.       set i=1; 
  73.       while  i<=50000000 do 
  74.       insert into sc  values( i,floor(rand()*1000),floor(rand()*10000000),floor(rand()*750)) ; 
  75.       set i=i+1; 
  76.       end while; 
  77.       return 1; 
  78.     end$$ 
  79. delimiter ; 
  80. select  insert_sc_data(); 
  81. commit
  82. select  insert_sc_data(); 
  83. commit
  84. create index idx_s_id  on sc(s_id)   ; 
  85. create index idx_t_id  on sc(t_id)   ; 
  86. create index idx_c_id  on sc(c_id)   ; 
  87. select count(*) from sc; 
  88. use stu; 
  89. drop table if exists teacher; 
  90. create table teacher 
  91.   ( 
  92.     t_id  int(11) not null auto_increment , 
  93.      tname varchar(50) , 
  94.      note varchar(500),primary key (t_id) 
  95.   )  engine=innodb default charset=utf8mb4; 
  96.  
  97.   truncate table teacher; 
  98.   delimiter $$ 
  99. drop function if exists   insert_teacher_data $$ 
  100. create function insert_teacher_data() 
  101.  returns  int deterministic 
  102.     begin 
  103.     declare  i int
  104.       set i=1; 
  105.       while  i<=10000000 do 
  106.       insert into teacher  values(i , concat('tname',i),concat('note',i) ); 
  107.       set i=i+1; 
  108.       end while; 
  109.       return 1; 
  110.     end$$ 
  111. delimiter ; 
  112. select  insert_teacher_data(); 
  113. commit
  114. select count(*) from teacher; 

这个是 oracle 的测试和造数据脚本:

  1. create tablespace scott_data  datafile  '/home/oracle/oracle_space/sitpay1/scott_data.dbf'  size 1024m autoextend on
  2. create tablespace scott_index   datafile  '/home/oracle/oracle_space/sitpay1/scott_index.dbf'  size 64m  autoextend on
  3. create temporary tablespace scott_temp  tempfile  '/home/oracle/oracle_space/sitpay1/scott_temp.dbf'  size 64m autoextend on
  4. drop user  scott cascade
  5. create user  scott  identified by  tiger  default tablespace scott_data  temporary tablespace scott_temp  ; 
  6. grant resource,connect,dba to  scott; 
  7. drop table student; 
  8. create table student 
  9.   (  s_id number(11) , 
  10.      sno    number(11) , 
  11.      sname varchar2(50), 
  12.      sage  number(11), 
  13.      ssex  varchar2(8) , 
  14.      father_id number(11), 
  15.       mather_id number(11), 
  16.       note varchar2(500) 
  17.   ) nologging; 
  18. truncate table student; 
  19. create or replace procedure insert_student_data 
  20.  is 
  21.    q number(11); 
  22.     begin 
  23.      q:=0; 
  24.       for i in  1..50 loop 
  25.       insert /*+append*/ into student   select rownum+q as s_id,rownum+q  as sno, concat('sutdent',rownum+q ) as sname,floor(dbms_random.value(1,100)) as sage,'f' as ssex,rownum+q  as father_id,rownum+q  as mather_id,concat('note',rownum+q ) as note from dual connect by level<=1000000; 
  26.       q:=q+1000000; 
  27.       commit
  28.       end loop; 
  29. end insert_student_data; 
  30. call insert_student_data(); 
  31. alter table student  add constraint  pk_student primary key (s_id); 
  32. commit
  33. select count(*) from student; 
  34. create table course 
  35.   ( 
  36.      c_id number(11) primary key
  37.      cname varchar2(50), 
  38.      note varchar2(500) 
  39.   )  ; 
  40. truncate table course; 
  41.  create or replace procedure insert_course_data 
  42.  is 
  43.    q number(11); 
  44.     begin 
  45.  
  46.       for i in  1..1000 loop 
  47.       insert /*+append*/ into course  values(i , concat('name',i),concat('note',i) ); 
  48.       end loop; 
  49. end insert_course_data; 
  50. call insert_course_data(); 
  51. commit
  52. select count(*) from course; 
  53. create table sc 
  54.   ( 
  55.      s_id    number(11), 
  56.      c_id    number(11), 
  57.      t_id    number(11), 
  58.      score number(11) 
  59.   ) nologging; 
  60. truncate table sc; 
  61.  create or replace procedure insert_sc_data 
  62.  is 
  63.    q number(11); 
  64.     begin 
  65.      q:=0; 
  66.       for i in  1..50 loop 
  67.       insert /*+append*/ into sc   select rownum+q as s_id, floor(dbms_random.value(0,1000))  as c_id,floor(dbms_random.value(0,10000000)) t_id,floor(dbms_random.value(0,750)) as score from dual connect by level<=1000000; 
  68.       q:=q+1000000; 
  69.       commit
  70.       end loop; 
  71. end insert_sc_data; 
  72. call insert_sc_data(); 
  73. create index idx_s_id  on sc(s_id)   ; 
  74. create index idx_t_id  on sc(t_id)   ; 
  75. create index idx_c_id  on sc(c_id)   ; 
  76. select count(*) from sc; 
  77. create table teacher 
  78.   ( 
  79.     t_id  number(11) , 
  80.      tname varchar2(50) , 
  81.      note varchar2(500) 
  82.   )nologging ; 
  83.     truncate table teacher; 
  84. create or replace procedure insert_teacher_data 
  85.  is 
  86.    q number(11); 
  87.     begin 
  88.      q:=0; 
  89.       for i in  1..10 loop 
  90.       insert /*+append*/ into teacher   select rownum+q as t_id, concat('teacher',rownum+q ) as tname,concat('note',rownum+q ) as note from dual connect by level<=1000000; 
  91.       q:=q+1000000; 
  92.       commit
  93.       end loop; 
  94. end insert_teacher_data; 
  95. call insert_teacher_data(); 
  96. alter table teacher  add constraint  pk_teacher primary key (t_id); 
  97. select count(*) from teacher; 

作者:e71hao

编辑:陶家龙

出处:blog.itpub.net/30393770/viewspace-2650450/

 

责任编辑:武晓燕 来源: blog.itpub.net
相关推荐

2020-11-12 07:49:18

MySQL

2022-09-05 10:06:21

MySQL外循环内循环

2022-02-22 12:51:39

存储过程JavaSQL

2021-06-17 06:19:20

存储SQL数据库

2009-04-02 08:28:17

2019-03-18 15:00:48

SQLJoin用法数据库

2021-07-28 22:45:32

人脸识别生物识别信息安全

2023-11-16 12:34:00

MySQLjoin

2021-06-26 07:18:15

JscodeshiftBabel节点

2022-01-27 07:40:27

iOS微信朋友圈

2010-05-04 14:10:53

Oracle表

2018-09-19 21:34:12

阿里云芯片达摩院

2022-01-11 10:51:29

FlinkJoin数据

2016-09-23 09:29:08

mysq分区表l互联网

2019-03-04 13:54:18

MySQL分区表数据

2023-01-09 08:43:34

2020-09-08 16:25:18

Apache BeancopyJava

2010-09-16 16:06:01

sql server表

2020-06-22 08:23:42

阿里技术架构图

2022-07-13 12:53:59

数据存储
点赞
收藏

51CTO技术栈公众号