简体   繁体   中英

Why this MySQL query is faster when execute singly?

Why this query:

select sum(column_2) from table1 where column_1 in 
(select column_1 from table2 where column_3 = 'foo');

takes several minutes to execute so if I execute the two queries singly is so more faster?

For instance:

select column_1 from table2 where column_3 = 'foo' results xxx

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

You should to avoid nested queries for better performance, you can rewrite it as:

select sum(column_2) 
from table1  t1
inner join table2  t2
on t1.column_1 =  t2.column_1
where column_3 = 'foo';

Quoting MySQL Docs:

It can be more efficient to make use of some of these techniques rather than to use subqueries

For example, this query:

SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);

Can be rewritten as:

SELECT DISTINCT t1.* FROM t1, t2 WHERE t1.id=t2.id;

You asked why, rather than for options on how to make it faster. The short answer is that this is not an area that MySQL's query parser is well optimized. More simply, subquery performance in MySQL is pathetic.

That is not strictly true, but through bitter learned experience, it's true about 90% of the time.[1][2] In almost any other database, the relational calculus will reduce subqueries where possible, including Oracle, PosgreSQL, SQL Server, and SQLite (not an exhaustive list, but the DB's with which I have the most experience). And the reason is sheer development time on the relational theory.

For MySQL, this is a "gotcha" area of which you just need to be aware when formulating your queries. Generally (not always), try to avoid subqueries. Use JOINs, multiple queries, and any reference that helps .

And for specific help with your queries and data sets, use the EXPLAIN operator :

EXPLAIN SELECT SUM(column_2) FROM table1 WHERE column_1 IN
     (SELECT column_1 FROM table2 WHERE column_3 = 'foo');

[1] MySQL Limitations Part 3: Subqueries

[2] When the subselect runs faster , from 2010 (but yet a good analysis)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM