现有两个数据库中的两个表(Db1.table和Db2.table),Db1.table中的数据经常要导到Db2.table中去,记录数很大.怎样写高效的SQL语句,查出Db1.table中的新记录,即在Db2.table中不存在的记录.
现假设两个表的结构都是(id,name),记录如下:
db1.table: | db2.table
id name | id name
-----------------------------------------------------
1 张三 | 1 张三
2 李四 | 2 李四
3 王五(新) |也就是说要选出db1.table中的ID为3的记录.
现假设两个表的结构都是(id,name),记录如下:
db1.table: | db2.table
id name | id name
-----------------------------------------------------
1 张三 | 1 张三
2 李四 | 2 李四
3 王五(新) |也就是说要选出db1.table中的ID为3的记录.
from db1.table
minus
select db2.table.id
from db2.table
from table
minus
select tab.id
from tab
请问,会不会用这个sql在效率上会更好?
select table.id from table
where not existing (select 1 from tab where tab.id = table.id)
from db1.table
minus
select db2.table.id
from db2.table
其次,固定条件的查询,如:no='12452'这样的查询条件,放在where子句的最后;