简体   繁体   中英

Improve efficiency of MySQL self-join statement

I have the following MySQL table of tuples and need to know if the row contains a root or leaf using 2 SQL statements.

The SQL statements work but are extremely slow (each statement takes ~6 hours for a 100k row table on a dedicated RHEL web server) so I'd like to get some ideas on how to improve performance.

For the decision-making -> Root meaning where 'parent' value is not found in any table row in 'child' and Leaf meaning where 'child' value is not found in any table row in 'parent'.

Table 'tuples'...

parent   varchar(20),
child    varchar(20),
root     boolean,
leaf     boolean

Table is indexed on unique parent+child.

The table is set up to have default values for root & leaf of true.

The 1st SQL statement finds rows which are not a root and the 2nd finds rows which are not a leaf.

update tuples t1 inner join tuples t2 on (t1.parent in (select t2.child)) set t1.root=false;

update tuples t1 inner join tuples t2 on (t1.child in (select t2.parent)) set t1.leaf=false;

=> Does anyone have a better (faster) statement than I have been able to find up to now? Thanks.

Try this to replace the first update query:

UPDATE tuples t1
    LEFT JOIN tuples t2 on t2.child=t1.parent
SET t1.root=0
WHERE t2.parent is NULL

Correlated subqueries can be quite slow. I like to replace them with JOINs where possible. If that works for you, a similar approach can recast the second update query as well.

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