0

Am trying to get the all rows from Tabl1 which are not available in Table2 with help of NOT IN MSQL query. But am getting timeout exception and query is not getting executed. Below is the mysql query which I am using.

    SELECT * FROM identity WHERE
  unique_id NOT IN (SELECT Message_Queue.index FROM Message_Queue);

Could any please tell the reason or any other way for replacement of NOT IN operation?

4

2 回答 2

0

因为 inMySQL NOT IN性能较差,请尝试使用EXISTS

SELECT  * 
FROM    identity a
WHERE   NOT EXISTS
        (
            SELECT  null
            FROM    Message_Queue b
            WHERE   b.index = a.unique_id 
        );

您还应该在这些列上放置一个索引。

于 2013-09-12T09:48:32.097 回答
0

in()子句中有这么多记录时,您应该使用 ajoin代替

SELECT t1.* 
FROM table1 t1
left join table2 t2 on t2.myId = t1.myId 
where t2.myId is null
于 2013-09-12T09:48:38.917 回答