【发布时间】:2020-11-01 01:08:56
【问题描述】:
我有这张桌子:
CREATE TABLE `datacollector` (
`id` binary(16) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`processed` char(1) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'N',
`processed_at` timestamp NULL DEFAULT NULL,
`request` json NOT NULL,
`response` json NOT NULL,
`response_date` timestamp GENERATED ALWAYS AS (from_unixtime(json_unquote(json_extract(`response`,_utf8mb4'$.date')))) VIRTUAL NULL,
`sha224` varchar(80) COLLATE utf8mb4_unicode_ci NOT NULL,
`available_for` json NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_sha224` (`sha224`),
KEY `created_at` (`created_at`) /*!80000 INVISIBLE */,
KEY `cmoa` (`created_at`,`processed`) /*!80000 INVISIBLE */,
KEY `response_date` (`response_date`) /*!80000 INVISIBLE */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
我使用在available_for 中创建字符串数组的触发器。现在我想为available_for 创建一个索引。
我已经试过了
ALTER TABLE `hwtools`.`datacollector`
ADD INDEX `available_for_idx` ((CAST(json_unquote(available_for) AS UNSIGNED ARRAY)));
或
ALTER TABLE `hwtools`.`datacollector`
ADD INDEX `available_for_idx` ((CAST(available_for AS UNSIGNED ARRAY)));
这总是给我一个错误。
存储在 available_for 中的 JSON 看起来像 '["getUserInfo", "stashClient"]'。
我现在如何使用最后一个 MySQL 8 创建正确的索引?
感谢您对我的问题的意见。
【问题讨论】:
-
确切的错误信息是什么? json 值似乎是字符串,而不是无符号整数。为什么使用 unsigned 作为数组类型?
-
因为无法使用 Varchar...
13:21:41 ALTER TABLE hwtools.datacollector ADD INDEX available_for_idx ((CAST(available_for AS VARCHAR(32) ARRAY))) VISIBLE Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(32) ARRAY))) VISIBLE' at line 2 0.031 sec -
还有
14:39:00 ALTER TABLE hwtools.datacollector ADD INDEX available_for_idx ((CAST(available_for AS CHAR ARRAY))) VISIBLE Error Code: 1235. This version of MySQL doesn't yet support 'CAST-ing data to array of char/binary BLOBs' 0.031 sec -
你应该使用char,而不是varchar,但是第二个错误表明你的版本可能还不支持这个功能。
-
您希望在 JSON 列上建立什么索引?索引键是有意义的,但索引文档没有。请包含您要优化的查询。