I have 2 tables on my database. They both have more than 16m records, they have the same uuid for relation (I have indexes for both uuid fields). One of them is like 166GB and the other one is around 50GB. I'll change table names on my question but I hope you will get the question.
Let's say my first table is called users and the second one is profile. Now I have a field on my users table and I want to copy it to my profile table.
I've done something last night but it's still processing and been more than 10 hours already.
I have 3 questions now. First question; are my queries ok?
ALTER TABLE profiles ADD COLUMN start_stamp TIMESTAMP DEFAULT NOW();
SET start_stamp = (SELECT start_stamp::DATE FROM users WHERE uuid = profiles.uuid);
CREATE INDEX start_stamp ON profiles;
And the second question; is there any difference between these two queries? If yes, whats the difference and which one is better?
UPDATE profiles
SET start_stamp = (SELECT start_stamp::DATE FROM users WHERE uuid = profiles.uuid);
QUERY PLAN
--------------------------------------------------------------------------
Update on profiles (cost=0.00..159956638.61 rows=18491638 width=116)
-> Seq Scan on profiles (cost=0.00..159956638.61 rows=18491638 width=116)
SubPlan 1
-> Index Scan using unique_user_uuid on users (cost=0.56..8.58 rows=1 width=20)
Index Cond: ((uuid)::text = (profiles.uuid)::text)
UPDATE profile
SET start_stamp = users.start_stamp
FROM users
WHERE profiles.start_stamp = users.start_stamp;
QUERY PLAN
--------------------------------------------------------------------------
Update on profiles (cost=2766854.25..5282948.42 rows=11913522 width=142)
-> Hash Join (cost=2766854.25..5282948.42 rows=11913522 width=142)
Hash Cond: ((profiles.uuid)::text = (users.uuid)::text)
-> Seq Scan on profiles (cost=0.00..1205927.56 rows=18491656 width=116)
-> Hash (cost=2489957.22..2489957.22 rows=11913522 width=63)
-> Seq Scan on users (cost=0.00..2489957.22 rows=11913522 width=63)
And my final question is; is there a better way to copy a value from a table to another with more than 16m and 200gb records?
Thanks.
users.uuidis unique