ITPub博客

首页 > 数据库 > MySQL > Generated Columns + index 代替函数索引

Generated Columns + index 代替函数索引

原创 MySQL 作者:zyclove2008 时间:2019-06-06 11:07:58 0 删除 编辑

#####################

OS:CentOS 7

DB:mysql 5.7

时间:2019.06

#####################

最近有一个项目遇到性能问题,我这里把优化的方法分享出来(这个方案并非谁独创,而是mysql 官方为实现函数索引的替代方法)。

问题是这样的:我们需要从一个JSON 字段中取出相应的项作为 where 子句的条件,如下:

CREATE TABLE `xxxxxx` (
  `id` varchar(96) DEFAULT NULL,
  `gid` varchar(96) DEFAULT NULL,
  `user_id` varchar(900) DEFAULT NULL,
  `order_no` varchar(96) DEFAULT NULL,
  `request_time` varchar(96) DEFAULT NULL,
  `code` varchar(96) DEFAULT NULL,
  `msg` varchar(96) DEFAULT NULL,
  `request_conetent` text,
  `request_response` text,
  `product_code` varchar(600) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
##SQL如下:
select id from table_name where json_extract(`request_conetent`,'$.vclN')='xxxxx';
+----+-------------+----------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table          | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | xxxxxx         | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   85 |   100.00 | Using where |
+----+-------------+----------------+------------+------+---------------+------+---------+------+------+----------+-------------+

大家都知道MYSQL不支持函数索引,那如何优化呢?

MYSQL虽然不支持函数索引,但有一个替代方法:Generated Columns + index 代替函数索引


###在 request_record 表中添加一个计算列,并在该列上创建函数索引
alter table table_name add column carno varchar(100) generated always as (json_extract(`request_conetent`,'$.vclN')) VIRTUAL,add key idx_carno(carno);

#表结构如下:
CREATE TABLE `xxxxxx` (
  `id` varchar(96) DEFAULT NULL,
  `gid` varchar(96) DEFAULT NULL,
  `user_id` varchar(900) DEFAULT NULL,
  `order_no` varchar(96) DEFAULT NULL,
  `request_time` varchar(96) DEFAULT NULL,
  `code` varchar(96) DEFAULT NULL,
  `msg` varchar(96) DEFAULT NULL,
  `request_conetent` text,
  `request_response` text,
  `product_code` varchar(600) DEFAULT NULL,
  `carno` varchar(100) GENERATED ALWAYS AS (json_extract(`request_conetent`,'$.vclN')) VIRTUAL,
  KEY `idx_carno` (`carno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


# 用 新增加的计算列(carno)查询
mysql> explain select id from request_record where carno ='xxxxxx';                                    
+----+-------------+----------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table          | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+----------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | xxxxxxx        | NULL       | ref  | idx_carno     | idx_carno | 303     | const |    1 |   100.00 | NULL  |
+----+-------------+----------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+

从执行计划里可以看出,已经走索引了


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29701030/viewspace-2646917/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 三星索引系统
下一篇: oracle 12c 监听
请登录后发表评论 登录
全部评论

注册时间:2014-06-10

  • 博文量
    10
  • 访问量
    19068