对大量数据使用SQL这样的函数好吗JSON_EXTRACT

Is it good to use SQL function like JSON_EXTRACT for large number of data

我正在实施一个数据库,每天要处理超过 2000 条记录,我将使用 [ 获取数据=55=]函数如MysqlJSON_EXTRACTORACLEJSON_VALUE。因为。我将数据存储在 JSON 列中。

我会用MysqlJSON_EXTRACTORACLEJSON_VALUE函数来搜索数据范围如下

SELECT *
FROM audit
where json_extract(detail_x,'$.eventDt') > '2017-10-01 00:00:00'
  And json_extract(detail,'$.eventDt')   < '2018-11-01 00:00:00'

最大日期范围将为 30 天。所以, 最大行数约为 2000 * 30 = 60000.

我的问题是。使用SQL函数可以吗MysqlJSON_EXTRACTORACLE JSON_VALUE 对于这种情况。

欢迎讨论。 谢谢

如果您知道所有行都有 eventDt,那么您应该将其提取到列中。列上的索引将加快您的许多查询。不可否认,索引对于如此宽的时间跨度可能没有用,但是当您检索少量记录时它应该会提高性能。

JSON 的目的应该是存储具有不适合列的不规则格式的数据。 MySQL 不直接索引 JSON。

您始终可以使用生成的列添加索引:

alter table audit
    add column eventDt date generated as (json_extract(detail_x,'$.eventDt')) stored;

create index idx_audit_eventDt on audit(eventDt);

MySQL 8.0.21 引入了一个新功能JSON_VALUE:

JSON_VALUE(json_doc, path)

Extracts a value from a JSON document at the path given in the specified document, and returns the extracted value, optionally converting it to a desired type.

JSON_VALUE() simplifies creating indexes on JSON columns by making it unnecessary in many cases to create a generated column and then an index on the generated column. You can do this when creating a table t1 that has a JSON column by creating an index on an expression that uses JSON_VALUE() operating on that column (with a path that matches a value in that column)

在这种情况下:

CREATE INDEX idx_t1 ON audit( (JSON_VALUE(detail, '$.eventDt' RETURNING DATE)) )

db<>fiddle demo