70 points · by paratrooper · 14y ago · 39 comments
See my post here: http://thoughts.davisjeff.com/2009/09/30/choosing-data-types...
And no, using arrays is not an automatic violation of first normal form.
One of the conditions of the 1NF is that each row and column contain one and only one value. While a NULL might be disputable, using multiple values as shown in the two examples (tags, items/price/quantity) are a clear violation of the 1NF. Only if the array data were truly "one value" (e.g., a vector) it would not be a violation. (I.e., I am not trying to contradict your statement, but I think it helps to add a bit of clarification.)
There is no useful differentiation between atomic and non-atomic values. A timestamp is made up of a date and a time, a string is an array of characters, a real number has a sign and magnitude, etc.
I suppose you could just use boolean only, which could be regarded as "atomic".
That's not to say they aren't useful though; read performance would be much better with all the data living in a single row somewhere on disk. SQL queries would also be easier to write.
Why do you say that? To a certain extent I agree in broad terms; but I don't believe that it is somehow "unclean" or un-relational just because you are using an array (or any other complex data type).
"This means that using them will change the ACID characteristics compared to a relational design."
No, arrays are protected by ACID as is any other data in postgres.
"it would seem like adding another item to a purchase would cause the entire purchase row to lock"
It will still allow concurrent reads of the row in postgres (MVCC). In order for the write lock to be a practical problem, there would have to be a lot of concurrent updates to the very same purchase.
I think we can all agree that the first example is "hacky" (which the author says in the article), so let's ignore that one. How about the second example? I think it's pretty reasonable to store tags that way, if for no other reason than it could save a lot of space.
The interesting thing about arrays is depending on the work you're doing with them, they could either be a violation of 1NF or not. For example, if you're storing vectors in the physics sense, you may not be querying or updating their contents, in which case it's not a violation of 1NF because it's an atomic value. The danger of encouraging people to use Postgres's arrays is a fair number of the people reading are developers and will take this as carte blanch to store repeated values or sets as arrays, and miss out on a bunch of the power and elegance of relational databases (and probably cause themselves a lot of pain dealing with a type that has no direct and helpful JDBC mapping, for example).
> if for no other reason than it could save a lot of space
I discourage people from worrying too much up-front about the space utilization of their database design for several reasons. Intuitions about database storage and performance tend to be incorrect. Indexes usually matter a lot more in terms of disk use than the exact types of the columns. It's also a bad idea to spend a lot of energy optimizing the database before you have a significant amount of real-world data. Query performance alone varies dramatically between fake and real-world data sets, in part because the query planner will choose different strategies based on the sizes of tables and indexes and also because you usually don't know ahead of time what your access patterns will look like.
When it comes to databases, there really aren't very many hard and fast rules compared to rules of thumb. The rule of thumb on arrays is don't use them, and there are good reasons for that. It could be that you see massive performance and disk savings with tags and the loss of relational correctness doesn't bite you in a particular application, but you should investigate before choosing. All optimizations are trading flexibility for performance. You should make sure you don't need the flexibility and you do need the performance before making the trade.
One of my primary uses for arrays is for passing data to/from the application. Data may not be stored in the db as an array, but it really makes passing complex data structures to/from stored procedures a lot easier.
- Nonstandard SQL is required
- Database specific functions are used
- Later developers can be confused by the use of a non-standard data type
- SQL Commenting does not happen much in practice in my experience
- Array data can be handled using existing SQL constructs, so it is never an absolute necessity
- Other languages are better equipped for handling the data types (in my fuzzy subjective assessment)
My experience is mostly with Oracle, which has been adding various data types for years (XML, Objects, Arrays, etc). I can't think of a specific case where their use proved to be a real specific benefit to a project... though the usual argument is improved performance.
select array_agg(email_address), home_state from users group by home_state
Will give you a list of home states and all the email addresses that belong to that state.However, there are many cases where arrays are actually extremely useful, including the ability to be a useful intermediary type for stored procedure interfaces. For example, you can aggregate arrays of rows, and pass those into another stored procedure for processing, or you can use them for pulling info to/from your application. We do this extensively in PostgreSQL in part because DBD::Pg has excellent array support.
These are actually remarkably useful in PostgreSQL. Of course like any advanced feature it can be abused.
To the DB driver, it was just a string -- { 0, 0, 0, 0, 0, 0, 0} -- so I split it into an array manually in client code.
You can store json, or, even better, you can write sql that returns an arbitrary json structure.
So you can have one sql query return a nested array of hashes of arrays of hashes... handy if you need a retrieve a lot of different data at once that doesn't fit into a neat set of rows.
This could then be used as an input format for object-relational modelling.
Please just don't do it.
[1] http://rhaas.blogspot.com/2012/04/did-i-say-32-cores-how-abo...
Our application is 15 years old, and we're on a 64-core machine with 768Gb of RAM and 35Tb of disk.
We're running at 80% capacity.
Where do we go from here? Yes, we rewrite and scale out for the measly cost of £450k. That cost would have been avoided with the appropriate due diligence. That is not a cost anyone wants to swallow.
Then again I don't work disposable CRUD applications...
I don't see a obvious relationship between using arrays to store product tags and scalability.
What method of solving this problem are you suggesting, and what are the scalability characteristics of that solution?
Databases don't go out of their way to hurt scalability. Databases are shared state, and scaling that is just hard. Unless the problem is easy, in which case it's easy no matter what you use.
I would suggest that the relational model is maintained as there are no cases in which it isn't valid where arrays are and the optimiser is likely to come up with a better solution.
Relational databases (well all databases) have convenient tools which in the short term look good, but in the long term will hurt you.
Careful testing and feature selection is the alternative I am suggesting (from 20 years of experience with RDBMS platforms).
Which operations, specifically? We have the product tags example to work from, so it would help to be more concrete.
"into O(M*N) rather than O(N)"
I assume here that your target is "scalability to higher numbers of tags for a given product"? If so, please state that explicitly. Again, M and N aren't necessary, because we have an example to work from.
Are the operations still susceptible to worse scalability on this axis if you employ a GIN index?
"PgSQL documentation suggests cases that will hurt you."
Please elaborate.
"I would suggest that the relational model is maintained"
I don't see how your solution is relational and the other one is not. Can you please explain? How is using an array different from using, say, a string, which is essentially an array of characters? How is it different from using a date, the components of which you can access (e.g. grouping by the month)?
"the optimiser is likely to come up with a better solution."
Please elaborate.
http://www.codinghorror.com/blog/2009/06/scaling-up-vs-scali...
http://www.codinghorror.com/blog/2009/07/oh-you-wanted-aweso...