mysql - 在mysql中使用和搜索jsonb数据

标签 mysql database postgresql jsonb

我们之前设置了 PostgreSQL 数据库,并使用 JSONB 将一些数据保存在 json 中,因为我们需要根据我们可以存储在其中的任何 JSON 来保持其中一列数据的动态性。我们还可以通过 JSON 键和值执行文本搜索和比较,一切都运行良好。

最近由于一些技术原因,我们的团队决定迁移到 MySQL。所以我的问题是 MySQL 中的 JSON 是否支持像 PostgreSQL 那样的搜索,以及 MySQL 中的 JSON 支持有多稳定,因为它是在版本 >= MySQL 5.7.8、MySQL 中引入的。

最佳答案

直接来自 MySQL 文档:

JSON support

Beginning with MySQL 5.7.8, MySQL supports a native JSON type. JSON values are not stored as strings, instead using an internal binary format that permits quick read access to document elements. JSON documents stored in JSON columns are automatically validated whenever they are inserted or updated, with an invalid document producing an error. JSON documents are normalized on creation, and can be compared using most comparison operators such as =, <, <=, >, >=, <>, !=, and <=>; for information about supported operators as well as precedence and other rules that MySQL follows when comparing JSON values, see Comparison and Ordering of JSON Values.

MySQL 5.7.8 also introduces a number of functions for working with JSON values. These functions include those listed here:

Functions that create JSON values: JSON_ARRAY(), JSON_MERGE(), and JSON_OBJECT(). See Section 12.16.2, “Functions That Create JSON Values”. Functions that search JSON values: JSON_CONTAINS(), JSON_CONTAINS_PATH(), JSON_EXTRACT(), JSON_KEYS(), and JSON_SEARCH(). See Section 12.16.3, “Functions That Search JSON Values”. Functions that modify JSON values: JSON_APPEND(), JSON_ARRAY_APPEND(), JSON_ARRAY_INSERT(), JSON_INSERT(), JSON_QUOTE(), JSON_REMOVE(), JSON_REPLACE(), JSON_SET(), and JSON_UNQUOTE(). See Section 12.16.4, “Functions That Modify JSON Values”. Functions that provide information about JSON values: JSON_DEPTH(), JSON_LENGTH(), JSON_TYPE(), and JSON_VALID(). See Section 12.16.5, “Functions That Return JSON Value Attributes”. In MySQL 5.7.9 and later, you can use column->path as shorthand for JSON_EXTRACT(column, path). This works as an alias for a column wherever a column identifier can occur in an SQL statement, including WHERE, ORDER BY, and GROUP BY clauses. This includes SELECT, UPDATE, DELETE, CREATE TABLE, and other SQL statements. The left hand side must be a JSON column identifier (and not an alias). The right hand side is a quoted JSON path expression which is evaluated against the JSON document returned as the column value.

See Section 12.16.3, “Functions That Search JSON Values”, for more information about -> and JSON_EXTRACT(). For information about JSON path support in MySQL 5.7, see Searching and Modifying JSON Values. See also Secondary Indexes and Virtual Generated Columns.

此外,我们一直在使用 MySQL 并以 JSON 格式存储数据,就性能和可扩展性而言,它运行得非常好。

您可以在以下位置阅读更多相关信息:https://dev.mysql.com/doc/refman/5.7/en/json.html

关于mysql - 在mysql中使用和搜索jsonb数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43085130/

相关文章:

php - 使用 Android 应用程序时无法加载数据库

linux - 如何使用 cloudwatch 以外的脚本获取 AWS RDS CPU 和内存指标

php - PostgreSQL 选择查询 (GPS)

php - 在 php 类中返回多个 mysql 结果

mysql日期转换错误

php - 仅显示来自 PHP 和 MySQL 的最新三个结果

sql - Play框架忽略进化脚本

mysql - 更新具有大量数据的表中的行

database - 您如何编写独立于数据库的应用程序?

java - 无法使用 hibernate 5.0 将 List<String> 插入到 postgres 9.4 表中,其中要插入的列的类型为字符 varying[]