I have the following information in a python sqlite3 database which has around 4 million records.
Term No of articles Article Ids
Obama 300 [411,523,534, …. 846]
Gandhi 3900 [23,32,33…..4578]
Mandela 3900 [21,14,56,145 …4536]
George Bush 450 [230,310 … 700]
Tony Blair 350 [225,320 … 800]
Justin Bieber 25 [401 , 420, 690 …. 904]
Lionel Messi 150 [23, 78, …… 570]
'Article Ids' is a blob holding the list of ids (as returned by an API)
I have the task of finding the common-ids from the Id List for each term and save them in 'relationships.db'
How do I establish relationships where I find which articles talk both about Gandhi & Mandela together (intersecting article ids) ?
The relationships.db should look like this;
Term 1 Term 2 No of Common Article Ids Common Article IDS
Obama Gandhi 17 [34,123,25 ...]
Obama Mandela 43 [145,111,234,456 ....]
Obama George Bush 46
Obama Tony Blair 2
Obama Justin Bieber 36
Obama Lionel Messi 3
Gandhi Mandela 40
Gandhi George Bush 41
Gandhi Tony Blair 32
Gandhi Justin Bieber 31
Gandhi Lionel Messi 20
Mandela George Bush 20
Mandela Tony Blair 11
Mandela Justin Bieber 19
Mandela Lionel Messi 39
George Bush Tony Blair 46
George Bush Justin Bieber 49
George Bush Lionel Messi 2
Tony Blair Justin Bieber 50
Tony Blair Lionel Messi 3
Justin Bieber Lionel Messi 6
Using a 'for loop' to loop through every term to get the intersection is painful. Is there an efficient way to do this? Will there be trade-offs between 'memory' and 'speed'?