索引实践和调优(3)

今儿啊,洲际哥给大家带来两个8.0的新特性

Ⅰ、不可见索引

schema_unused_indexes表,这个表有三个列分别是object_schema、object_name、index_name

对于这些索引,通常来说,就是删掉,但是直接删除有问题啊,可能现在没用到,过段时间要用啊

但在MySQL 8.0中我们可以先将索引设置为invisible,优化器就不会走这个索引,跑一段时间观察对业务有没有影响,如果没有影响再把这个索引删掉

(root@localhost) [sys]> select * from schema_unused_indexes limit 3;
+---------------+-------------+---------------------+
| object_schema | object_name | index_name          |
+---------------+-------------+---------------------+
| dbt3          | customer    | i_c_nationkey       |
| dbt3          | lineitem    | i_l_shipdate        |
| dbt3          | lineitem    | i_l_suppkey_partkey |
+---------------+-------------+---------------------+
3 rows in set (0.00 sec)
alter table xxx alter index_name invisible/visible;

Ⅱ、降序索引

(root@localhost) [dbt3]> explain select * from orders where o_custkey = 1 order by o_orderDate,o_orderStatus;
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table  | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra                                 |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
|  1 | SIMPLE      | orders | NULL       | ref  | i_o_custkey   | i_o_custkey | 5       | const |    6 |   100.00 | Using index condition; Using filesort |
+----+-------------+--------+------------+------+---------------+-------------+---------+-------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)

(root@localhost) [dbt3]> alter table orders add index idx_a_b_c(o_custkey,o_orderDate,o_orderStatus);
Query OK, 0 rows affected (6.33 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [dbt3]> explain select * from orders where o_custkey = 1 order by o_orderDate,o_orderStatus;
+----+-------------+--------+------------+------+-----------------------+-----------+---------+-------+------+----------+-----------------------+
| id | select_type | table  | partitions | type | possible_keys         | key       | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+--------+------------+------+-----------------------+-----------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | orders | NULL       | ref  | i_o_custkey,idx_a_b_c | idx_a_b_c | 5       | const |    6 |   100.00 | Using index condition |
+----+-------------+--------+------------+------+-----------------------+-----------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.06 sec)

(root@localhost) [dbt3]> explain select * from orders where o_custkey = 1 order by o_orderDate DESC,o_orderStatus;
+----+-------------+--------+------------+------+-----------------------+-------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table  | partitions | type | possible_keys         | key         | key_len | ref   | rows | filtered | Extra                                 |
+----+-------------+--------+------------+------+-----------------------+-------------+---------+-------+------+----------+---------------------------------------+
|  1 | SIMPLE      | orders | NULL       | ref  | i_o_custkey,idx_a_b_c | i_o_custkey | 5       | const |    6 |   100.00 | Using index condition; Using filesort |
+----+-------------+--------+------------+------+-----------------------+-------------+---------+-------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)

综上所述:到5.7为止,所有的排序必须是一个方向(A,B,C),因为索引默认都是(A asc,B asc,C asc)这种,如果DESC就得重新排序,即方向不一致就行不通

但是线上我们经常会碰到这种方向不一致的场景,怎么办呢?

方案一:8.0 新特性

alter table orders add index idx_cust_data_status (o_custkey,o_orderDate DESC,o_orderStatus);

其实在5.7中也可以这样执行,但是会把desc忽略掉,没用

方案二:虚拟列
目前大家基本上还用的5.7,我们这边用下面这种函数索引(虚拟列)的方法来实现一下需求

alter tablename add column as (function(column)) virtual/stored;

virtual 每次访问这个列需要一次额外计算,不占任何存储空间,只是一个计算得到的列,但是可以在上面增加一个索引,这个索引是占空间的,stored 在磁盘上占据空间

通常用virtual,不用stored

用函数表达式创建一个虚拟列,datediff是一个日期差,化降序为升序
(root@localhost) [dbt3]> alter table orders add column o_orderdate2 int as (datediff('2099-01-01',o_orderdate)) virtual;
Query OK, 0 rows affected (0.94 sec)
Records: 0  Duplicates: 0  Warnings: 0

在这个虚拟列上加一个索引
(root@localhost) [dbt3]> alter table orders add index idx_cust_date_status (o_custkey,o_orderdate2,o_orderstatus);
Query OK, 0 rows affected (8.99 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [dbt3]> explain select * from orders where o_custkey = 1 order by o_orderDate2,o_orderStatus;
+----+-------------+--------+------------+------+--------------------------------------------+----------------------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys                              | key                  | key_len | ref   | rows | filtered | Extra       |
+----+-------------+--------+------------+------+--------------------------------------------+----------------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | orders | NULL       | ref  | i_o_custkey,idx_a_b_c,idx_cust_date_status | idx_cust_date_status | 5       | const |    6 |   100.00 | Using where |
+----+-------------+--------+------------+------+--------------------------------------------+----------------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

这个做法只有5.7版本才能用,因为虚拟列和函数索引都是5.7才支持的

Ⅲ、索引倾斜

这个知识点先了解一下即可
背景
订单status上是否需要建索引,三种状态 0 1 2

倾斜:大部分状态是已完成,查询条件大部分时候是未完成状态,而这种未完成的状态又是很少量的,

如果一共1000w,未完成的只有10w,这样去取是符合B+ tree的条件,从大量数据取小部分数据,这时候本身是有意义的,但是如果去做一些比较复杂的查询,数据库的优化器可能会出错,因为他不知道索引是倾斜的,导致它在选择上有问题

5.7之前,优化器不能感知索引倾斜,如果优化器的执行计划出现小偏差,去看下他使用的哪个索引是否存在倾斜,如果存在那也没办法,后面8.0可能会改进

(root@localhost) [dbt3]> explain select * from lineitem where l_orderkey=1;
+----+-------------+----------+------------+------+--------------------------------------------+---------+---------+-------+------+----------+-------+
| id | select_type | table    | partitions | type | possible_keys                              | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+----------+------------+------+--------------------------------------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | lineitem | NULL       | ref  | PRIMARY,i_l_orderkey,i_l_orderkey_quantity | PRIMARY | 4       | const |    6 |   100.00 | NULL  |
+----+-------------+----------+------------+------+--------------------------------------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.11 sec)
优化器选择了pk

我们也可以强制走索引,但不是很建议这么做
(root@localhost) [dbt3]> explain select * from lineitem force index(i_l_orderkey) where l_orderkey=1;
+----+-------------+----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| id | select_type | table    | partitions | type | possible_keys | key          | key_len | ref   | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | lineitem | NULL       | ref  | i_l_orderkey  | i_l_orderkey | 4       | const |    6 |   100.00 | NULL  |
+----+-------------+----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

转载于:https://www.cnblogs.com/---wunian/p/9210631.html

计及风电并网运行的微电网及集群电动汽车综合需求侧响应的度策略研究(Matlab代码实现)内容概要:本文研究了计及风电并网运行的微电网及集群电动汽车综合需求侧响应的度策略,并提供了基于Matlab的代码实现。研究聚焦于在高渗透率可再生能源接入背景下,如何协微电网内部分布式电源、储能系统与大规模电动汽车充电负荷之间的互动关系,通过引入需求侧响应机制,建立多目标度模型,实现系统运行成本最小化、可再生能源消纳最大化以及电网负荷曲线的削峰填谷。文中详细阐述了风电出力不确定性处理、电动汽车集群充放电行为建模、电价型与激励型需求响应机制设计以及化求解算法的应用。; 适合人群:具备一定电力系统基础知识Matlab编程能力的研究生、科研人员及从事新能源、微电网、电动汽车等领域技术研发的工程师。; 使用场景及目标:①用于复现相关硕士论文研究成果,深入理解含高比例风电的微电网度建模方法;②为开展电动汽车参与电网互动(V2G)、需求侧响应等课题提供仿真平台技术参考;③适用于电力系统化、能源互联网、综合能源系统等相关领域的教学与科研项目开发。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注模型构建逻辑与算法实现细节,同时可参考文档中提及的其他相关案例(如储能化、负荷预测等),以拓宽研究视野并促进交叉创新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值