SQL and varying data types

Status
Not open for further replies.

dlp

Ars Tribunus Militum
2,220
How do you store data in a SQL table that could be numeric or not? When it is numeric, you want to treat it as numbers (for analysis like averaging, min, max, custom equations, etc.).

Here's what I'm looking at: Varying pieces of equipment storing time-value pairs for specific attributes. We pull the data from the equipment and want to store it in a database instead of the awful data-duplicating text files. The issue is the value can vary from "On|Off" to "1|0" to "???" depending on the equipment. So true/false pairs can be alpha or numeric, and any value (true/false or readings like temperature) of any attribute can wind up as a series of question marks when the equipment isn't reporting properly. How do you structure your tables, column types, and presentation of this? MS SQL server if it matters, not sure what the presentation layer will be though likely ASP.NET and/or Flash.

I'm not looking for anyone to do the work for me, but looking for at least a start to research this. What comes to mind for me because I can't control the source data is store it as char()/varchar() and use my presentation layer to check each value before doing analysis, but this seems like it could be extremely slow and not scale as data sets grow. Thoughts?
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
One option is an EAV database

Lots of systems end up this way without forethought as to how to correctly design it. They are not inherently bad if used properly where appropriateness depends largely on their benefits as a sparse description of the modeled system (fringe benefits like being able to quickly expand the modeled attributes are very useful but should still be approached with care).

You can go for something like a star schema and have a different 'fact' table per data type.

Where the datum value is a single selection from the available set {"On", "Off"} you can have an additional FK relation ship from your fact table table to the possible values table with a simple integral key so you automatically denormalise (at the cost of a slightly more complex query). You then need to make sure that you only append to the possible values table (unless you genuinely want to change "Off" to mean false for example.

I would strongly suggest making use of the ternary logic in most RDBMS's and converting 'placeholders like "???" you know to mean 'no data' to be null where it is legal as well.

Querying this becomes slightly trickier as for any attribute you must first determine the fact table responsible for it, which, if you are doing dynamic query construction can be a bit of a pain (as it inevitably leads to sql string construction. If you do this you can still do the dynamic construction of a prepared statement string, then prepare it and fill in the value properly.

Note that querying by 'hand' does become more of a pain. For common operations providing views that break it down to look like a nice simple table per attribute can be very helpful.

One issue with these is that I know of no off the shelf provider of such a system. You almost inevitably end up implementing one of these on top of an existing RDBMS which can take time, and will bite you badly if you design the entity structure poorly (avoid trying to model inheritance relationships when doing it, it's a world of pain). You do then get all the benefits of your nice RDBMS like replication, easy backups, transactions etc.
 
dlp":24aw5xix said:
Here's what I'm looking at: Varying pieces of equipment storing time-value pairs for specific attributes. We pull the data from the equipment and want to store it in a database instead of the awful data-duplicating text files. The issue is the value can vary from "On|Off" to "1|0" to "???" depending on the equipment. So true/false pairs can be alpha or numeric, and any value (true/false or readings like temperature) of any attribute can wind up as a series of question marks when the equipment isn't reporting properly. How do you structure your tables, column types, and presentation of this? MS SQL server if it matters, not sure what the presentation layer will be though likely ASP.NET and/or Flash.

It sounds like you're collecting process data from your equipment. Have you looked at data historians (or operational historians)? They excel at storing time-value data with annotations. The one I've had experience with is PI, from OSISoft, but there's a number of common ones.

A decent one should provide the ability to calculate based on time series data, as well as connectors for importing and retrieving data in various forms.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
To agree with both Richard and Ominous this is considerable effort (we have several dedicated DBAs and one, very, smart guy who deals with much of the application side of things, this is not cheap) and serious time series data is stored in files (a whole bunch of hassle for the sys admins :) instead).

If you can start storing things in files with decent structure that is by far the most sensible first approach based on what you are discussing.

Think about whether you ever query the data as anything but an ordered stream? If it is only to calculate aggregate values then perhaps you can just store them in the db and keep the data as streams?
 

tinyMan

Ars Tribunus Angusticlavius
6,149
Subscriptor++
Unless you are trying to aggregate and report on different types of equipment as a single metric, it seems to me like you could have separate tables for each of your equipment types, along with a stored proc or application logic for performing intake for each piece of equipment.

I'd make use of the NULL value instead of dropping ??? into the DB for missing values, as that is what (imho) NULL is for.

If you need to roll up each of these equipment types, with their various values, into a single metric or report column, then it would probably be worth normalizing everything in your intake process prior to insertion in the DB.

This situation does not seem to scream out EAV to me, and since those solutions are generally a pain in the ass to work with and have potentially unpredictable performance, I try to avoid them at all costs anyways.
 

DebtAngel

Ars Scholae Palatinae
1,027
Buy a copy of splunk, put the text files on a storage device that supports dedupe, and call it a day?

Another potential solution would be to write a log analyzer utility that would transform the data in the log into a standardized format, which would then get imported into your SQL database. For that to be effective, you would need a very firm handle on what metrics you're trying to get from the aggregated logs. It would also mean that you would still have to hold onto the text files, as inevitably somebody will come up with a new metric/report which will require the logs to be re-transformed.

I'm being deliberately vague here, but the general advice of "start with your reports and work backwards" seems to apply here: the reports will dictate what needs to be in the SQL database, which will dictate what you will have to do to the original logs to get the data you actually need in there.
 
Status
Not open for further replies.