mysql generated_Generated Columns + index 代替函数索引

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

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  |

+----+-------------+----------------+------------+------+---------------+-----------+---------+-------+------+----------+-------+

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

MySQL generated columns are virtual columns that are not stored physically on the disk but are computed based on an expression or formula. They are also known as computed columns or virtual columns. Generated columns were introduced in MySQL version 5.7 and they provide a way to create a column whose values are computed from an expression. The expression can involve one or more other columns in the same table, constants, or functions. To create a generated column, you need to specify the column name, data type, and the expression that computes the values for that column. Here is an example: ``` CREATE TABLE employees ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), salary INT, tax_rate DECIMAL(4,2), net_salary DECIMAL(10,2) GENERATED ALWAYS AS (salary - (salary * tax_rate)) STORED ); ``` In this example, the `net_salary` column is a generated column whose value is computed from the `salary` and `tax_rate` columns using the expression `(salary - (salary * tax_rate))`. Generated columns can be either `STORED` or `VIRTUAL`. A `STORED` generated column is computed when a row is inserted or updated and its value is stored on the disk like any other column. A `VIRTUAL` generated column is computed dynamically when it is accessed and its value is not stored on the disk. Generated columns can provide significant performance benefits in certain scenarios because they can avoid the need to compute values in application code or in queries. However, they can also increase the storage requirements for a table, so it's important to use them judiciously.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值