Mysql 不同的SQL查询效率
项目中有与几个表相关的业务需求。在这些情况下,以下两个查询选项有助于性能优化。我该如何选择? 第一:过滤笛卡尔积:Mysql 不同的SQL查询效率,mysql,sql-optimization,Mysql,Sql Optimization,项目中有与几个表相关的业务需求。在这些情况下,以下两个查询选项有助于性能优化。我该如何选择? 第一:过滤笛卡尔积: select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id 第二:左侧外部连接模式或右侧外部连接模式 select table1.a ,table2.b from table1 left join table2 on table1.id=table2.id 请告诉我。非常感谢。第二次查询速
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
第二:左侧外部连接模式或右侧外部连接模式
select table1.a ,table2.b from table1 left join table2 on table1.id=table2.id
请告诉我。非常感谢。第二次查询速度更快。它没有嵌套条件。SQL引擎将联接视为单个表或视图
在我的openion中,第一个查询作为For循环工作,所以运行时复杂度为On,而seconf查询作为worsecase中的开关情况工作,运行时复杂度为Olog n 如果您以一种形式编写它们,其中它们给出相同的结果,那么期望查询分析器给出相同的*查询计划。也就是说,Use-JOIN-not,,因为,不太清楚,几十年来一直被弃用 如果要比较两个不同查询的性能,最简单的方法是同时运行这两个查询并比较它们所用的时间,尽管通过比较mysql中的查询计划EXPLAIN query text来检查它们是否在做不同的事情会为您提供查询计划 请注意,说我知道它们会给出不同的结果,但我想要更快的结果,这在编程中从来都不是一件明智的事情。你应该知道你想要什么样的结果 即 具有与相同的含义
select table1.a ,table2.b from table1 join table2 on table1.id=table2.id
及
具有与相同的含义
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
以及您未使用的连接表单:
select table1.a ,table2.b from table1 right join table2 on table1.id=table2.id
具有与相同的含义
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
及
具有与相同的含义
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
select table1.a ,table2.b from table1 ,table2 where table1.id=table2.id
union
select table1.a , null from table1 where table1.id not in (select table2.id from table2)
union
select null, table2.b from table2 where table2.id not in (select table1.id from table1)
我不明白这个问题。你能详细说明一下吗?这两个不相等。LEFT JOIN将返回没有匹配table2行的table1行,笛卡尔乘积将不会返回这些行。@0xCAFEBABE使用上述两种方法查询相同的数据,但我想知道在何种情况下使用这种方法性能会更好这两种查询选择不同的数据会有不同的结果。如果你想要完全相同的查询,你应该进行内部连接而不是左连接。那么表演也会完全一样。现在比较两个不一定检索相同数据的查询,因此它们是无关的。在任何情况下,第二个查询都比第一个好?SQL执行一个查询计划步骤。如果OP从left join更改为join,那么他的两个查询将具有相同的相似计划