MySQL-技术专题-实战技巧

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" join用于多表中字段之间的联系,在数据库的"},{"type":"text","marks":[{"type":"strong"}],"text":"DML"},{"type":"text","text":" (数据操作语言,即各种增删改查操作)中有着重要的作用。"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 合理使用Join语句优化SQL有利于:"}]},{"type":"horizontalrule"},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"增加数据库的处理效率,减少响应时间;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"减少数据库服务器负载,增加服务器稳定性;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"减少服务器通讯的网络流量;"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1.Join的分类:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"内连接 Inner Join"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"全外连接 FULL Outer Join"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"左外连接 Left Outer Join"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"右外连接 Right Outer Join"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"交叉连接 Cross Join"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"连接的分类"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 每种连接的区别作为基础内容,这里就不再展开说明,请读者自己参看其他文章了解,比如"},{"type":"link","attrs":{"href":"https://link.jianshu.com?t=https%3A%2F%2Fwww.cnblogs.com%2Fblueoverflow%2Fp%2F4714470.html","title":null},"content":[{"type":"text","text":"Mysql Join语法以及性能优化"}]}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 需要说明的是,目前MySQL不支持全连接,需要使用"},{"type":"text","marks":[{"type":"strong"}],"text":"UNION"},{"type":"text","text":"关键字进行联合。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Union"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Union All"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":对两个结果集进行并集操作,包括重复行,不进行排序;"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2.Join使用的注意事项"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面进行本文重点,Join的使用注意事项和技巧,首先给出要使用的表结构:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"-- auto-generated definition\nCREATE TABLE customer\n(\n id INT AUTO_INCREMENT PRIMARY KEY,\n cust_name VARCHAR(50) NOT NULL CHARSET utf8,\n over VARCHAR(100) NULL CHARSET utf8,\n CONSTRAINT customer_id_uindex\n UNIQUE (id)\n)\nENGINE = InnoDB;\n\n-- auto-generated definition\nCREATE TABLE faculty\n(\n id INT AUTO_INCREMENT PRIMARY KEY,\n user_name VARCHAR(50) NOT NULL CHARSET utf8,\n over VARCHAR(200) NULL CHARSET utf8,\n CONSTRAINT faculty_id_uindex\n UNIQUE (id)\n)\nENGINE = InnoDB;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ac/ac0c055e95732674948b825cfc3b2e6f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ff/fff77ce9bce961e4c82f93071b6f1b8a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1 显式连接 VS 隐式连接"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所谓显式连接,即如上显示使用"},{"type":"text","marks":[{"type":"strong"}],"text":"inner Join"},{"type":"text","text":"关键字连接两个表,"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from table a inner join table b on a.id = b.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而隐式连接即不显示使用"},{"type":"text","marks":[{"type":"strong"}],"text":"inner Join"},{"type":"text","text":"关键字,如:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select a.*, b.* from table a, table b where a.id = b.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" 二者在功能上没有差别,实现的性能上也几乎一样。只不过隐式连接是"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"SQL92"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"中的标准内容,而在"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"SQL99"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"中显式连接为标准,虽然很多人还在用隐私连接,但是它已经从标准中被移除。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" 从使用的角度来说,还是推荐使用显示连接,这样可以更清楚的显示出多个表之间的连接关系和连接依赖的属性。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2 On VS Where"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有列为 NULL 的数据,在匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE 子句条件才会被使用。ON将从匹配阶段产生的数据中检索过滤。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"所以我们要注意:在使用Left (right) join的时候,一定要在先给出尽可能多的匹配满足条件,减少Where的执行。尽可能满足ON的条件,而少用Where的条件,从执行性能来看也更加高效。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3 Join的技巧"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.1 过虑条件更新自身表"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 假设现在要将是职工中的消费者的“over”属性设置为\"优惠\",直接如下更新会报错:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6ee74aeaaf1cb579b7e481efe6c4819b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 这是由于Mysql不支持这种查询后更新(这其实是标准SQL中一项要求,Oracle、SQL Server中都是可以的)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 为了解决这种更新的过虑条件中包含要更新的表的情况,可以把带过滤条件的查询结果当做一个新表,在新表上,执行更新操作。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"UPDATE (faculty f INNER JOIN customer c\n on user_name=cust_name)\nset c.over = \"优惠\";"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0b/0b57d5c0c62ad8c0417e976425306292.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更新成功"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.2 Join优化子查询"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 嵌套的子查询是比较低效地,因为每一条记录都要进行匹配,如果记录长度比较大的话,那么我们的查询就有可能非常的耗时。我们应该尽量避免使用子查询,而用表连接。如下面的这个子查询就可以转化为等价的连接查询"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"SELECT user_name, over ,(SELECT over FROM customer c where user_name=cust_name) as over2\nfrom faculty f;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"SELECT user_name, f.over , c.over as over2\nfrom faculty f\n LEFT JOIN customer c ON cust_name=user_name;\n"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.3 Join优化聚合查询"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了说明这个问题 ,我们在添加一个工作量的表,记录每个职工每天的工作量"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"-- auto-generated definition\nCREATE TABLE tasks\n(\n id SMALLINT(5) UNSIGNED AUTO_INCREMENT\n PRIMARY KEY,\n facult_id SMALLINT(5) UNSIGNED NULL,\n timestr TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,\n workload SMALLINT(5) UNSIGNED NULL\n)\nENGINE = InnoDB CHARSET = utf8;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e2849f87bce5d9efaa0ccde99d9739d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tasks记录职工的工作量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如我们想查询每个员工工作量最多是哪一天,通过子查询可以这样实现:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select a.user_name ,b.timestr,b.workload from faculty a join tasks b\non a.id = b.facult_id\nwhere b.workload = (\n select max(c.workload)\n from tasks c\n where c.facult_id = b.facult_id)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查询结果"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用表连接优化之后:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"SELECT user_name, t.timestr, t.workload\nFROM faculty f\n JOIN tasks t ON f.id = t.facult_id\n JOIN tasks t2 ON t2.facult_id = t.facult_id\nGROUP BY user_name,t.timestr,t.workload\nHAVING t.workload = max(t2.workload);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 这里额外的再连接了一个task表中内容,在这个“额外表”中通过聚合计算出工作量的最大值,然后再过虑(HAVING)出工作量最大的日期。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"因为聚合函数通过作用于一组数据而只返回一个单个值,因此,在SELECT语句中出现的元素要么为一个聚合函数的输入值,要么为GROUP BY语句的参数,否则会出错。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 但是mysql的"},{"type":"text","marks":[{"type":"strong"}],"text":"group by"},{"type":"text","text":"做过"},{"type":"text","marks":[{"type":"strong"}],"text":"扩展"},{"type":"text","text":"了,select之后的列允许其不出现在group by之后,MySQL在执行这类查询语句时,它会默认理解为,没写到GROUP BY子句的列,其列值是唯一的,如果GROUP BY省略的列值其实并不唯一,将会默认取第一个获得的值,这样就会指代不明,那么最好不要使用这项功能。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.4 如何实现分组查询"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 要获取每个员工完成工作量最多的两天。这个也可以通过Join来完成。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select d.user_name,c.timestr,workload\nFROM (\n select facult_id,timestr,workload,\n (SELECT COUNT(*)\n FROM tasks b\n WHERE b.facult_id=a.facult_id AND a.workload<=b.workload) AS cnt\n FROM tasks a\n GROUP BY facult_id,timestr,workload) c\nJOIN faculty d ON c.facult_id=d.id\nWHERE cnt <= 2;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 其中,内部的查询结果"},{"type":"text","marks":[{"type":"strong"}],"text":"cnt"},{"type":"text","text":"表示对于tasks表中某个给定记录,相同员工的工作里记录比其大的数量有多少。"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 内部查询的结果如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select facult_id,timestr,workload,\n (SELECT COUNT(*)\n FROM tasks b\n WHERE b.facult_id=a.facult_id AND a.workload<=b.workload) AS cnt\nFROM tasks a\nGROUP BY facult_id,timestr,workload;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"内部查询的结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即每个工作量记录信息和同一员工的工作量排名。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"cnt <= 2"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就代表该记录是某位员工的工作量最大两天之一。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/36/365d1fa5a941f0eadb1a1ea44e74182f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每个员工完成工作量最多的两"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"4. join的实现原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" join的实现是采用Nested Loop Join算法,就是通过驱动表的结果集作为循环基础数据,然后一条一条的通过该结果集中的数据作为过滤条件到下一个表中查询数据,然后合并结果。如果有多个join,则将前面的结果集作为循环数据,再一次作为循环条件到后一个表中查询数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如我们以如下SQL语句为例:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"EXPLAIN \nSELECT C.id, cust_name,T.workload\nFROM customer C\n INNER JOIN faculty F\n ON C.cust_name = F.user_name\n INNER JOIN tasks T\n ON T.facult_id = F.id ;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/80/80da352983871cfc0419d5dbb19485b3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"EXPLAIN 连接查询"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 从"},{"type":"text","marks":[{"type":"strong"}],"text":"explain"},{"type":"text","text":"的输出看出,MySQL选择"},{"type":"text","marks":[{"type":"strong"}],"text":"C"},{"type":"text","text":"作为驱动表,"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 首先通过"},{"type":"text","marks":[{"type":"italic"}],"text":"Using Where"},{"type":"text","text":"和"},{"type":"text","marks":[{"type":"italic"}],"text":"Using join buffer"},{"type":"text","text":"来匹配"},{"type":"text","marks":[{"type":"strong"}],"text":"F"},{"type":"text","text":"中的内容,然后在其结果的基础上通过主键的索引"},{"type":"text","marks":[{"type":"italic"}],"text":"PRIMARY,faculty_id_uindex"},{"type":"text","text":"匹配到"},{"type":"text","marks":[{"type":"strong"}],"text":"T"},{"type":"text","text":"表中的内容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其过程类似于三次次嵌套的循环。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"需要说明的是,C作为驱动表,通过"},{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Using Where"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"和"},{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Using join buffer"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"来匹配F,是因为"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"C.cust_name ,F.user_name"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"都没有加索引,要获取具体的内容只能通过对全表的数据进行where过滤才能获取,而"},{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Using join buffer"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"是指使用到了Cache(只有当join类型为ALL,index,rang或者是index_merge的时候才会使用join buffer),记录已经查询的结果,提高效率。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而对于"},{"type":"text","marks":[{"type":"strong"}],"text":"T"},{"type":"text","text":"和"},{"type":"text","marks":[{"type":"strong"}],"text":"F"},{"type":"text","text":"之间通过T的主键T.id连接,所以join类型为"},{"type":"codeinline","content":[{"type":"text","text":"eq_ref"}]},{"type":"text","text":",也不用使用Using join buffer。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"5. join语句的优化原则"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"用小结果集驱动大结果集"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":",将筛选结果小的表首先连接,再去连接结果集比较大的表,尽量减少join语句中的Nested Loop的循环总次数;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"优先优化Nested Loop的内层循环"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"(也就是最外层的Join连接),因为内层循环是循环中执行次数最多的,每次循环提升很小的性能都能在整个循环中提升很大的性能;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"对被驱动表的join字段上建立"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"索引"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":";"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"当被驱动表的join字段上无法建立索引的时候,设置"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"足够的Join Buffer Size"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"6.join的实现原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" join的实现是采用Nested Loop Join算法,就是通过驱动表的结果集作为循环基础数据,然后一条一条的通过该结果集中的数据作为过滤条件到下一个表中查询数据,然后合并结果。如果有多个join,则将前面的结果集作为循环数据,再一次作为循环条件到后一个表中查询数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 接下来通过一个三表join查询来说明mysql的Nested Loop Join的实现方式。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select m.subject msg_subject, c.content msg_contentfrom user_group g,group_message m,group_message_content cwhere g.user_id = 1and m.group_id = g.group_idand c.group_msg_id = m.id"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用explain看看执行计划:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"explain select m.subject msg_subject, c.content msg_content from user_group g,group_message m,group_message_content c where g.user_id = 1 and m.group_id = g.group_id and c.group_msg_id = m.id\\G;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"结果如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"*************************** 1. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: g\ntype: ref\npossible_keys: user_group_gid_ind,user_group_uid_ind,user_group_gid_uid_ind\nkey: user_group_uid_ind\nkey_len: 4\nref: const\nrows: 2\nExtra:\n*************************** 2. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: m\ntype: ref\npossible_keys: PRIMARY,idx_group_message_gid_uid\nkey: idx_group_message_gid_uid\nkey_len: 4\nref: g.group_id\nrows: 3\nExtra:\n*************************** 3. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: c\ntype: ref\npossible_keys: idx_group_message_content_msg_id\nkey: idx_group_message_content_msg_id\nkey_len: 4\nref: m.id\nrows: 2\nExtra:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从结果可以看出,explain选择user_group作为驱动表,首先通过索引user_group_uid_ind来进行const条件的索引ref查找,然后用user_group表中过滤出来的结果集group_id字段作为查询条件,对group_message循环查询,然后再用过滤出来的结果集中的group_message的id作为条件与group_message_content的group_msg_id进行循环比较查询,获得最终的结果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这个过程可以通过如下代码来表示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"for each record g_rec in table user_group that g_rec.user_id=1{"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"     for each record m_rec in group_message that m_rec.group_id=g_rec.group_id{"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"          for each record c_rec in group_message_content that c_rec.group_msg_id=m_rec.id"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"                pass the (g_rec.user_id, m_rec.subject, c_rec.content) row"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"          combination to output;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果去掉group_message_content表上面的group_msg_id字段的索引,执行计划会有所不一样。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"drop index idx_group_message_content_msg_id on group_message_content;explain select m.subject msg_subject, c.content msg_content from user_group g,group_message m,group_message_content c where g.user_id = 1 and m.group_id = g.group_id and c.group_msg_id = m.id\\G;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"得到的执行计划如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"*************************** 1. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: g\ntype: ref\npossible_keys: user_group_uid_ind\nkey: user_group_uid_ind\nkey_len: 4\nref: const\nrows: 2\nExtra:\n*************************** 2. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: m\ntype: ref\npossible_keys: PRIMARY,idx_group_message_gid_uid\nkey: idx_group_message_gid_uid\nkey_len: 4\nref: g.group_id\nrows: 3\nExtra:\n*************************** 3. row ***************************\nid: 1\nselect_type: SIMPLE\ntable: c\ntype: ALL\npossible_keys: NULL\nkey: NULL\nkey_len: NULL\nref: NULL\nrows: 96\nExtra:Using where;Using join buffer"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因为删除了索引,所以group_message_content的访问从ref变成了ALL,keys相关的信息也变成了NULL,Extra信息也变成了Using Where和Using join buffer,也就是说需要获取content内容只能通过对全表的数据进行where过滤才能获取。Using join buffer是指使用到了Cache,只有当join类型为ALL,index,rang或者是index_merge的时候才会使用join buffer,它的使用过程可以用下面代码来表示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"for each record g_rec in table user_group{"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      for each record m_rec in group_message that m_rec.group_id=g_rec.group_id{"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"           put (g_rec, m_rec) into the buffer"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"           if (buffer is full)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"                 flush_buffer();"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"flush_buffer(){"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      for each record c_rec in group_message_content that c_rec.group_msg_id = c_rec.id{"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"            for each record in the buffer"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"                 pass (g_rec.user_id, m_rec.subject, c_rec.content) row combination to output;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      }"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"      empty the buffer;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在实现过程中可以看到把user_group和group_message的结果集放到join buffer中,而不用每次user_group和group_message关联后马上和group_message_content关联,这也是没有必要的;需要注意的是join buffer中只保留查询结果中出现的列值,它的大小不依赖于表的大小,我们在伪代码中看到当join buffer被填满后,mysql将会flush buffer。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"join语句的优化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1. 用小结果集驱动大结果集,尽量减少join语句中的Nested Loop的循环总次数;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2. 优先优化Nested Loop的内层循环,因为内层循环是循环中执行次数最多的,每次循环提升很小的性能都能在整个循环中提升很大的性能;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3. 对被驱动表的join字段上建立索引;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4. 当被驱动表的join字段上无法建立索引的时候,设置足够的Join Buffer Size。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"参考文章"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=http%3A%2F%2Fblog.itpub.net%2F7607759%2Fviewspace-692946%2F","title":null},"content":[{"type":"text","text":"MySQL数据库对GROUP BY子句的功能扩展(1)"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=ttps%3A%2F%2Fwww.cnblogs.com%2F8335IT%2Fp%2F5850531.html","title":null},"content":[{"type":"text","text":"SQL中GROUP BY语句与HAVING语句的使用"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=https%3A%2F%2Fwww.cnblogs.com%2Fblueoverflow%2Fp%2F4714470.html","title":null},"content":[{"type":"text","text":"Mysql Join语法以及性能优化"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=http%3A%2F%2Fblog.csdn.net%2Ftonyxf121%2Farticle%2Fdetails%2F7796657","title":null},"content":[{"type":"text","text":"mysql join的实现原理及优化思路"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F44917%2Fexplicit-vs-implicit-sql-joins","title":null},"content":[{"type":"text","text":"Explicit vs implicit SQL joins"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://link.jianshu.com?t=https%3A%2F%2Fblogs.technet.microsoft.com%2Fwardpond%2F2008%2F09%2F13%2Fdeprecation-of-old-style-join-syntax-only-a-partial-thing%2F","title":null},"content":[{"type":"text","text":"Deprecation of \"Old Style\" JOIN Syntax: Only A Partial Thing"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章