【GreatSQL优化器-08】statistics和index dives

SQL优化(二)统计信息 数据库的统计信息非常重要,如果没有正确地收集表的统计信息,或者没有及时地更新表的统计信息,SQL就有可能走错执行计划,也就会出现性能问题。统计信息主要分为系统的统计信息、数据字典的统计信息以及动态性能视图基表的统计信息,我们主要关注表、列、索引的统计信息。注:本文涉及到的数据库版本为Oracle Database 19c Enterprise Edition Release表的统计信息主要包含表的以及​,通过查询数据字典获取表的统计信息。 阅读详情

【GreatSQL优化器-08】statistics和index dives

一、statistics和index_dives介绍

GreatSQL的优化器对于查询条件带有范围的情况,需要根据 mm tree 来估计该范围内大概有多少行,然后以此来计算cost。对于等号条件,给出了两种方法来估计对应行数--Statistics和index dives,前者不精确后者精确,可以通过系统变量eq_range_index_dive_limit设置阈值来决定用哪种方法来估计等号条件的行数。对于一条查询 SQL 如果等号条件太多的时候执行index dives会占用较多资源,这时候设置一个合理的阈值改为统计值估计可以有效避免占用过多资源,提升执行效率。

名称定义说明
Statistics用统计值来估计等号条件的行数,不精确意味着SKIP_RECORDS_IN_RANGE模式,计算方式: rows = table->key_info[keyno].records_per_key()
index dives精确计算等号条件的行数意味着RECORDS_IN_RANGE,计算方式: rows = this->records_in_range()

下面用一个简单的例子来说明index dives是什么:

greatsql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT,date1 DATETIME);
greatsql> INSERT INTO t1 VALUES (1,10,'2021-03-25 16:44:00.123456'),(2,1,'2022-03-26 16:44:00.123456'),(3,4,'2023-03-27 16:44:00.123456'),(5,5,'2024-03-25 16:44:00.123456'),(7,null,'2020-03-25 16:44:00.123456'),(8,10,'2020-10-25 16:44:00.123456'),(11,16,'2023-03-25 16:44:00.123456');
greatsql> CREATE TABLE t2 (cc1 INT PRIMARY KEY, cc2 INT);
greatsql> INSERT INTO t2 VALUES (1,3),(2,1),(3,2),(4,3),(5,15);
greatsql> CREATE TABLE t3 (ccc1 INT, ccc2 varchar(100));
greatsql> INSERT INTO t3 VALUES (1,'aa1'),(2,'bb1'),(3,'cc1'),(4,'dd1'),(null,'ee');
greatsql> CREATE INDEX idx1 ON t1(c2);
greatsql> CREATE INDEX idx2 ON t1(c2,date1);
greatsql> CREATE INDEX idx2_1 ON t2(cc2);
greatsql> CREATE INDEX idx3_1 ON t3(ccc1);

greatsql> SELECT * FROM t1 WHERE (c1=4 AND c2<10) OR (c2=4 AND c1<9) OR (c2=10 AND c1<9) OR (c2=12 AND c1=8) OR (c2<8 AND date1<'2023-03-25 16:44:00.123456') OR (c2<15 AND date1<'2023-03-25 16:44:00.123456');
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx1",
                        "ranges": [
                          "NULL < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 9, # 这里的值包含c2 = 10和c2 = 12根据index dive估算出来的值
                        "cost": 4.66,
                        "chosen": false,
                        "cause": "cost"
                      },
                      {
                        "index": "idx2",
                        "ranges": [
                          "NULL < c2 < 4",
                          "c2 = 4",
                          "4 < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": true,
                        "in_memory": 1,
                        "rows": 9, # 这里的值包含c2 = 10和c2 = 12根据index dive估算出来的值
                        "cost": 1.16366,
                        "chosen": false,
                        "cause": "cost"
                      }
                    ],
                  }
                }
              }

上面的二叉树转置结果如下,按照左节点到最右节点来累计总行数。

idx1索引的结果:count=2
              8<c2<10黑
             /         \
          0<c2<8红      c2=10
                          \
                           10<c2<12
                              \
                              c2=12
                                \
                               12<c2<15

idx2索引的结果:count=3
                c2=4黑 
             /         \
          0<c2<4红      4<c2<8
                          \
                           8<c2<10
                              \
                              c2=10
                                \
                               10<c2<12
                                    \
                                     c2=12 
                                       \
                                     12<c2<15
# 注:count表示c2等号条件的数量

二、test_quick_select代码解释

范围扫描计算行数和 cost 在test_quick_select函数实现,用来预估不同范围对应的行数和 cost,最后选取适合的 index 来进行查询。

# 代码流程:make_join_plan --> estimate_rowcount --> get_quick_record_count --> check_skip_records_in_range_qualification -> test_quick_select -> get_key_scans_params -> check_quick_select

int test_quick_select() {
  if (cond)
          tree = get_mm_tree(); # 这里创建mm tree
  if (tree) {
      Opt_trace_object trace_range_alt(trace, "analyzing_range_alternatives",
                                       Opt_trace_context::RANGE_OPTIMIZER);
      AccessPath *range_path = get_key_scans_params(
          thd, &param, tree, false, true, interesting_order,
          skip_records_in_range, best_cost, needed_reg);

      /* Get best 'range' plan and prepare data for making other plans */
      if (range_path) {
        best_path = range_path; # 行数信息在这里面
        best_cost = best_path->cost; # cost信息在这里面
      }
  }
}

AccessPath *get_key_scans_params() {
  for (idx = 0; idx < param->keys; idx++) {
      // 主要计算下面2个参数的值,found_records是找到的行数,另一个就是cost
      ha_rows found_records;
      Cost_estimate cost;
      found_records = check_quick_select(
          thd, param, idx, read_index_only, key, update_tbl_stats,
          order_direction, skip_records_in_range, &mrr_flags, &buf_size, &cost,
          &is_ror_scan, &is_imerge_scan);
  }
}

ha_rows check_quick_select() {
  # eq_ranges_exceeds_limit函数统计等号条件的行数,返回count
  param->use_index_statistics = eq_ranges_exceeds_limit(
      tree, &range_count, thd->variables.eq_range_index_dive_limit);
  # 以下这行代码没用,是冗余代码
  *bufsize = thd->variables.read_rnd_buff_size;
  # multi_range_read_info_const函数累加找到的范围内的总行数,并且计算出所有rows对应的cost
  rows = file->multi_range_read_info_const(keynr, &seq_if, (void *)&seq, 0,
                                           bufsize, mrr_flags, cost);
  # 设置quick_condition_rows用于后面best_access_path时候计算cost
  param->table->quick_condition_rows =
          min(param->table->quick_condition_rows, rows);
}

ha_rows handler::multi_range_read_info_const() {
  if(SKIP_RECORDS_IN_RANGE)
    rows = table->key_info[keyno].records_per_key() # 对于SKIP_RECORDS_IN_RANGE模式,用records_per_key估计行数
  else
    rows = this->records_in_range() # 对于非SKIP_RECORDS_IN_RANGE模式,用records_in_range估计行数
}

# 对于innodb计算范围内行数的时候,如果没有找到值那么就返回1
ha_rows ha_innobase::records_in_range() {
  if (n_rows == 0) {
    n_rows = 1;
  }
}

表一,skip_records_in_range为true的场景,意味着index dive可以被跳过,改为Statistics估计row和cost

场景条件
check_skip_records_in_range_qualification() (右边全都要满足)单表查询
FORCE INDEX单个索引
没有子查询
不涉及Fulltext Index
没有GROUP-BY或者DISTINCT语句
没有ORDER-BY语句
不是EXPLAIN查询语句
param->use_index_statistics值为true的时候

注:skip_records_in_range的值由check_skip_records_in_range_qualification函数决定,具体可以看该函数

表二,param->use_index_statistics值

thd->variables.eq_range_index_dive_limituse_index_statistics说明
0false不用Statistics代替index dives
1true用Statistics代替index dives
其他值(默认200) :count>=limittrue用Statistics代替index dives
其他值(默认200) :count<limitfalse不用Statistics代替index dives

注:count 是指mm二叉树里面等号的数量

三、实际例子说明

接下来看几个例子来说明上面的代码:

greatsql> SELECT * FROM t1 WHERE (c1=4 AND c2<10) OR (c2=4 AND c1<9) OR (c2=10 AND c1<9) OR (c2=12 AND c1=8) OR (c2<8 AND date1<'2023-03-25 16:44:00.123456') OR (c2<15 AND date1<'2023-03-25 16:44:00.123456');

场景一,eq_range_index_dive_limit=200,skip_records_in_range=false

                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx1",
                        "ranges": [
                          "NULL < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 9, # 按照上面的范围计算:3+1+2+1+1+1,这里找到的结果是0行的话也会返回1
                        "cost": 4.66,
                        "chosen": false,
                        "cause": "cost"
                      },
                      {
                        "index": "idx2",
                        "ranges": [
                          "NULL < c2 < 4",
                          "c2 = 4",
                          "4 < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": true,
                        "in_memory": 1,
                        "rows": 9, # 按照上面的范围计算:3+1+2+1+1+1,这里找到的结果是0行的话也会返回1
                        "cost": 1.16366,
                        "chosen": false,
                        "cause": "cost"
                      }
                    ],
                  }
                }
              }

场景二,eq_range_index_dive_limit=3,skip_records_in_range=true or false

greatsql> SET eq_range_index_dive_limit=3;
                 "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx1",
                        "ranges": [
                          "NULL < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 9, # 这里等号数量为2个,数量没有超过3个,因此还是用原来的index dive计算方法,公式:3+1+2+1+1+1
                        "cost": 4.66,
                        "chosen": false,
                        "cause": "cost"
                      },
                      {
                        "index": "idx2",
                        "ranges": [
                          "NULL < c2 < 4",
                          "c2 = 4",
                          "4 < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": false,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": true,
                        "in_memory": 1,
                        "rows": 8, # 这里等号数量为3个,等于eq_range_index_dive_limit,因此用statistics方法来统计行数,公式:3+1+1+1+1+1
                        "cost": 1.0632,
                        "chosen": false,
                        "cause": "cost"
                      }
                    ],

场景三,eq_range_index_dive_limit=2,skip_records_in_range=true

greatsql> SET eq_range_index_dive_limit=2;
                 "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx1",
                        "ranges": [
                          "NULL < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": false,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 1,
                        "rows": 8, # 这里等号数量为2个,等于eq_range_index_dive_limit,因此用statistics方法来统计行数,公式:3+1+1+1+1+1
                        "cost": 4.31,
                        "chosen": false,
                        "cause": "cost"
                      },
                      {
                        "index": "idx2",
                        "ranges": [
                          "NULL < c2 < 4",
                          "c2 = 4",
                          "4 < c2 < 8",
                          "8 <= c2 < 10",
                          "c2 = 10",
                          "10 < c2 < 12",
                          "c2 = 12",
                          "12 < c2 < 15"
                        ],
                        "index_dives_for_eq_ranges": false,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": true,
                        "in_memory": 1,
                        "rows": 8, # 这里等号数量为3个,等于eq_range_index_dive_limit,因此用statistics方法来统计行数,公式:3+1+1+1+1+1
                        "cost": 1.0632,
                        "chosen": false,
                        "cause": "cost"
                      }
                    ],

上面结果总结如下,可以看出 index dive 对于等号计算精确,而 statistics 方法计算等号条件是不精确的。也可以看出优化器预估范围行数肯定是比实际值大于或者等于的。

eq_range_index_dive_limitNULL < c2 < 88 <= c2 < 10c2 = 1010 < c2 < 12c2 = 1212 < c2 < 15总计
实际分布3020005
场景一2003121119
场景二33121119
场景三23111118

注:index dive只针对等号条件进行精确计数

四、总结

从上面我们认识了对于带有条件的查询语句,对等号条件有两种预估行数方法,认识了他们的区别以及使用方法。对于系统变量eq_range_index_dive_limit的设置也有了一个比较好的认识。在以后的使用中,对于等号条件太多的情况,可以通过设置eq_range_index_dive_limit阈值来控制等号条件估计行数的行为。

考考你:如果上面例子的eq_range_index_dive_limit设为1的话,估计结果是多少行?欢迎下方留言评论~

Enjoy GreatSQL :)

关于 GreatSQL

GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。

相关链接: GreatSQL社区 Gitee GitHub Bilibili

GreatSQL社区:

image

社区有奖建议反馈: https://greatsql.cn/thread-54-1-1.html

社区博客有奖征稿详情: https://greatsql.cn/thread-100-1-1.html

(对文章有疑问或者有独到见解都可以去社区官网提出或分享哦~)

技术交流群:

微信&QQ群:

QQ群:533341697

微信群:添加GreatSQL社区助手(微信号:wanlidbc )好友,待社区助手拉您进群。

利用图层自动识别算法实现CAD到Revit BIM模型的高效转换 - VS2022与AutoCAD2024开发环境配置指南 本文详细介绍了如何配置VS2022与AutoCAD2024开发环境,并利用图层自动识别算法实现CAD图纸到Revit BIM模型的高效自动化转换。通过解析CAD图层的规律性,该算法能智能识别并分类墙、门、窗等构件,提取几何信息并驱动Revit自动创建三维模型,从而将传统繁琐的人工“翻模”工作转化为高效、准确的自动化流程,大幅提升BIM模型创建效率。 阅读详情

相关推荐

①Modbus TCP转Modbus RTU/ASCII网关同步采集无需编程高速轻松组网

MS-A1-50X1系列作为MODBUS TCP通信的服务器进行动作。可通过MODBUS TCP 通信,将 MS-A1-50X1 系列产品通过 RS485 采集的仪器仪表之类的值作为通信数 据输出到 PLC,上位机等。

2401_89571741的博客 1403

mysql index dive_MySQL非唯一索引值分布不均匀与优化器参数eq_range_index_dive_limit

MySQL5.6引入了一个新的系统变量eq_range_index_dive_limit。这可能会显着影响查询执行计划。这里我举一个典型的例子。有一个表“t”。主键由从“id1”开始的多个列组成。表t中有1.67M行,id1的基数是46K(这些数字可以通过SHOW TABLE STATUS / SHOW INDEX收集)。因此,每个id1平均有36行(1.67M / 46K = 36),但实际的i...

weixin_39581739的博客 478

别光看教程了!手把手带你用STM32F103C8T6最小系统板点亮第一个LED(附完整电路图)

本文详细介绍了如何使用STM32F103C8T6最小系统板点亮第一个LED,从硬件连接到软件开发环境搭建,再到编写和下载程序,手把手教你完成STM32入门的第一步。适合初学者快速上手STM32开发,附完整电路图和代码示例。

weixin_28678831的博客 207

通俗易懂SQL server 中的统计信息statistics

一:什么是数据库的统计信息? 在数据库使用中经常会看见这个东西 可以看到有键,约束,触发器,索引,最后这个就是统计信息。 统计信息字面意思就是 统计一下多少数据呗,知道有多少东西,然后去分配空间去用呗。 你如果没有统计信息,里面有几百万条数据你都不知道,你查个东西不跟海底捞针一样,你要是知道有多少条数据,而且还 知道你想要的东西在哪个地方,那你查东西不快都不行。 所以我认为,统计信...

Marzlam的博客 3983

麒麟操作系统安装达梦数据库ARM版

Linux安装达梦数据库ARM版

运维@小兵的博客 8785

PackML工业标准解析:统一机器状态管理与数据交换的实践指南

在工业自动化与智能制造领域,实现设备间的互联互通与高效协同是核心挑战。这背后依赖于一套标准化的机器行为与数据交换规范,其原理在于通过定义统一的状态模型与接口协议,消除不同厂商设备间的“语言”隔阂。这项技术的核心价值在于大幅降低系统集成复杂度、提升运维效率与生产透明度,是构建数字化工厂的基石。其典型应用场景包括包装机械、离散制造产线等需要多设备协同的领域。本文聚焦的PackML(包装机械语言)标准,正是这一理念的杰出实践,它通过一套严谨的**状态机模型**和**数据标签(PackTags)** 体系,为机器提

weixin_30070339的博客 417

SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇)

SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇) 最近真的没有什么精力写文章,天天加班,为了完成这个系列,硬着头皮上了 再看这篇文章之前请大家先看我之前写的第一篇和第二篇 第一篇:SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第一篇) 第二篇:SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第二篇) 1、统计信息的含义与作用...

dingshen2767的博客 342

科研人必备!用WisPaper搭建个人文献知识库的完整指南(附Zotero联动技巧)

本文详细介绍了如何利用WisPaper搭建高效的个人文献知识库,包括文件命名标准化、文件夹结构设计、语义搜索技巧及与Zotero的深度联动方法。通过智能标签系统和知识图谱功能,科研人员可以快速定位关键文献,提升研究效率。特别适合需要管理大量学术文献的研究者。

weixin_30823001的博客 274

MySQL物理查询优化技术---index dive辨析

一 引子在MySQL的官方手册上有这么一句话:the optimizer can estimate the row count for each range using dives into the index or index statistics.这是在说: 优化器为每一个范围段(如“a IN (10, 20, 30)”是等值比较, 括3个范围段实则简化为3个单值,分别是...

weixin_34220963的博客 367

vivado中出现ERROR: [Labtools 27-2220]检测不出开发板

这个错误可能是由于 Vivado 工具中的 cs_server.exe 文件没有正确安装或者文件路径不正确导致的。你可以尝试以下步骤来。时间: 2023-08-16 12:05:50 浏览: 767。希望以上方法能够帮助你。

qq_40510975的博客 2396

网络发生环路了,别慌,这些步骤一定要安排起来!

网络环路是指在计算机网络中,数据包因为某种原因在多个网络设备之间不断循环,形成了一个闭环。这样数据包就永远也无法到达目标,导致网络的流量滞留,设备间的资源耗尽,最终网络性能受到严重影响。网络环路虽然是常见的网络故障,但只要掌握正确的排查和解决方法,面对环路问题时,你就能冷静应对,迅速恢复网络的正常运行。希望这篇文章能帮助你更好地理解和处理网络环路的问题,让你的网络环境更加稳定可靠!🚀📡如果你有任何疑问,或者在实际操作中遇到困难,随时可以联系我,我们一起攻克技术难题!

网络技术联盟站 1978

mysql中eq_range_index_dive_limit参数学习

参考:https://www.phpmianshi.com/?id=137 概念 官方文档如下描述: This variable indicates the number of equality ranges in an equality comparison condition when the optimizer should switch from using index dives to index statistics in estimating the number of qualifyin

tiancityycf的专栏 3457

SAP 系统和银行系统通过 SM2 加密算法进行集成

SM2 是基于椭圆曲线离散对数问题(ECDLP)的一种公钥密码算法。椭圆曲线加密的核心原理,是利用了一个数学难题:在椭圆曲线群上,已知曲线上两点的运算结果,逆向推导出这两个点的具体信息是非常困难的。这样的复杂度使得基于椭圆曲线的密码体制比传统的 RSA 更加难以破解。在 SM2 加密中,公钥和私钥的生成与传统的椭圆曲线加密类似。选择一个椭圆曲线和一个基点 P。使用私钥 d,计算公钥 P’ = d * P。私钥 d 保密,而公钥 P’ 可公开分发,用于加密数据。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1217

MySQL物理查询优化技术 —— Index Dive

一 引子 在MySQL的官方手册上有这么一句话: the optimizer can estimate the row count for each range using dives into the index or index statistics. 这是在说: 优化器为每一个范围段(如“a IN (10, 20, 30)”是等值比较, 括3个范围段实则简化为3个单值,分别是10,2...

永生只是一场幻梦 2767

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

关于 MySQL index dives 详细解读

文章未完工,码字中...... 1. 关于index dives MySQL 官方文档中给出的解释 Equality Range Optimization of Many-Valued Comparisons Consider these expressions, where col_name is an indexed column: col_name IN(val1, ..., valN) col_name = val1 OR ... OR col_name = valN Each e..

渔夫数据库笔记 958

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

SQL语句优化

性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化。 为了获得稳定的执行性能,SQL语句越简单越好。对复杂的SQL语句,要设法对之进行简化。 常见的简化规则如下:1)不要有超过5个以上的表连接(JOIN)2)考虑使用临时表或表变量存放中间结果。3)少用子查询4)视图嵌套不要过深,一般视图嵌套不要超过2个为宜。 连...

weixin_30247159的博客 258

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

index dive 导致同类sql执行计划不一致

1.版本 1)操作系统 cat /proc/version Linux version 3.10.0-1127.18.2.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) 2)mysql数据库版本 mysql> select version(); +-----------+ | version() | +-----------+ | 8.

渔夫数据库笔记 738

MySQL如何选择合适的索引-trace工具

在众多索引中,mysql优化器是如何选择那个索引的;利用Mysql trace工具分析优化器是如何做的选择

PingFanXianSheng的博客 983

MySQL--eq_range_index_dive_limit参数学习,MYSQL 5.6 5.7处理数据分布不均的问题

官方文档如下描述:This variable indicates the number of equality ranges in an equality comparison condition when the optimizer should switch from using index dives to index statistics in estimating the nu...

aecuhty88306453的博客 701

MYSQL索引+执行计划之成本计算

MYSQL执行计划之成本计算 对于MySQL5.6之前的版本来说,只能通过EXPLAIN语句查看到最后优化器决定使用的执行 计划,却无法知道它为什么做这个决策。在MySQL 5.6以及之后的版本中,MySQL提出了一个optimizer trace的功能,这个功能可以让我们方便的查看优化器生成执行计划的整个过程。 索引 在计算成本之前,我们需要先了解MYSQL的索引结构以及索引命中规则。 索引是什么? 索引是帮助MySQL高效获取数据的排好序的数据结构 数据结构 B-tree B-Tree即B树,是一种多路

TianH_legend的博客 3394

【Oracle】AWR analyse

AWR analyse--SQL ordered by Elapsed Time记录了执行总和时间的TOP SQL(请注意是监控范围内该SQL的执行时间总和,而不是单次SQL执行时间 Elapsed Time = CPU Tim...

cojm55771的博客 318

关于极客时间 | MySQL实战45讲的部分总结

MySQL为什么有时候会选错索引 1.MySQL选择索引的依据 概述 mysql选择索引无非就是优化器觉得哪一种索引的成本执行低,优化器会从该索引的扫描行数、是否会用到临时表及是否要进行排序来进行综合判断 1.1 基于主键的成本计算 概述 对于主键来说就是计算全表扫描的成本,取决于下面两个方面 聚簇索引占用的页面数 该表中的记录数 该表中的记录数 对于这个数据的获取,mysql使用的是统计数据中的估计值,每个表都会有一些列的统计信息,本选项表示表中的记录条数。对于使用 MyISAM

光看不点赞的博客 4529

SQL优化分析学习

本文介绍了四种SQL分析指令,重点解析了STATISTICS PROFILE和SHOWPLAN_XML两种方式。STATISTICS PROFILE通过PhysicalOp字段展示实际执行方式,如哈希匹配、索引扫描等,并对比了不同操作的性能特点。SHOWPLAN_XML则以图形化方式直观显示资源占用情况。优化方向包括减少查询行数、避免全表扫描、优化索引等。两种方法数据一致但各有侧重,共同帮助提升查询效率。

yqsy123123的博客 721

MySQL优化器如何预估查询成本

MySQL有哪些查询成本 MySQL 执行一个查询可以有不同的执行方案。在我们开发过程中,所有写过的sql语句都会丢给MySQL端的优化器。由优化器判断并选择其中成本最低,或者说代价最低的那种方案去真正的执行查询。 不过我们之前对成本的描述是非常模糊的,其实在 MySQL优化器去考察一条sql执行成本是由下边这两个方面。 I/O 成本 我们的表经常使用的 MyISAM、InnoDB 存储引擎都是将数据和索引都存储到磁盘上的,当我们想查询表中的记录时,需要先把数据或者索引加载到内存中然后再操作。这个从

weixin_47184173的博客 1579

SQL Server 的 Statistics 簡介

當你要清空「資料表(table)」,或倒入大量「資料(data;record)」,或公司「資料庫(database)」改用新版本要資料大搬家…等情形,不只是要重建「索引(index)」,還應要重建或更新「統計(statistics)」。「統計」的正確與否,直接牽動 SQL Server 的「效能(performance)」。沒有「統計」固然不好,「統計」若過時,會讓最佳化程式做出錯誤的決定則更糟。...

weixin_34203832的博客 336

mysql 相关内容

mysql 主从同步原理 1)、复制类型 1.1)、SBR(STATEMENT)模式 基于语句,主库将每一条会修改的sql语句记录到 binlog 中, 优点:不需要记录每一条sql和每一行数据变化,减少 binlog 日志量,节约IO,提高性能 缺点:某些情况下会导致 master 和 slave 的数据不一致(如 sleep()、last_insert_id()、user_defin...

大龙的博客 595
上一篇: 使用 gt-checksum 迁移表结构到 GreatSQL
下一篇: GreatSQL temp文件占用时长分析
GreatSQL社区
博客等级 码龄5年 1210粉丝 390原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址