Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Apart from what the others have said regarding normalization, I'd like to answer to the "<em>Or any inbuilt feature of modern-day databses to exploit?</em>" part of your question: </p> <p>PostgreSQL has a pretty nifty extension called <code>hstore</code> which does exactly that and in a highly optimized manner. </p> <p>The <code>hstore</code> data type is essentially a key/value pair, where you can store anything. In your example something like this:</p> <pre><code>INSERT INTO user_attributes (user_id, , attributes) VALUES (1, ('att1 =&gt; x, att2 =&gt; y')); </code></pre> <p>Will insert the keys <code>att1</code> and <code>att2</code> into the column attributes. This can be indexed to make lookups fast. </p> <p>You can query the data using this syntax:</p> <pre><code>SELECT * FROM user_attributes WHERE attributes @&gt; ('att1 =&gt; "Some Value"') </code></pre> <p>This will return all rows that have a key named <code>att1</code> and where that is mapped to the value "Some Value". The above statement will use an existing index on the column, so the lookup is nearly as fast as with a "real" column. The above statement takes ~2ms on my laptop to find a row in a table with 100.000 rows.</p> <p>You can also query for rows that have a specific attribute defined regardless of the value:</p> <pre><code>SELECT user_id, (attributes -&gt; 'att1') FROM user_attributes WHERE attributes ? 'att1' </code></pre> <p>will find all rows where <code>att1</code> is defined and will output the value for those.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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