in reply to Re: How to interpolate sql-output
in thread How to interpolate sql-output

I'm quite aware how dangerous is to let pp make own queries.
This is only for admins of that software.

Why two tables? There are one table which contains only id of that item and its' activity.
Other table contains all fields in key-value-way, so one can add fields as many as one wants without altering db-schema.
That order and limit gets only latest value, cause there is no alter or delete, just inserts, so it builds history of changes.

Replies are listed 'Best First'.
Re^3: How to interpolate sql-output
by chacham (Prior) on Jan 14, 2015 at 22:46 UTC

    The other table is simply not required. It contains no information that the inner table does not have. The only benefit it might have is id is already unique, but at the cost of loading another table and doing a join, it probably hurts more than it helps.

    Queries get more and more complicated over time as they are used for more and more things. This adds complexity which makes understanding the queries take longer and hampers their revision. The simpler the query, the easier on the programmer and the database's optimizer.

      Let's say that I have table ppl, which includes running id, activity boolean and timestamp defaulting to now.
      Then I have another table (=ppl_data) where is also id as serial (only for easier manipulation), ppl_id, key and value and usually timestamp.

      I can put anything to that ppl_data-table without altering db-schema, cause it is in key-value-pair.

      Cause I use only inserts and fetching only newest data with that key, there is nice history of changes.

      Reason to this is, that customer is able to add different fields if needed.
      Ofc there is also datatype for that field, so sanity-check and casting is easier.

      If needed one can vacuum old data from that ppl_data, if it affects to db-server, but usually there is enough power.
      As log as that db-server isn't virtual-server where disk-latency might cause slowing downs eventually.

      And when there is also way to do different queries, one can alter or even add features without code-changes.

        Oh, i think we have a misunderstanding. I wasn't saying the two table structure is bad. I meant that the query requires only one table.

        The structure you are using is known as Entity Attribute Value. It is excellent when working with unknown data, though some efforts may be required to keep it working efficiently.