How to find rows in one table that have no corresponding row in another table

后端 未结 6 759
甜味超标
甜味超标 2020-11-29 01:50

I have a 1:1 relationship between two tables. I want to find all the rows in table A that don\'t have a corresponding row in table B. I use this query:

SELEC         


        
6条回答
  •  生来不讨喜
    2020-11-29 02:23

    You have to check every ID in tableA against every ID in tableB. A fully featured RDBMS (such as Oracle) would be able to optimize that into an INDEX FULL FAST SCAN and not touch the table at all. I don't know whether H2's optimizer is as smart as that.

    H2 does support the MINUS syntax so you should try this

    select id from tableA
    minus
    select id from tableB
    order by id desc
    

    That may perform faster; it is certainly worth benchmarking.

提交回复
热议问题