views:

320

answers:

13

In the process industry, lots of data is read, often at a high frequency, from several different data sources, such as NIR instruments as well as common instruments for pH, temperature, and pressure measurements. This data is often stored in a process historian, usually for a long time.

Due to this, process historians have different requirements than relational databases. Most queries to a process historian require either time stamps or time ranges to operate on, as well as a set of variables of interest.

Frequent and many INSERT, many SELECT, few or no UPDATE, almost no DELETE.

Q1. Is relational databases a good backend for a process historian?


A very naive implementation of a process historian in SQL could be something like this.

+------------------------------------------------+
| Variable                                       |
+------------------------------------------------+
| Id : integer primary key                       |
| Name : nvarchar(32)                            |
+------------------------------------------------+

+------------------------------------------------+
| Data                                           |
+------------------------------------------------+
| Id : integer primary key                       |
| Time : datetime                                |
| VariableId : integer foreign key (Variable.Id) |
| Value : float                                  |
+------------------------------------------------+

This structure is very simple, but probably slow for normal process historian operations, as it lacks "sufficient" indexes.

But for example if the Variable table would consist of 1.000 rows (rather optimistic number), and data for all these 1.000 variables would be sampled once per minute (also an optimistic number) then the Data table would grow with 1.440.000 rows per day. Lets continue the example, estimate that each row would take about 16 bytes, which gives roughly 23 megabytes per day, not counting additional space for indexes and other overhead.

23 megabytes as such perhaps isn't that much but keep in mind that numbers of variables and samples in the example were optimistic and that the system will need to be operational 24/7/365.

Of course, archiving and compression comes to mind.

Q2. Is there a better way to accomplish this? Perhaps using some other table structure?

+1  A: 

I believe you're headed in the right path. We have a similar situation were we work. Data comes from various transport / automation systems across various technologies such as manufacturing, auto, etc. Mainly we deal with the big 3: Ford, Chrysler, GM. But we've had a lot of data coming in from customers like CAT.

We ended up extracting data into a database and as long as you properly index your table, keep updates to a minimum and schedule maintenance (rebuild indexes, purge old data, update statistics) then I see no reason for this to be a bad solution; in fact I think it is a good solution.

JonH
A: 

There is probably a data structure that would be more optimal for your given case than a relational database.

Having said that, there are many reasons to go with a relational DB including robust code support, backup & replication technology and a large community of experts.

Your use case is similar to high-volume financial applications and telco applications. Both are frequently inserting data and frequently doing queries that are both time-based and include other select factors.

I worked on a mid-sized billing project that handled cable bills for millions of subscribers. That meant an average of around 5 rows per subscriber times a few million subscribers per month in the financial transaction table alone. That was easily handled by a mid-size Oracle server using (now) 4 year old hardware and software. Large billing platforms can have 10x that many records per unit time.

Properly architected and with the right hardware, this case can be handled well by modern relational DB's.

Eric J.
+1  A: 

Yes, a DBMS is appropriate for this, although not the fastest option. You will need to invest in a reasonable system to handle the load though. I will address the rest of my answer to this problem.

It depends on how beefy a system you're willing to throw at the problem. There are two main limiters for how fast you can insert data into a DB: bulk I/O speed and seek time. A well-designed relational DB will perform at least 2 seeks per insertion: one to begin the transaction (in case the transaction can not be completed), and one when the transaction is committed. Add to this additional storage to seek to your index entries and update them.

If your data are large, then the limiting factor will be how fast you can write data. For a hard drive, this will be about 60-120 MB/s. For a solid state disk, you can expect upwards of 200 MB/s. You will (of course) want extra disks for a RAID array. The pertinent figure is storage bandwidth AKA sequential I/O speed.

If writing a lot of small transactions, the limitation will be how fast your disk can seek to a spot and write a small piece of data, measured in IO per second (IOPS). We can estimate that it will take 4-8 seeks per transaction (a reasonable case with transactions enabled and an index or two, plus some integrity checks). For a hard drive, the seek time will be several milliseconds, depending on disk RPM. This will limit you to several hundred writes per second. For a solid state disk, the seek time is under 1 ms, so you can write several THOUSAND transactions per second.

When updating indices, you will need to do about O(log n) small seeks to find where to update, so the DB will slow down as the record counts grow. Remember that a DB may not write in the most efficient format possible, so data size may be bigger than you expect.

So, in general, YES, you can do this with a DBMS, although you will want to invest in good storage to ensure it can keep up with your insertion rate. If you wish to cut on cost, you may want to roll data over a specific age (say 1 year) into a secondary, compressed archive format.

EDIT: A DBMS is probably the easiest system to work with for storing recent data, but you should strongly consider the HDF5/CDF format someone else suggested for storing older, archived data. It is an flexible and widely supported format, provides compression, and provides for compression and VERY efficient storage of large time series and multi-dimensional arrays. I believe it also provides for some methods of indexing in the data. You should be able to write a little code to fetch from these archive files if data is too old to be in the DB.

BobMcGee
+2  A: 

It sounds like you're talking about telemetry data (time stamps, data points).

We don't use SQL databases for this (although we do use SQL databases to organize it); instead, we use binary streaming files to capture the actual data. There are a number of binary file formats that are suitable for this, including HDF5 and CDF. The file format we use here is a proprietary compressible format. But then, we deal with hundreds of megabytes of telemetry data in one go.

You might find this article interesting (links directly to Microsoft Word document):
http://www.microsoft.com/caseStudies/ServeFileResource.aspx?4000003362

It is a case study from the McClaren group, describing how SQL Server 2008 is used to capture and process telemetry data from formula one race cars. Note that they don't actually store the telemetry data in the database; instead, it is stored in the file system, and the FILESTREAM capability of SQL Server 2008 is used to access it.

Robert Harvey
The FILESTREAM capability of SQL Server 2008 seems interesting...
dalle
... and it fits rather well with a previous idea, mapping Time to DataBlobs, representing an array of variables.
dalle
A: 

I know you're asking about relational database systems, but those are unicorns. SQL DBMSs are probably a bad match for your needs because no current SQL system (I know of) provides reasonable facilities to deal with temporal data. depending on your needs you might or might not have another option in specialized tools and formats, see e. g. rrdtool.

just somebody
No "reasonable facilities"? What about time and date stamps? Or in the worst case, storing epoch milliseconds as BIG INTEGER or doubles?For that matter, while SQL isn't strictly relational, it's close enough for these purposes. -1 for being nitpicky and giving false information.
BobMcGee
@BobMcGee: read the book. the TIMESTAMP data type is only the beginning.
just somebody
@just somebody: I'm not spending $55 on a book just to prove you wrong. There are *weaknesses* in use of conventional SQL DBMSes to handle temporal data, but it's flat out wrong to say there are *no reasonable facilities* to handle them. People manifestly DO use SQL DBMSes to handle time data, it just takes a little bit of thought into an appropriate design.
BobMcGee
It's only $42.95 used ;)
RedFilter
@BobMcGee: you don't know it, but you're not spending $55 to avoid admitting that you were wrong. It's not necessarily a bad decision: the book would only frustrate you. There's so much more to temporal data than a TIMESTAMP data type, and you won't have it from a SQL DBMS any time soon (if ever, especially the Snodgrass' effort is troubling).
just somebody
@jjust somebody: there are other capabilities that facilitate working with temporal data, true, but none that the question needs. I'm not saying that an SQL DB is the *best* approach to working with time series, or for anything for that matter. SQL is not the best for just about anything, but it is reasonably good for a lot of things, and it is good enough for many more. This is just another case where it is "good enough." For other tasks involving temporal data it may be a very bad tool, or even a total failure. Or, is there something that *every* other answer is missing which you know?
BobMcGee
dalle
@BobMcGee: sure, as long as the OP only needs `WHERE ts BETWEEN x AND y`. things rarely stay that way.
just somebody
@just: there OP does not need anything but that, and can load pertinent slices of the data into something *else* for fancier analysis anyway. From what I can gather, the requirements that book stipulates increase the complexity of the system tremendously and would reduce performance accordingly. Performance is critical for a system handling that volume of data in real-time. Live in the real world: everyone uses SQL DBs for this, and nobody uses the system you suggest, because it's not worth the hassle *most* of the time. "would only frustrate you" just shows you to be an arrogant prat.
BobMcGee
eh, that wasn't a sign of arrogance, just empathy. Date's books irritate me to no end because they're very good at showing what we could (and should) have, but don't. and the increased complexity isn't that cut-and-dried either, see the (sadly patented) approach described in ... erm, The Third Manifesto? IIRC, but not sure. http://www.amazon.com/Databases-Types-Relational-Model-3rd/dp/0321399420
just somebody
A: 

Years ago, a customer of ours tried to load an RDBMS with real-time data collected from monitoring plant machinery. It didn't work in a simplistic way.

Is relational databases a good backend for a process historian?

Yes, but. It needs to store summary data, not details.

You'll need a front-end based in-memory and on flat files. Periodic summaries and digests can be loaded into an RDBMS for further analysis.

You'll want to look at Data Warehousing techniques for this. Most of what you want to do is to split your data into two essential parts ---

  1. Facts. The data that has units. Actual measurements.

  2. Dimensions. The various attributes of the facts -- date, location, device, etc.

This leads you to a more sophisticated data model.

 Fact: Key, Measure 1, Measure 2, ..., Measure n, Date, Geography, Device, Product Line, Customer, etc.

 Dimension 1 (Date/Time): Year, Quarter, Month, Week, Day, Hour

 Dimension 2 (Geography): location hierarchy of some kind

 Dimension 3 (Device): attributes of the device

 Dimension *n*:  attributes of each dimension of the fact
S.Lott
+3  A: 

I work with a SQL Server 2008 database that has similar characteristics; heavy on insertion and selection, light on update/delete. About 100,000 "nodes" all sampling at least once per hour. And there's a twist; all of the incoming data for each "node" needs to be correlated against the history and used for validation, forecasting, etc. Oh, there's another twist; the data needs to be represented in 4 different ways, so there are essentially 4 different copies of this data, none of which can be derived from any of the other data with reasonable accuracy and within reasonable time. 23 megabytes would be a cakewalk; we're talking hundreds-of-gigabytes to terabytes here.

You'll learn a lot about scale in the process, about what techniques work and what don't, but modern SQL databases are definitely up to the task. This system that I just described? It's running on a 5-year-old IBM xSeries with 2 GB of RAM and a RAID 5 array, and it performs admirably, nobody has to wait more than a few seconds for even the most complex queries.

You'll need to optimize, of course. You'll need to denormalize frequently, and maintain pre-computed aggregates (or a data warehouse) if that's part of your reporting requirement. You might need to think outside the box a little: for example, we use a number of custom CLR types for raw data storage and CLR aggregates/functions for some of the more unusual transactional reports. SQL Server and other DB engines might not offer everything you need up-front, but you can work around their limitations.

You'll also want to cache - heavily. Maintain hourly, daily, weekly summaries. Invest in a front-end server with plenty of memory and cache as many reports as you can. This is in addition to whatever data warehousing solution you come up with if applicable.

One of the things you'll probably want to get rid of is that "Id" key in your hypothetical Data table. My guess is that Data is a leaf table - it usually is in these scenarios - and this makes it one of the few situations where I'd recommend a natural key over a surrogate. The same variable probably can't generate duplicate rows for the same timestamp, so all you really need is the variable and timestamp as your primary key. As the table gets larger and larger, having a separate index on variable and timestamp (which of course needs to be covering) is going to waste enormous amounts of space - 20, 50, 100 GB, easily. And of course every INSERT now needs to update two or more indexes.

I really believe that an RDBMS (or SQL database, if you prefer) is as capable for this task as any other if you exercise sufficient care and planning in your design. If you just start slinging tables together without any regard for performance or scale, then of course you will get into trouble later, and when the database is several hundred GB it will be difficult to dig yourself out of that hole.

But is it feasible? Absolutely. Monitor the performance constantly and over time you will learn what optimizations you need to make.

Aaronaught
Thanks, your assumptions are correct. The same variable can't generate duplicate rows for the same timestamp. In one of my tests I used a primary key (time, variable), which worked rather well. But what I saw was that it was somewhat insufficient when storing large groups of variables (such as spectroscopic data) for different timestamps.
dalle
i'm curious. Is the reason you suggest removing the unneeded **id** column, is it also because you shave off 4 bytes per row, which after a billion rows is 4 GB? And since an index contains the table's clustered key, it's going to include the ID, which is more lost space. i just want to confirm that i do understand the reasons.
Ian Boyd
@dalle: If you have 10,000 nodes each with 1000 variables each that are all sampled 1 minute per day, then I might start to worry, but if it's just one "node" with the 1000 variables, no problem at all.
Aaronaught
@Ian Boyd: In my case the ID was a `bigint`, and there were already about 10 billion rows (hence the `bigint`), which comes out to 80 GB. But the real killer was the secondary covering index on node+time, which was really the only *useful* index, which had to cover almost everything and was just as big as the data itself, doubling the space used. There was simply no reason to continue using the surrogate key; it was never referenced anywhere.
Aaronaught
+1  A: 

Certainly a relational database is suitable for mining the data after the fact.

Various nuclear and particle physics experiments I have been involved with have explored several points from not using a RDBMS at all though storing just the run summaries or the run summaries and the slowly varying environmental conditions in the DB all the way to cramming every bit collected into the DB (though it was staged to disk first).

When and where the data rate allows more and more groups are moving towards putting as much data as possible into the database.

dmckee
+1  A: 

IBM Informix Dynamic Server (IDS) has a TimeSeries DataBlade and RealTime Loader which might provide relevant functionality.

Your naïve schema records each reading 100% independently, which makes it hard to correlate across readings- both for the same variable at different times and for different variables at (approximately) the same time. That may be necessary, but it makes life harder when dealing with subsequent processing. How much of an issue that is depends on how often you will need to run correlations across all 1000 variables (or even a significant percentage of the 1000 variables, where significant might be as small as 1% and would almost certainly start by 10%).

I would look to combine key variables into groups that can be recorded jointly. For example, if you have a monitor unit that records temperature, pressure and acidity (pH) at one location, and there are perhaps a hundred of these monitors in the plant that is being monitored, I would expect to group the three readings plus the location ID (or monitor ID) and time into a single row:

CREATE TABLE MonitorReading
(
    MonitorID        INTEGER NOT NULL REFERENCES MonitorUnit,
    Time             DATETIME NOT NULL,
    PhReading        FLOAT NOT NULL,
    Pressure         FLOAT NOT NULL,
    Temperature      FLOAT NOT NULL,
    PRIMARY KEY (MonitorID, Time)
);

This saves having to do self-joins to see what the three readings were at a particular location at a particular time, and uses about 20 bytes instead of 3 * 16 = 48 bytes per row. If you are adamant that you need a unique ID integer for the record, that increases to 24 or 28 bytes (depending on whether you use a 4-byte or 8-byte integer for the ID column).

Jonathan Leffler
This is actually a good idea, as may of the variables are always read/written at once, group-wise. For example spectroscopic data, such as from NIR-instruments, which could have output of several thousands of *variables* at any given time. Normalizing this data to fit the naive schema would be very wasteful.
dalle
A: 

You may want to look at KDB. It is specificaly optimized for this kind of usage: many inserts, few or no updates or deletes. It isn't as easy to use as traditional RDBMS though.

shura
A: 

The other aspect to consider is what kind of selects you're doing. Relational/SQL databases are great for doing complex joins dependent on multiple indexes, etc. They really can't be beaten for that. But if you're not doing that kind of thing, they're probably not such a great match.

If all you're doing is storing per-time records, I'd be tempted to roll your own file format ... even just output the stuff as CSV (groans from the audience, I know, but it's hard to beat for wide acceptance)

It really depends on your indexing/lookup requirements, and your willingness to write tools to do it.

Sharkey
A: 

""Temporal Data & the Relational Model" seems like an interesting book."

"the book would only frustrate you"

Both are true. It's a brilliant book that is decades ahead of its time. Mediocre programmers (and they are the majority) won't understand it, and even less so why it is relevant.

If the DBMS offers support for interval types plus Allen's operators, then a whole damn lot of queries become a whole damn lot easier to express, compared to the case when you do have timestamps, but neither of interval types and Allen's operators.

Erwin Smout
Are you also implying that only a mediocre programmer tries to match the tool to the problem? It does look like an interesting book (if perhaps a bit overly theoretical) but it also looks to be beyond the scope of the question. Also, if it is *that* critical you would expect there to be working implementations of its concepts all over the place. Now, if the question stipulated advanced analytics or a trail for data, THEN I'd agree that that book is required material.
BobMcGee
A: 

You may want to take a look at a Stream Data Manager System (SDMS).

While not addressing all your needs (long-time persistence), sliding windows over time and rows and frequently changing data are their points of strength.

Some useful links:

AFAIK major database makers all should have some kind of prototype version of an SDMS in the works, so I think it's a paradigm worth checking out.

Agos