I’m using a postgresql install to house my data.
My Post model has an attribute called “selection” which currently stores data within a TEXT column in the form of: “x1,x2,x3,x4,x5…”
When I need to access this data i split it on the the comma and do my thing with it.
I’m prototyping an app so i quickly just did the easiest thing when i was writing it but now i can see an alternative option would be to create a table for “selections” and associate it back to the post, then have individual rows for each bit.
My question is, how or when do i make the choice to store or not data like this?
Thank you
If those represent other data elements in other tables in your database, then I would never store it as a comma separated string.
SQL in general is optimized for set-based arithmetic and functions, not for string parsing.
The only scenario that I can think of where the string version may be easier/faster is if you want to find a specific set of values and ONLY those values, i.e.
Col = 'A1, B2, C3, d4'.Otherwise, if you want to check individual fields or do other comparisons, storing that data in a normalized table is the best course of action. It’s more extensible, easier and more efficient to check for specific values, and will make other operations on that table quicker (since you store less data in-row for that main table).