postgresql - Turning polymorphic table association into a type column -


say have entities table (200mil rows +) in each row has contentid column references piece of content exists in either posts, comments , replies table (without fk's).

now, each row in entities i'd check table associated piece of content exists in, , return type.

this part of data migration process. ideas on how perform in relatively performant way?

i create mapping table , use in migration, based on following query. might costly, should have run once. if entity data set live, can add triggers automatically maintain entities_type association table you.

depending on next steps, might way smarter maintain 3 different tables rather tag table this. hope gets started, tho.

create table entities_type  select 'post', contentid   entities   join posts using(contentid)  union  select 'comments', contentid   entities   join comments using(contentid)  union  select 'replies', contentid   entities   join replies using(contentid) ; 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -