简体   繁体   中英

MySQL subquery very slow in huge table

I have a sql query like this:

SELECT o_id, someColumn... FROM link_table WHERE s_id = 1 and o_id IN
(SELECT s_id FROM link_table WHERE o_id=2)

This sql query is to find out intermediate_id connected between specific s_id and o_id, but seems extremely slow. Take about 10s.

link_table is huge (with 40M rows)

Can anyone help me? Thanks.

Try this : As per my experience EXISTS gives better performance over IN Cluase

SELECT o_id, someColumn... 
FROM link_table t1 
WHERE s_id = 1 and EXISTS (SELECT 1 
                           FROM link_table t2 
                           WHERE t2.s_id=t1.o_id AND t2.o_id=2)

Try using self join:::

SELECT o_id, someColumn... FROM 
link_table A, link_table b
WHERE a.s_id = 1 AND
a.o_id=b.s_id AND
b.o_id=2

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