Note that there are some explanatory texts on larger screens.

plurals
  1. PORepresenting "linked" contacts in mysql
    primarykey
    data
    text
    <p>I'm trying to determine the best way to represent the concept of "linked" contacts (people) in an existing mysql database. Let me explain what that means:</p> <p>I have a table with different contacts, each with an ID. I have another table with different time periods that a contact can belong to. Each period also have an ID. There's a N:1 relationship between contacts and periods. I'd like to be able to represent that two (or more) contacts in different periods are actually the same person (for analytics sake). So if two contacts are linked, they should be treated as one for analytics purposes. For example, I might want to represent that contacts 1 and 3 are the same person.</p> <p><img src="https://i.stack.imgur.com/zXi3x.png" alt="Existing Schema"></p> <p>The difficulty is in representing this concept of "linked" for more than two contacts (e.g. contacts 1, 3, and 4). If there were just two, I could simply have a table with two columns representing the connection. However, since there could be three contacts who are linked (or any number, really), this doesn't work out of the box.</p> <p>I've come up with two possible solutions for now:</p> <p><strong>1)</strong> Have one table (let's call it linked_contacts) that has two columns, one for each contact ID. This will represent a connection. If there are more than 2 linked contacts, then add a connection to one of the linked contacts. The upside of this is that it's cheap to add another contact into the link. The downside is that in order to get all contacts in the link, I essentially have to construct the graph by making a query for each connection.</p> <p><img src="https://i.stack.imgur.com/gwGZs.png" alt="Linked Contacts Option 1"></p> <p><strong>2)</strong> Still have the linked_contacts table with two columns. When adding a new contact to the link, generate a new row for each connection between the new contact and the pre-existing ones. The upside is that I can get all the linked contacts for a particular contact in a single query. The downside is a larger table.</p> <p><img src="https://i.stack.imgur.com/if11H.png" alt="Linked Contacts Option 2"></p> <p><strong>Additional notes</strong></p> <ul> <li>It's not expected that there will be many links with more than 2 linked contacts, but I would like to support more than 2.</li> <li>The main use case for retrieving this data is getting all contacts that are linked to a specific contact.</li> <li>It is very unlikely that a contact will be removed from a link. It will be more common for a contact to be added to a link.</li> <li>Performance of retrieval is the main factor, since I'll be displaying which contacts are linked to a particular contact on each contact's page.</li> </ul> <p>I'm leaning towards option 2 for ease/speed of retrieval of all contacts in a link given a particular contact. Is the right way to go about it? Are there any other factors that I should take into account? I'm open to other design strategies as well!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload