Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么单独执行MySQL查询会更快?_Mysql_Performance_In Clause - Fatal编程技术网

为什么单独执行MySQL查询会更快?

为什么单独执行MySQL查询会更快?,mysql,performance,in-clause,Mysql,Performance,In Clause,为什么要这样问: select sum(column_2) from table1 where column_1 in (select column_1 from table2 where column_3 = 'foo'); 执行需要几分钟,所以如果我单独执行这两个查询,速度会更快吗 例如: 从表2中选择列_1,其中列_3='foo'结果xxx select sum(column_2) from table1 where column_1 in (xxx); 为了避免嵌套查询以获得更好的

为什么要这样问:

select sum(column_2) from table1 where column_1 in 
(select column_1 from table2 where column_3 = 'foo');
执行需要几分钟,所以如果我单独执行这两个查询,速度会更快吗

例如:

从表2中选择列_1,其中列_3='foo'结果xxx

select sum(column_2) from table1 where column_1 in (xxx);

为了避免嵌套查询以获得更好的性能,您可以将其重写为:

select sum(column_2) 
from table1  t1
inner join table2  t2
on t1.column_1 =  t2.column_1
where column_3 = 'foo';
引用MySQL文档:

例如,此查询:

select sum(column_2) from table1 where column_1 in 
(select column_1 from table2 where column_3 = 'foo');
从t1中选择*其中id在从t2中选择id

可以重写为:

从t1,t2中选择不同的t1.*其中t1.id=t2.id


为了避免嵌套查询以获得更好的性能,您可以将其重写为:

select sum(column_2) 
from table1  t1
inner join table2  t2
on t1.column_1 =  t2.column_1
where column_3 = 'foo';
引用MySQL文档:

例如,此查询:

select sum(column_2) from table1 where column_1 in 
(select column_1 from table2 where column_3 = 'foo');
从t1中选择*其中id在从t2中选择id

可以重写为:

从t1,t2中选择不同的t1.*其中t1.id=t2.id


你问的是为什么,而不是关于如何加快速度的选项。简单的回答是,这不是MySQL的查询解析器能够很好地优化的领域。更简单地说,MySQL中的子查询性能是可怜的

严格来说,这并不是真的,但通过苦涩的经验,这在90%的情况下都是真的。[1][2]在几乎任何其他数据库中,关系演算都会尽可能减少子查询,包括Oracle、PosgreSQL、SQL Server和SQLite不是一个详尽的列表,而是我最有经验的数据库。原因完全是关系理论的发展时间

对于MySQL来说,这是一个你在制定查询时需要注意的问题。通常情况下,不一定要避免子查询。使用联接、多个查询和查询

有关查询和数据集的具体帮助,请使用:

[1]


[2] ,但这是一个很好的分析

你问的是为什么,而不是如何加快速度的选项。简单的回答是,这不是MySQL的查询解析器能够很好地优化的领域。更简单地说,MySQL中的子查询性能是可怜的

严格来说,这并不是真的,但通过苦涩的经验,这在90%的情况下都是真的。[1][2]在几乎任何其他数据库中,关系演算都会尽可能减少子查询,包括Oracle、PosgreSQL、SQL Server和SQLite不是一个详尽的列表,而是我最有经验的数据库。原因完全是关系理论的发展时间

对于MySQL来说,这是一个你在制定查询时需要注意的问题。通常情况下,不一定要避免子查询。使用联接、多个查询和查询

有关查询和数据集的具体帮助,请使用:

[1]


[2] ,从2010年开始,但仍然是一个很好的分析

共享执行计划如何?EXPLAIN extended单独执行它们只执行2个查询。将一个作为嵌套子查询执行会对父查询的每一行执行它。共享执行计划如何?EXPLAIN extended单独执行它们只执行2个查询。将一个作为嵌套子查询执行会对父查询的每一行执行它。在选择中。。。几乎总是表现不佳。通常解决方法是使用联接。在选择中。。。几乎总是表现不佳。通常解决方法是使用联接。