Store multiple values for multiple times in one column?

Joe Scotto

With MySQL is there a way to store an array of key/values for a single column? For example, I'm wanting to store a few (3 - 6) different values for times of the day such as this:

12AM: 1.2
1AM: 3.0
6AM: 4.0

I've seen that doing a relational table might be the right way to do this but I would have no control over the column timeframes or values, they would be user input data. The data here would also link to a single user.

I could have a separate table linked by UID that would have multiple rows with the key/value but wouldn't that be slow in the long run with say 10 million plus rows of data?

Bill Karwin

MySQL does not support an array data type, though it supports a JSON data type in MySQL 5.7 and later.

Strictly speaking, you can store any kind of semi-structured data you want in a TEXT column. But then you will find it's not easy or efficient to search it.

You might find yourself wishing to search your "array" for a specific value. In such a case, you'll be tempted to use LIKE '%pattern%' or regular expression matching or something like that to find a particular time of day. These expressions are hard to impossible to optimize with indexes, so searches generally become table-scans.

The alternative is to use a child table, with one datetime per row referencing your UID. Yes, this can grow to be a long table with millions of rows, but it is much easier to optimize the search with indexes. An indexes search on a table with millions of rows will beat a non-indexes table-scan search on a much smaller table.

You'll experience less gray hair and have a much happier experience with SQL if you live by the rule that every column should be one scalar value — not an "array," or a comma-separated list, or a "document" of JSON or XML, or any other sort of semi-structured data like that.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

View comma separated values of one column in multiple rows or in multiple values

分類Dev

How to merge multiple columns values into one column?

分類Dev

Modify one column values based on multiple conditions of another column

分類Dev

`.one` function is attached multiple times

分類Dev

Multiple rows to one column

分類Dev

How to store inputs multiple times into local storage

分類Dev

R - Recoding column with multiple text values associated with one code

分類Dev

Function to return many values in one column based on multiple queries

分類Dev

TSQL: Join columns, but rows in one column have multiple values

分類Dev

In SQL Joins - one to many select one column multiple times based upon context

分類Dev

How to store multiple values in array

分類Dev

RecyclerView onBindViewHolder called multiple times for one item

分類Dev

How to Pivot one columns multiple times?

分類Dev

Merge multiple columns into one column with multiple rows

分類Dev

Convert one column to multiple rows

分類Dev

Filter by multiple values on the same column

分類Dev

Split column values into multiple columns with

分類Dev

Trying to find multiple values from one column and group by another column - can't seem to figure it out

分類Dev

how to select multiple same column in one column

分類Dev

Is it a good idea to store multiple values as SharedPreference?

分類Dev

Pandas/Python: interpolation of multiple columns based on values specified for one reference column

分類Dev

Spread values from one column into multiple new columns using data.table

分類Dev

remove duplicate entries in one column and linearize the values in multiple rows to a single row

分類Dev

Entity framework The column 'x' was specified multiple times for 'Filter1'

分類Dev

How to select multiple columns and group by one column

分類Dev

Divide one column into multiple in a new table

分類Dev

JS: How to match one capture group multiple times in the same string?

分類Dev

Mapping elements of one list multiple times to elements of a second list

分類Dev

Is it allowed to do longjmp() multiple times for one setjmp() call?

Related 関連記事

  1. 1

    View comma separated values of one column in multiple rows or in multiple values

  2. 2

    How to merge multiple columns values into one column?

  3. 3

    Modify one column values based on multiple conditions of another column

  4. 4

    `.one` function is attached multiple times

  5. 5

    Multiple rows to one column

  6. 6

    How to store inputs multiple times into local storage

  7. 7

    R - Recoding column with multiple text values associated with one code

  8. 8

    Function to return many values in one column based on multiple queries

  9. 9

    TSQL: Join columns, but rows in one column have multiple values

  10. 10

    In SQL Joins - one to many select one column multiple times based upon context

  11. 11

    How to store multiple values in array

  12. 12

    RecyclerView onBindViewHolder called multiple times for one item

  13. 13

    How to Pivot one columns multiple times?

  14. 14

    Merge multiple columns into one column with multiple rows

  15. 15

    Convert one column to multiple rows

  16. 16

    Filter by multiple values on the same column

  17. 17

    Split column values into multiple columns with

  18. 18

    Trying to find multiple values from one column and group by another column - can't seem to figure it out

  19. 19

    how to select multiple same column in one column

  20. 20

    Is it a good idea to store multiple values as SharedPreference?

  21. 21

    Pandas/Python: interpolation of multiple columns based on values specified for one reference column

  22. 22

    Spread values from one column into multiple new columns using data.table

  23. 23

    remove duplicate entries in one column and linearize the values in multiple rows to a single row

  24. 24

    Entity framework The column 'x' was specified multiple times for 'Filter1'

  25. 25

    How to select multiple columns and group by one column

  26. 26

    Divide one column into multiple in a new table

  27. 27

    JS: How to match one capture group multiple times in the same string?

  28. 28

    Mapping elements of one list multiple times to elements of a second list

  29. 29

    Is it allowed to do longjmp() multiple times for one setjmp() call?

ホットタグ

アーカイブ