mysql加入并选择速度差

问题描述

| 我要SQL查询
SELECT child.name,(COUNT(parent.name) - (parentDepth.depth + 1)) AS depth 
from category as child,category as parent,category as sub_parent,( select child.name,(count(parent.name)-1) as depth from category as child,category as parent where child.lft between parent.lft and parent.rgt and child.name=\'ELECTRONICS\' ) as parentDepth 
where child.lft between parent.lft and parent.rgt and child.lft between sub_parent.lft and sub_parent.rgt and sub_parent.name=parentDepth.name 
group by child.name having depth >0 order by child.lft
使用联接
SELECT child.name,(COUNT(parent.name) - (parentDepth.depth + 1)) AS depth from 
category as child join category as parent on child.lft between parent.lft and parent.rgt join category as sub_parent on child.lft between sub_parent.lft and sub_parent.rgt,category as parent where child.lft between parent.lft and parent.rgt and child.name=\'ELECTRONICS\' ) as parentDepth 
where sub_parent.name=parentDepth.name 
group by child.name having depth >0 order by child.lft
我想知道一个更好!我的意思是性能和速度     

解决方法

如果存在差异,则性能差异绝对是最小的。基本上,“ 2”语法只不过是编写联接的一种较短形式,因此唯一的区别是用于“解析”语句的时间。要在数据库上获得更好的性能,还有许多重要的事情要做,例如 使用索引 不要选择未使用的字段 选择最适合您需求的存储引擎 就我个人而言,我将使用第二种语法,因为它对我而言更具可读性(并且可读代码很重要-比如此小的性能差异更为重要),但这仅是我的主观意见,其他人可能喜欢第一种语法,因为\较短...     ,在查询之前使用EXPLAIN以获得有关MySQL将如何执行查询,它将使用哪些表,将读取多少行,利用哪些索引的信息,等等。 其他解决方案是多次运行此查询(例如100)并测量平均响应时间。 查看您的查询后,我认为您可能会编写出更好的查询(不过,不确定-乍一看太复杂了)。     ,我更喜欢实际测试和获取数据,而不是推测(即使一个人的推理听起来很合理)。 在单个查询级别,可以使用SHOW PROFILE语句显示当前会话过程中执行的语句的性能分析信息: http://dev.mysql.com/doc/refman/5.1/zh-CN/show-profiles.html 这将使您了解在MySQL数据库上运行查询要花费多长时间(即查询的持续时间或速度)。当然,在应用程序级别,可能还有其他风险。 有关如何优化MySQL的一般概述,一本有趣的书是Baron Schwartz等人的“高性能MySQL”,由O'Reilly Media,Inc.发行(ISBN:9780596101718)。 ://www.highperfmysql.com/ 本书详细介绍了如何解释EXPLAIN语句的输出以及许多其他有用的主题。