djangodatabasepostgresqlbackend

Review table for two different profile types in Django and PostgreSQL DB


I have a question how I should design this relational database. I have a main profile, which has 2 sub-profiles, which are weak entities. These sub-profiles represent counterparts of a business process, an Employee and an Employer.

Given I want to have tables that are related to these sub-profiles, such as "Review" or "Job Posting" (Similar to LinkedIn), which approach would be the most suitable:

  1. Creating a separate table for each profile type, e.g.,
    EmployerReview and EmployeeReview
  2. Creating a single unified table, which has fields to determine which sub-profile type is sending and receiving the data. e.g.,

Review

My main concern is the performance issues, e.g., when wanting to query all EmployerProfiles and the related Reviews of that profile.
Now, If I have understood correctly, in method 2. you would have to do filter query, which is obviously slower than select_related (Similar to SQL Join?).
The gain would be more flexibility and simple complexity, but there would be performance loss.
Which of these methods would be more standardized or optimized way for such a problem?

I tried creating both of the solutions, and they both work, but I am not sure if performance will become an issue with method 2. if the backend userbase scales a lot.


Solution

  • "method 2. ... which is obviously slower". No it is not, where is your demonstration of that? Consider, for method 1 you need code to determine which table to access. Is that code faster than filtering? Also can a condition arise where both are needed? Is running two selects (oj joining the tables) faster than filtering? You indicate you have tried both so extend this the tables to have a 100K rows, then run each several times in different sequence. Capture EXPLAIN ANALYZE on each run. Also ensure to capture app code execution time in addition to SQL execution time.