how to avoid deadlock in mysql

后端 未结 2 1221
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 01:58

I have the following query (all tables are innoDB)

INSERT INTO busy_machines(machine) 
               SELECT machine FROM all_machines 
               WHERE mach         


        
2条回答
  •  广开言路
    2021-02-06 02:28

    You will probably get better performance if you replace your "NOT IN" with an outer join.

    You can also separate this into two queries to avoid inserting and selecting the same table in a single query.

    Something like this:

               SELECT a.machine 
               into @machine
               FROM all_machines a
               LEFT OUTER JOIN busy_machines b on b.machine = a.machine
               WHERE a.machine_name!='Main' 
               and b.machine IS NULL 
               LIMIT 1;
    
               INSERT INTO busy_machines(machine) 
               VALUES (@machine);
    

提交回复
热议问题