4 ms·
In this example longitude/latitude tuple is a single logical unit, and a perfect example for using arrays, though of course PostgreSQL already has this covered.
by deno 13y ago
In this example longitude/latitude tuple is a single logical unit, and a perfect example for using arrays, though of course PostgreSQL already has this covered. However this isn’t comparable; it’s a single location, single column, not a list of locations. A list of locations gives you choice between using multi‐dimensional array ([[x,y], [x1, y1]…]) or a relational design, and we’re back at square one.
I think the crucial element here is whether the set is bounded. A set of coordinates is bounded, a set of tags usually is not.
I know I made this point about semantics in my last post, but I’m actually more concerned about the practical aspects of working with arrays used in this, in my opinion, relational (unbounded) case. It may be so that arrays in PostgreSQL will become so powerful as to provide complete alternative for forming relational models… however that wasn’t my experience so far. More complex joins require you to go through UNNEST() and essentially normalize the arrays into temporary tables, which makes for terrible performance.
The relational design has also other advantages, a composite primary key has unique guarantees, something that’s not yet possible for arrays, and separate table always gives you the flexibility of including additional metadata as your needs evolve.
For me the question really is ‘what are the practical advantages for using arrays over a relational design,’ and not the other way around. After all, PostgreSQL is still mostly a relational database. And of course, as others have pointed out, there are some advantages, but most of them would be considered (micro)optimizations, and I’d rather leave those to database developers.
- saurik 13y agoOperations you tend to perform on tags: "find me posts that have all of these tags", "find me posts that have similar tags to this one", "order the results of any of these queries by properties of the post". Something you usually do not do with tags: "give me a list of every tag that the system has ever seen used on a post". Tags normally don't themselves have properties. The event of a tagging usually does not (but sometimes does) have properties either. I am not at all certain why you would find yourself needing to UNNEST: there's nothing you'd do with a tag where that would be interesting or valuable. In my previous comments, I offered multiple "practical advantages" of using arrays for this case over relations; you are choosing to view them as "microoptimizations", but I'd say the exact opposite: every operation one normally does with tags, when used with arrays, is a single operator in a where clause. Even the simplest case, "show me posts with these tags" is at least slightly harder with the relation. In general, the clearest code with the right semantics tends to also be the one that gives the database the most room to optimize: its the same here; you are just muddling up the N-dimensionality of tags by moving them into a relation. Your attempt to compare a list of locations doesn't make much sense to me, as a list of locations doesn't seem to fit well with the question "give me a list of posts that have similar sets of locations to this other sets of locations"; if you can explain the semantics of that use case more, I might be able to appreciate it. However, right now, I think you are still just caught up looking at tags at the wrong scale: a set of locations is more similar to a set of tag clouds, not a single tag cloud; a tag cloud is a single location in an N-dimensional space, and all of the operations developers tend to perform on that data type are easier to think about, easier to optimize, and even easier to type the SQL for with PostgreSQL, than thinking about them as discrete data items. What separates PostgreSQL from other database systems in this regard is that they actually have index implementations that can make this obvious translation of the semantics of tags fast: normally, you are forced to do the relation, and you end up in the hell of working with relations for things where they make no sense at all. As an example: "give me all the posts from last friday, and all of the tags on each"; this is the exact opposite of your UNNEST scenario: you end up having to rebuild the records of the posts here by doing a join and an array_agg. It would be ok if you bought something, but you didn't, and the only reason it even seems to be "more relational" is due to a backwards understanding of what a tag cloud is (like, to be clear: I really don't like the denormalization-is-good crowd, and I think NoSQL is generally the wrong solution to any problem, but I maintain that splitting a tag cloud into a relation is like splitting a basic data type like a long/lat, or attempting to do an ORM columns->rows mapping, both of which misapply relations; its like you are reading what I said as if I'm talking about long/lat being changed into an array, and totally ignoring the semantics of a long/lat as a location in a two-dimensional space... the fact that "PostgreSQL already has yo covered" for something like long/lat precisely demonstrates that people wouldn't even consider breaking those apart). Can you provide me an example of your problem using arrays here? You seem to just be asserting the same point that they are somehow harder over and over again. The only such example I'm seeing in this post is "uniqueness guarantee", which is a property of a single array and thereby trivially described as a check constraint on the column. This kind of set-oriented use case, FWIW, is actually precisely why GiST and GIN were created, and why they support the intarray contrib module; and this kind of facet-oriented search is one of the reasons why PostgreSQL got so many great contributors via PostGIS.
- losvedir 13y ago> Something you usually do not do with tags: "give me a list of every tag that the system has ever seen used on a post" I'm thinking of the times I've used tags in my webapp and in all cases I've had to do: "for this subset of records what are all the tags?" And in one case, "what are all the tags we're using?" Does that mean using postgres native arrays as tags would be a bad idea in those cases? I read this article with interest because we've been using acts-as-taggable a fair amount.
- saurik 13y agoIf you consider this kind of query common or important, you run into a problem where that information is not exposed via the semantics of storing a tag cloud per item. It just so happens that at an implementation level, PostgreSQL could very easily answer the "all tags" query if using a GiN index, but AFAIK does not expose that to the developer. For a GiST index, that information simply does not exist. You could now simulate the information for the all tags case by using a table of tags that you garbage collect by using efficient "are there still any posts with this tag on them" queries whenever you change a tag cloud, but that is now a separate reverse-denormalized data structure you are maintaining in the database. For "show me all tags used by a subset of posts", though: that use case sucks either way, and tha array is probably better. For the relation, you are doing a select distinct off a join, and in the tag cloud solution you are doing a set merge aggregation. The tag loud is going to be more efficient, but neither is indexable, do it doesn't really matter in a way (you should just avoid the user needing to make that query ;P).
- gbog 13y agoI think you pointed out the main issue with arrays. If I follow your reasoning and suppose we will not want to know who is tagging what and when because we believe we just want to know the bare list of tags attached to an item. But then the app complexifies, we need more data, and we would like to have ansers to questions such as "is tagging different in the morning or in the evening?", "are there new trends in tags or new tags since last year?", or we even simply want to remove the tags created by some users because we know they try to cheat the system or spam it. Then with your "simple" solution I think we are stuck and will need to migrate, which is a pain. Moreover, all past data is unsuable. If, on the opposite, if we just do a "who tags what when" table, we are on much safer ground. We get a scalable event table that can be indexed (and sharded) by timestamp. We can undo any operation (which should be required for any action but email sending). The only drawbacks are 1) we need to aggregate and cache this data to have a list of tags for each item, and 2) we might have stored some data we might not need in the future. I always feel the saved data granularity should be one level finer than the data granularity used in the business model.