Home > Blockchain >  Store large chart data points in MySQL
Store large chart data points in MySQL

Time:08-17

I am creating an application that stores ECG data. I want to eventually graph this data in react but for now I need help storing it.

The biggest problem is storing the data points that will go along the x & y axis on the graph. Along the bottom is time and the y axis will be some value between. There are no limits but as it’s basically a heart rhythm most points will lie close to 0.

What is the best way to store the x and y data?? An example of the y data : [204.77, 216.86 … 3372.872] The files that I will be getting this data from can contain millions of data points, depending on the sampling rate and the time the experiment took.

What is the best way to store this type of data in MySQL. I cannot use any other DB as they’re not installed on the server this will be hosted on.

Thanks

CodePudding user response:

Well as you said there are million of points, so JSON is the best way to store these points. The space required to store a JSON document is roughly the same as for LONGBLOB or LONGTEXT;

Please have a look into this - https://dev.mysql.com/doc/refman/8.0/en/json.html

CodePudding user response:

The JSON encoding of your sample data would take 7-8 bytes per reading. Multiply that by the number of readings you will get at a single time. There is a practical limit of 16MB for a string being fed to MySQL. That seems "too big".

A workaround is to break the list into, say, 1K points at a time. Then there would be several rows, each row being manageable. There would be virtually no limit on the number of points you could store.

FLOAT is 4 bytes, but you would need a separate row for each reading. So, let's say about 25 bytes per row (including overhead); So size is not a problem, however, two other problems could arise. 7 digits is about the limit of precision for FLOAT. Fetching a million rows will not be very fast.

DOUBLE is 8 bytes, 16 digits of precision.

DECIMAL(6,2) is 3 bytes and overflows above 9999.99.

Considering that a computer monitor has less than 4 digits of precision (4K pixels < 10^4), I would argue for FLOAT as "good enough".

Another option is to take the JSON string, compress it, then store that in a LONGBLOB. The compression will give you an average of about 2.5 bytes per reading and the row for one complete reading will be a few megabytes.

I have experience difficulty in INSERTing a row bigger than 1MB. Changing a setting let me got to 16MB; I have not tried any row bigger than that. If you run into troubles there, start a new question with just that topic. I will probably come along and explain how to chunk up the data, thereby even allowing a "row" spread over multiple database rows that could effectively be even bigger than 4GB. That is the 'hard' limit for JSON, LONGTEXT and LONGBLOB.

You did not mention the X values. Are you assuming that they are evenly spaced? If you need to provide X,Y pairs, the computations above get a bit messier, but I have provided some of the data for analysis.

  • Related