Quickest way to combine two identical structured tables without duplicate entries

蓝咒 提交于 2020-01-17 05:20:09

问题


I have two tables with the exact same structure/columns. I need to combine them without duplicates based on the second column (title). First column is ID and these may have duplicates but should be ignored.

Example of database structure

id (primary key, auto increment), title (unique), description, link

Ex. Table 1
 1 Bob thisisbob bob.com
 2 Tom thisistom tom.com
 3 Chad thisischad chad.com

Ex. Table 2
 1 Chris thisischris chris.com
 2 Chad thisischad chad.com
 3 Dough thisisdough doug.com

What I need in Table 3
 1 Bob thisisbob bob.com
 2 Tom thisistom tom.com
 3 Chad thisischad chad.com
 4 Chris thisischris chris.com
 5 Dough thisisdough doug.com

Both tables have about 5 million entries/rows each. I need the most efficient way possible to combine them.


回答1:


It is a little hard to understand exactly what you want. Perhaps, though, this might be:

select *
from table1
union all
select *
from table2
where not exists (select 1 from table1 where table1.title = table2.title);

This will run faster with an index on table1(title).

EDIT:

If you want to insert them into a third table, you can do:

create table table3 as
    select *
    from table1
    union all
    select *
    from table2
    where not exists (select 1 from table1 where table1.title = table2.title);


来源:https://stackoverflow.com/questions/23965352/quickest-way-to-combine-two-identical-structured-tables-without-duplicate-entrie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!