Search code examples
mysqlcellrecords

Is it possible to add a set of elements in one cell in mysql?


I have a sensor sends x,y and z values, I want to save this data in one row/cell in MySQL database. Because the sensor sends thousands of data (x,y,z) in a minute and I know if I insert each x,y and z data in a new row then it will take time to fetch all these data and that's time-consuming. I know this is possible in Neo4j but I am not sure if it is possible in MySQL. If it is, Can anybody please help me by showing an example?

The next image shows how I want the data to be inserted. [Example of the database that I am trying to use]


Solution

  • Since MySQL 5.7, you can have JSON fields:

    INSERT INTO t1 VALUES('[{"x":20,"y":40,"z":50},{"x":30,"y":50,"z":60}]');
    

    Then, to get for example all X coordinates per row:

    SELECT data->"$[*].x" FROM t1;
    

    Fiddle

    However, I think having a separate field for each coordinate would be OK too. Even if you have all the coordinates in one field, you're going to need to handle them in your app. Most of the times you can't beat the MySQL in optimized retrieving of data.