mysql - SQL Insert into and Select multiple columns? -
so have tables this:
communication: (calls made)
timestamp fromidnumber toidnumber generallocation 2012-03-02 09:02:30 878 674 grasslands 2012-03-02 11:30:01 456 213 tundra 2012-03-02 07:02:12 789 654 mountains 2012-03-02 08:06:08 458 789 tundra and want create new table has distinct fromidnumber , toidnumber's.
this sql fiddle it.
this works:
insert commidtemp (`id`) select distinct communication.fromidnumber communication union distinct select distinct communication.toidnumber communication; and got:
id 878 456 789 674 213 654 365 but wonder if there more efficient way, because dataset have has millions , millions of lines , didn't know performance of union distinct.
i tried
insert commidtemp (`id`) select distinct communication.fromidnumber , communication.toidnumber communication; but didn't work... there other way more efficiently? i'm pretty new sql, appreciated, thank you!!
first thing: not have experience big tables. have test out following tipps find out if working in situation:
1. create index in source table
make sure both columns fromidnumber , toidnumber have index, i.e.
alter table communication add index (fromidnumber); alter table communication add index (toidnumber); 2. try remove distinct
i not find faster query example, though might try query without distinct keyword - using union returns distinct values definition. sql gives same result current query:
insert commidtemp (`id`) select fromidnumber communication union select toidnumberfrom communication; 3. use primary key in temp table
also try approach setting commidtemp.id column primary key , use insert ignore - useful if want update table without deleting contents:
create table commidtemp (id int primary key); insert ignore commidtemp (`id`) select fromidnumber communication union select toidnumber communication;
Comments
Post a Comment