Note that there are some explanatory texts on larger screens.

plurals
  1. POTo normalize or not to normalize user_ids
    primarykey
    data
    text
    <p>In my Rails application, I have a variety of database tables that contain user data. Some of these tables have a lot of rows (as many as 500,000 rows per user in some cases) and are queried frequently. Whenever I query any table for anything, the user_id of the current user is somewhere in the query - either directly, if the table has a direct relation with the user, or through a join, if they are related through some other tables.</p> <p>Should I denormalize the user_id and include it in every table, for faster performance?</p> <hr> <p>Here's one example:</p> <ul> <li>Address belongs to user, and has a user_id</li> <li>Envelope belongs to user, and has a user_id</li> <li>AddressesEnvelopes joins an Address and an Envelope, so it has envelope_id and address_id -- it doesn't have user_id, but could get to it through either the envelope or the address (which must belong to the same user).</li> </ul> <p>One common expensive query is to select all the AddressesEnvelopes for a particular user, which I could accomplish by joining with either Address or Envelope, even though I don't need anything from those tables. Or I could just duplicate the user id in this table.</p> <hr> <p>Here's a different scenario:</p> <ul> <li>Letter belongs to user, and has a user_id</li> <li>Recepient belongs to Letter, and has a letter_id</li> <li>RecepientOption belongs to Recepient, and has a recepient_id</li> </ul> <p>Would it make sense to duplicate the user_id in both Recepient and RecepientOption, even though I could always get to it by going up through the associations, through Letter?</p> <hr> <p>Some notes:</p> <ul> <li>There are never any objects that are shared between users. An entire hierarchy of related objects always belongs to the same user.</li> <li>The user owner of objects never changes.</li> <li>Database performance is important because it's a data intensive application. There are many queries and many tables.</li> </ul> <hr> <p>So should I include user_id in every table so I can use it when creating indexes? Or would that be bad design?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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