<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post/661/</link>
<title><![CDATA[达梦数据库主备集群、数据守护搭建]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Thu, 21 Dec 2023 10:58:20 +0000</pubDate> 
<guid>https://www.heckjj.com/post/661/</guid> 
<description>
<![CDATA[ 
	什么是实时主备？<br/>&nbsp;&nbsp;实时主备由一个主库以及一个或者多个配置了实时（Realtime）归档的备库组成，其主要目的是保障数据库可用性，提高数据安全性。实时主备系统中，主库提供完整的数据库功能，备库提供只读服务。主库修改数据产生的Redo日志，通过实时归档机制，在写入联机Redo日志文件之前发送到备库，实时备库通过重演Redo日志与主库保持数据同步。当主库出现故障时，备库在将所有Redo日志重演结束后，就可以切换为主库对外提供数据库服务<br/>1、环境准备<br/>&nbsp;&nbsp;服务器 主库IP:192.168.90.41&nbsp;&nbsp; dm8数据库 实例名 ：RAC1 端口号：5236<br/>&nbsp;&nbsp;服务器 备库IP:192.168.90.42&nbsp;&nbsp; dm8数据库 实例名 ：RAC2 端口号：5236<br/>&nbsp;&nbsp;# 数据库启动服务命令路径/dm8/bin，实例配置文件路径/dm8/data/DAMENG/<br/>&nbsp;&nbsp;<br/>2、主备库分别初始化实例（按客户要求）<br/>&nbsp;&nbsp;./dminit path=/dm8/data page_size=32 instance_name=RAC1<br/>&nbsp;&nbsp;./dminit path=/dm8/data page_size=32 instance_name=RAC2<br/><br/>3 备份还原<br/>&nbsp;&nbsp;# 如果是初始搭建环境，可以通过对主库脱机备份、对备库脱机还原的方式来准备数据， 如果主库已经处于运行状态，则可以对主库进行联机备份、对备库脱机还原的方式来准备数据。<br/><br/>3.1 主库创建实例之后，启动数据库并登录<br/>&nbsp;&nbsp;./dmserver /dm8/data/DAMENG/dm.ini<br/><br/>3.2 关闭数据库，以dmrman备份数据库<br/>&nbsp;&nbsp;BACKUP DATABASE &#039;/dm8/data/DAMENG/dm.ini&#039; BACKUPSET &#039;/dm8/data/backup&#039;;<br/><br/>3.3 将备份文件复制到备库对应目录下（如果是root用户，复制成功需要将备份文件设置属于dmdba:dinstall用户组）<br/>&nbsp;&nbsp;scp -r /dm8/data/backup dmdba@192.168.90.42:/dm8/data/backup<br/><br/>3.4 备库启动dmrman执行数据库还原<br/>&nbsp;&nbsp;RESTORE DATABASE &#039;/dm8/data/DAMENG/dm.ini&#039; FROM BACKUPSET &#039;/dm8/data/backup&#039;;<br/>&nbsp;&nbsp;RECOVER DATABASE &#039;/dm8/data/DAMENG/dm.ini&#039; FROM BACKUPSET &#039;/dm8/data/backup&#039;;<br/>&nbsp;&nbsp;RECOVER DATABASE &#039;/dm8/data/DAMENG/dm.ini&#039; UPDATE DB_MAGIC;<br/><br/>4 配置集群文件<br/>4.1&nbsp;&nbsp;vim dm.ini&nbsp;&nbsp;&nbsp;&nbsp;#主备库实例都需更改下面参数<br/>&nbsp;&nbsp;ALTER_MODE_STATUS = 0 #不允许手工方式修改实例模式/状态/OGUID<br/>&nbsp;&nbsp;ENABLE_OFFLINE_TS = 2 #不允许备库 OFFLINE 表空间<br/>&nbsp;&nbsp;MAL_INI = 1 #打开 MAL 系统<br/>&nbsp;&nbsp;ARCH_INI = 1 #打开归档配置<br/><br/>4.2&nbsp;&nbsp;vim dmmal.ini&nbsp;&nbsp;#主备库配置必须完全一致<br/>&nbsp;&nbsp;MAL_CHECK_INTERVAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#MAL 链路检测时间间隔<br/>&nbsp;&nbsp;MAL_CONN_FAIL_INTERVAL&nbsp;&nbsp; = 5&nbsp;&nbsp;#判定 MAL 链路断开的时间<br/>&nbsp;&nbsp;[MAL_INST1]<br/>&nbsp;&nbsp;MAL_INST_NAME&nbsp;&nbsp;&nbsp;&nbsp;= RAC1 #实例名，和 dm.ini 中的 INSTANCE_NAME 一致<br/>&nbsp;&nbsp;MAL_HOST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 192.168.90.41 #MAL 系统监听 TCP 连接的 IP 地址<br/>&nbsp;&nbsp;MAL_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 61141&nbsp;&nbsp;&nbsp;&nbsp;#MAL 系统监听 TCP 连接的端口<br/>&nbsp;&nbsp;MAL_INST_HOST&nbsp;&nbsp;&nbsp;&nbsp;= 192.168.90.41 #实例的对外服务 IP 地址<br/>&nbsp;&nbsp;MAL_INST_PORT&nbsp;&nbsp;&nbsp;&nbsp;= 5236 #实例的对外服务端口，dm.ini 中的 PORT_NUM 一致<br/>&nbsp;&nbsp;MAL_DW_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 52141 #实例对应的守护进程监听 TCP 连接的端口<br/>&nbsp;&nbsp;MAL_INST_DW_PORT&nbsp;&nbsp;&nbsp;&nbsp;= 33141 #实例监听守护进程 TCP 连接的端口<br/>&nbsp;&nbsp;[MAL_INST2]<br/>&nbsp;&nbsp;MAL_INST_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = RAC2<br/>&nbsp;&nbsp;MAL_HOST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 192.168.90.42<br/>&nbsp;&nbsp;MAL_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 61141<br/>&nbsp;&nbsp;MAL_INST_HOST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 192.168.90.42<br/>&nbsp;&nbsp;MAL_INST_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 5236<br/>&nbsp;&nbsp;MAL_DW_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 52141<br/>&nbsp;&nbsp;MAL_INST_DW_PORT&nbsp;&nbsp;&nbsp;&nbsp;= 33141<br/><br/>4.3 vim dmarch.ini #主备库归档目标实例名不一致，其他一致<br/>&nbsp;&nbsp;#与联机 Redo 日志文件可以被覆盖重用不同，本地归档日志文件不能被覆盖，写入其中Redo 日志信息会一直保留，直到用户主动删除；如果配置了归档日志空间上限，系统会自动删除最早生成的归档 Redo 日志文件，腾出空间。如果磁盘空间不足，且没有配置归档日志空间上限（或者配置的上限超过实际空间），系统将自动挂起，直到用户主动释放出足够的空间后继续运行。配置如下:<br/>&nbsp;&nbsp;[ARCHIVE_REALTIME]<br/>&nbsp;&nbsp;ARCH_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;REALTIME #实时归档类型<br/>&nbsp;&nbsp;ARCH_DEST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= RAC2 #实时归档目标实例名<br/>&nbsp;&nbsp;[ARCHIVE_LOCAL1]<br/>&nbsp;&nbsp;ARCH_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= LOCAL #本地归档类型<br/>&nbsp;&nbsp;ARCH_DEST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= /dm8/data/DAMENG/arch #本地归档文件存放路径<br/>&nbsp;&nbsp;ARCH_FILE_SIZE&nbsp;&nbsp; = 128 #单位 Mb，本地单个归档文件最大值<br/>&nbsp;&nbsp;ARCH_SPACE_LIMIT = 500000 #单位 Mb，0 表示无限制，范围 1024~4294967294M<br/><br/>4.4 配置dmwatcher.ini #主备库配置一致<br/>&nbsp;&nbsp;[GRP1]<br/>&nbsp;&nbsp;DW_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp;&nbsp;GLOBAL #全局守护类型<br/>&nbsp;&nbsp;DW_MODE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp;&nbsp;MANUAL # 手工切换 AUTO自动切换模式必须部署一个确认监视器<br/>&nbsp;&nbsp;DW_ERROR_TIME&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;10 #远程守护进程故障认定时间<br/>&nbsp;&nbsp;INST_RECOVER_TIME =&nbsp;&nbsp;60 #主库守护进程启动恢复的间隔时间<br/>&nbsp;&nbsp;INST_ERROR_TIME&nbsp;&nbsp;=&nbsp;&nbsp;10 #本地实例故障认定时间<br/>&nbsp;&nbsp;INST_OGUID&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp;&nbsp;453332 #守护系统唯一 OGUID 值<br/>&nbsp;&nbsp;INST_INI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;/dm8/data/DAMENG/dm.ini #dm.ini配置文件路径<br/>&nbsp;&nbsp;INST_AUTO_RESTART =&nbsp;&nbsp;1 #打开实例的自动启动功能<br/>&nbsp;&nbsp;INST_STARTUP_CMD&nbsp;&nbsp;=&nbsp;&nbsp;/dm8/bin/dmserver #命令行方式启动<br/>&nbsp;&nbsp;RLOG_SEND_THRESHOLD =&nbsp;&nbsp;0 #指定主库发送日志到备库的时间阀值，默认关闭<br/>&nbsp;&nbsp;RLOG_APPLY_THRESHOLD =&nbsp;&nbsp;0 #指定备库重演日志的时间阀值，默认关闭<br/>&nbsp;&nbsp;4.5 vim dmmonitor.ini #生产环境如果是自动切换需单独一台服务器配置监视器<br/>&nbsp;&nbsp;MON_DW_Confirm&nbsp;&nbsp;&nbsp;&nbsp;= 0&nbsp;&nbsp;# 0表示普通监视器 ，最多配置8个，1 确认监视器模式<br/>&nbsp;&nbsp;MON_LOG_PATH&nbsp;&nbsp;&nbsp;&nbsp;= /dm8/data/log #监视器日志文件存放路径<br/>&nbsp;&nbsp;MON_LOG_INTERVAL&nbsp;&nbsp;= 60 #每隔 60s 定时记录系统信息到日志文件<br/>&nbsp;&nbsp;MON_LOG_FILE_SIZE&nbsp;&nbsp; = 32 #每个日志文件最大 32M<br/>&nbsp;&nbsp;MON_LOG_SPACE_LIMIT&nbsp;&nbsp;= 0&nbsp;&nbsp;#不限定日志文件总占用空间<br/>&nbsp;&nbsp;[GRP1]<br/>&nbsp;&nbsp;MON_INST_OGUID&nbsp;&nbsp;&nbsp;&nbsp;= 453332 #组 GRP1 的唯一OGUID 值<br/>&nbsp;&nbsp;#配置为监视器到组GRP1的守护进程的连接信息以―IP:PORT‖的形式配置<br/>&nbsp;&nbsp;#IP对应dmmal.ini中的 MAL_HOST，PORT 对应 dmmal.ini 中的 MAL_DW_PORT<br/>&nbsp;&nbsp;MON_DW_IP&nbsp;&nbsp;&nbsp;&nbsp; = 192.168.90.41:52141<br/>&nbsp;&nbsp;MON_DW_IP&nbsp;&nbsp;&nbsp;&nbsp; = 192.168.90.42:52141<br/><br/>5 以 Mount方式启动主备库<br/>&nbsp;&nbsp;./dmserver /dm8/data/DAMENG/dm.ini mount<br/>&nbsp;&nbsp;#一定要以 Mount 方式启动数据库实例，否则系统启动时会重构回滚表空间，生成 Redo 日志；启动后应用可能连接到数据库实例进行操作，破坏主备库的数据一致性。数据守护配置结束后，守护进程会自动 Open 数据库<br/><br/>6 启动命令行工具 disql，登录主备库设置 OGUID 值和数据库模式。<br/>6.1&nbsp;&nbsp;主库<br/>&nbsp;&nbsp;sp_set_oguid(453332);<br/>&nbsp;&nbsp;alter database primary;<br/><br/>6.2 备库<br/>&nbsp;&nbsp;sp_set_oguid(453332);<br/>&nbsp;&nbsp;alter database standby;<br/><br/>7 启动各个主备库上的守护进程<br/>&nbsp;&nbsp;./dmwatcher /dm8/data/DAMENG/dmwatcher.ini<br/><br/>8 启动监视器<br/>&nbsp;&nbsp;./dmmonitor /dm8/data/DAMENG/dmmonitor.ini<br/>&nbsp;&nbsp;监视器提供一系列命令，支持当前守护系统状态查看以及故障处理，可输入help 命令查看各种命令说明使用，结合实际情况选择使用。至此一主一备的实时数据守护系统搭建完毕，在搭建步骤和各项配置都正确的情况下， 在监视器上执行 show 命令，可以监控到所有实例都处于 Open 状态，所有守护进程也都处于Open 状态，即为正常运行状态。<br/><br/>9 创建服务（root用户，安装目录script/root下，输入./dm_service_installer.sh -h查看帮助，过程略）<br/>&nbsp;&nbsp;#如果是自动切换，一定要创建监视器服务并启动<br/>&nbsp;&nbsp;创建成功之后，由于前面已经前台方式启动，以服务方式启动会报错，需要将前台服务关闭。关闭顺序如下<br/>&nbsp;&nbsp;1. 如果启动了确认监视器，先关闭确认监视器（防止自动接管）<br/>&nbsp;&nbsp;2. 关闭备库守护进程（防止重启实例）<br/>&nbsp;&nbsp;3. 关闭主库守护进程（防止重启实例）<br/>&nbsp;&nbsp;4.先关主库，后关备库（主库 Shutdown 过程中，需要 Purge 所有已提交事务，会修改数据，并产生 Redo 日志。如果先 Shutdown 备库，会导致主库发送归档日志失败，并且由于主库已经处于 Shutdown 状态，会导致主库异常关闭）<br/><br/>10&nbsp;&nbsp;服务名配置<br/>&nbsp;&nbsp;配置 DM 数据守护，一般要求配置连接服务名，以实现故障自动重连。连接服务名可以在 DM 提供的 JDBC、DPI 等接口中使用，连接数据库时指定连接服务名，接口会随机选择一个 IP 进行连接，如果连接不成功或者服务器状态不正确，则顺序获取下一个 IP 进行连接，直至连接成功或者遍历了所有 IP。可以通过编辑 dm_svc.conf 文件配置连接服务名。<br/>&nbsp;&nbsp;dm_svc.conf 配置文件在 DM 安装时生成，Windows 平台下位于%SystemRoot%&#92;system32 目录，Linux 平台下位于/etc 目录,详细配置见系统管理员手册<br/><br/>10.1 vim dm_svc.conf<br/><br/>TIME_ZONE=(480)<br/>LANGUAGE=(cn)<br/>## 添加如下内容 HECK_DM服务名中填写主备机器的IP跟端口号<br/>HECK_DM=(192.168.90.41:5236,192.168.90.42:5236)<br/>[HECK_DM]<br/>LOGIN_MODE=(1)<br/>SWITCH_TIME=(3)<br/>SWITCH_INTERVAL=(100)<br/><br/>&nbsp;&nbsp;JDBC连接串写法：#具体参考程序员手册<br/>&nbsp;&nbsp;使用连接串：jdbc:dm://ip:port<br/>&nbsp;&nbsp;使用服务名：jdbc:dm://服务名<br/>&nbsp;&nbsp;类名为：dm.jdbc.driver.DmDriver<br/><br/>10.2 可直接字符串连接，无需在每个dm_svc.conf文件配置。<br/>&nbsp;&nbsp;主备示例： url=&quot;jdbc:dm://dmconn?dmconn=(192.168.90.41:5236,192.168.90.42:5236)&amp;LOGIN_MODE=(1)&amp;switch_time=(3)&amp;switch_interval(100)&quot;;
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/635/</link>
<title><![CDATA[Navicat Premium 连接sqlserver 提示Client安装失败]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 07 Dec 2022 10:01:56 +0000</pubDate> 
<guid>https://www.heckjj.com/post/635/</guid> 
<description>
<![CDATA[ 
	<a href="https://www.heckjj.com/attachment.php?fid=249" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=249" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="292"/></a><br/>使用Navicat连接SQLserver时，具体报错信息如下：<br/><br/>Installation of this product failed because it is not <br/>supported on this operating system. For information on supported configurations,<br/>see the product documentation.<br/> <br/><a href="https://www.heckjj.com/attachment.php?fid=250" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=250" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>解决方法： <br/>这个要安装一个叫做sqlncli_x64.msi的东西，手动安装，它在你的Navicat的安装目录下面，比如我的在：<br/><br/>D:&#92;Program Files&#92;PremiumSoft&#92;Navicat Premium<br/><br/><a href="https://www.heckjj.com/attachment.php?fid=251" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=251" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="450"/></a><br/><br/>如果你的电脑是64位的，就运行安装那个64位的（我电脑是64位，就只标记出了64位的），如果是32 的就运行那个sqlncli.msi。<br/><br/>然后再进行Sqlserver的链接就不再提示安装出错了。 <br/>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/622/</link>
<title><![CDATA[MYSQL的监控方式]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 23 Nov 2022 11:45:10 +0000</pubDate> 
<guid>https://www.heckjj.com/post/622/</guid> 
<description>
<![CDATA[ 
	<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important">对于当前数据库的监控方式有很多，分为数据库自带、商用、开源三大类，每一种都有各自的特色；而对于 mysql 数据库由于其有很高的社区活跃度，监控方式更是多种多样，不管哪种监控方式最核心的就是监控数据，获取得到全面的监控数据后就是灵活的展示部分。那我们今天就介绍一下完全采用 mysql 自有方式采集获取监控数据，在单体下达到最快速、方便、损耗最小。</span><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important">本次文章完全使用 mysql 自带的 show 命令实现获取，从 connects、buffercache、lock、SQL、statement、Database throughputs、serverconfig7 大方面全面获取监控数据。</span><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; visibility: visible; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important">1 连接数（Connects）</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; visibility: visible; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; visibility: visible; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important">最大使用连接数：show status like &lsquo;Max_used_connections&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; visibility: visible; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important">当前打开的连接数：show status like &lsquo;Threads_connected&rsquo;</span></p></li></ul><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">2 缓存（bufferCache）</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">未从缓冲池读取的次数：show status like &lsquo;Innodb_buffer_pool_reads&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">从缓冲池读取的次数：show status like &lsquo;Innodb_buffer_pool_read_requests&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">缓冲池的总页数：show status like &lsquo;Innodb_buffer_pool_pages_total&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">缓冲池空闲的页数：show status like &lsquo;Innodb_buffer_pool_pages_free&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">缓存命中率计算：（1-Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests）*100%</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">缓存池使用率为：((Innodb_buffer_pool_pages_total-Innodb_buffer_pool_pages_free）/Innodb_buffer_pool_pages_total）*100%</span></p></li></ul><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">3 锁（lock）</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">锁等待个数：show status like &lsquo;Innodb_row_lock_waits&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">平均每次锁等待时间：show status like &lsquo;Innodb_row_lock_time_avg&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">查看是否存在表锁：show open TABLES where in_use&gt;0；有数据代表存在锁表，空为无表锁</span></p></li></ul><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">备注：锁等待统计得数量为累加数据，每次获取得时候可以跟之前得数据进行相减，得到当前统计得数据</span><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">4 SQL</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">查看 mysql 开关是否打开：show variables like &lsquo;slow_query_log&rsquo;，ON 为开启状态，如果为 OFF，set global slow_query_log=1 进行开启</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">查看 mysql 阈值：show variables like &lsquo;long_query_time&rsquo;，根据页面传递阈值参数，修改阈值 set global long_query_time=0.1</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">查看 mysql 慢 sql 目录：show variables like &lsquo;slow_query_log_file&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">格式化慢 sql 日志：mysqldumpslow -s at -t 10 /export/data/mysql/log/slow.log<br />注：此语句通过 jdbc 执行不了，属于命令行执行。<br />意思为：显示出耗时最长的 10 个 SQL 语句执行信息，10 可以修改为 TOP 个数。显示的信息为：执行次数、平均执行时间、SQL 语句</span></p></li></ul><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">备注：当 mysqldumpslow 命令执行失败时，将慢日志同步到本地进行格式化处理。</span><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">5 statement</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">insert 数量：show status like &lsquo;Com_insert&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">delete 数量：show status like &lsquo;Com_delete&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">update 数量：show status like &lsquo;Com_update&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">select 数量：show status like &lsquo;Com_select&rsquo;</span></p></li></ul><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">6 吞吐（Database throughputs）</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">发送吞吐量：show status like &lsquo;Bytes_sent&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">接收吞吐量：show status like &lsquo;Bytes_received&rsquo;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">总吞吐量：Bytes_sent+Bytes_received</span></p></li></ul><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">7 数据库参数（serverconfig）</span></strong></span></h3><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">show variables</span><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">8 慢 SQL</span></strong></span></h3><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">慢 SQL 指的是 MySQL 慢查询，具体指运行时间超过 long_query_time 值的 SQL。<br />我们常听 MySQL 中有二进制日志 binlog、中继日志 relaylog、重做回滚日志 redolog、undolog 等。针对慢查询，还有一种慢查询日志 slowlog，用来记录在 MySQL 中响应时间超过阀值的语句。慢 SQL 对实际生产业务影响是致命的，所以测试人员在性能测试过程中，对数据库 SQL 语句执行情况实施监控，给开发提供准确的性能优化意见显得尤为重要。那怎么使用 Mysql 数据库提供的慢查询日志来监控 SQL 语句执行情况，找到消耗较高的 SQL 语句，以下详细说明一下慢查询日志的使用步骤：</span><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">确保打开慢 SQL 开关 slow_query_log</span></p></li></ul><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">设置慢 SQL 域值 long_query_time<br />这个 long_query_time 是用来定义慢于多少秒的才算 &ldquo;慢查询&rdquo;，注意单位是秒，我通过执行 sql 指令 set long_query_time=1 来设置了 long_query_time 的值为 1, 也就是执行时间超过 1 秒的都算慢查询。</span></p></li></ul><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">查看慢 SQL 日志路径</span></p></li></ul><br /><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">通过慢 sql 分析工具 mysqldumpslow 格式化分析慢 SQL 日志<br />mysqldumpslow 慢查询分析工具，是 mysql 安装后自带的，可以通过./mysqldumpslow &mdash;help 查看使用参数说明</span></p></li></ul><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">常见用法：</span><ol class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">取出使用最多的 10 条慢查询<br />./mysqldumpslow -s c -t 10 /export/data/mysql/log/slow.log</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">取出查询时间最慢的 3 条慢查询<br />./mysqldumpslow -s t -t 3 /export/data/mysql/log/slow.log</span></p></li></ol><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">注意：使用 mysqldumpslow 的分析结果不会显示具体完整的 sql 语句，只会显示 sql 的组成结构；<br />假如: SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span><em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">FROM sms_send WHERE service_id=10 GROUP BY content LIMIT 0, 1000;<br />mysqldumpslow 命令执行后显示：<br />Count: 2 Time=1.5s (3s) Lock=0.00s (0s) Rows=1000.0 (2000), vgos_dba[vgos_dba]@[10.130.229.196]SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span></em>FROM sms_send WHERE service_id=N GROUP BY content LIMIT N, N</span><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; font-weight: 700; overflow-wrap: break-word !important">mysqldumpslow 的分析结果详解：</span></span><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">Count：表示该类型的语句执行次数，上图中表示 select 语句执行了 2 次。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">Time：表示该类型的语句执行的平均时间（总计时间）</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">Lock：锁时间 0s。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">Rows：单次返回的结果数是 1000 条记录，2 次总共返回 2000 条记录。</span></p></li></ul><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important">通过这个工具就可以查询出来哪些 sql 语句是慢 SQL，从而反馈研发进行优化，比如加索引，该应用的实现方式等。</span><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 1px; font-size: 15px; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; font-weight: 700; overflow-wrap: break-word !important">常见慢 SQL 排查</span></span><ol class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">不使用子查询<br />SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span><em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">FROM t1 WHERE id (SELECT id FROM t2 WHERE name=&rsquo;hechunyang&rsquo;);<br />子查询在 MySQL5.5 版本里，内部执行计划器是这样执行的：先查外表再匹配内表，而不是先查内表 t2，当外表的数据很大时，查询速度会非常慢。<br />在 MariaDB10/MySQL5.6 版本里，采用 join 关联方式对其进行了优化，这条 SQL 会自动转换为 SELECT t1.</em><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span>FROM t1 JOIN t2 ON t1.id = t2.id;<br />但请注意的是：优化只针对 SELECT 有效，对 UPDATE/DELETE 子 查询无效， 生产环境尽量应避免使用子查询。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">避免函数索引<br />SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span><em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">FROM t WHERE YEAR(d) &gt;= 2016;<br />由于 MySQL 不像 Oracle 那样⽀持函数索引，即使 d 字段有索引，也会直接全表扫描。<br />应改为 &gt; SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span></em>FROM t WHERE d &gt;= &lsquo;2016-01-01&rsquo;;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">用 IN 来替换 OR 低效查询<br />慢 SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span><em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">FROM t WHERE LOC_ID = 10 OR LOC_ID = 20 OR LOC_ID = 30;<br />高效查询 &gt; SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span></em>FROM t WHERE LOC_IN IN (10,20,30);</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">LIKE 双百分号无法使用到索引<br />SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span><em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">FROM t WHERE name LIKE &lsquo;%de%&rsquo;;<br />使用 SELECT<span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">&nbsp;</span></em>FROM t WHERE name LIKE &lsquo;de%&rsquo;;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">分组统计可以禁止排序<br />SELECT goods_id,count(<em style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; overflow-wrap: break-word !important">) FROM t GROUP BY goods_id;<br />默认情况下，MySQL 对所有 GROUP BY col1，col2&hellip; 的字段进⾏排序。如果查询包括 GROUP BY，想要避免排序结果的消耗，则可以指定 ORDER BY NULL 禁止排序。<br />使用 SELECT goods_id,count (</em>) FROM t GROUP BY goods_id ORDER BY NULL;</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">禁止不必要的 ORDER BY 排序<br />SELECT count(1) FROM user u LEFT JOIN user_info i ON u.id = i.user_id WHERE 1 = 1 ORDER BY u.create_time DESC;<br />使用 SELECT count (1) FROM user u LEFT JOIN user_info i ON u.id = i.user_id;</span></p></li></ol><h3 style="margin: 22px 8px 16px; padding: 0px; outline: 0px; font-size: 22px; max-width: 100%; box-sizing: inherit; text-align: justify; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; border-width: initial; border-style: none; border-color: initial; color: #333333; line-height: 1.75em; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #31859b"><strong style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; font-size: 18px; box-sizing: border-box !important; overflow-wrap: break-word !important">9 总结</span></strong></span></h3><ul class="list-paddingleft-1" style="margin: 0px 0px 20px; padding: 0px 0px 0px 20px; outline: 0px; max-width: 100%; box-sizing: inherit; color: #333333; font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; overflow-wrap: break-word !important"><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">任何东西不应过重关注其外表，要注重内在的东西，往往绚丽的外表下会有对应的负担和损耗。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">mysql 数据库的监控支持通过 SQL 方式从 performance_schema 库中访问对应的表数据，前提是初始化此库并开启监控数据写入。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">对于监控而言，不在于手段的多样性，而需要明白监控的本质，以及需要的监控项内容，找到符合自身项目特色的监控方式。</span></p></li><li style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: inherit; line-height: 1.875em; letter-spacing: 1px; font-size: 15px; overflow-wrap: break-word !important"><p style="margin: 0px 0px 16px; padding: 0px; outline: 0px; max-width: 100%; clear: both; min-height: 1em; text-align: justify; line-height: 1.75em; box-sizing: border-box !important; overflow-wrap: break-word !important"><span style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important">在选择监控工具对 mysql 监控时，需要关注监控工具本身对于数据库服务器的消耗，不要影响到其自身的使用。</span></p></li></ul>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/619/</link>
<title><![CDATA[navicat连接Oracle数据库报错ORA-28547: connection to server failed, probable Oracle Net admin error]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Fri, 18 Nov 2022 02:46:11 +0000</pubDate> 
<guid>https://www.heckjj.com/post/619/</guid> 
<description>
<![CDATA[ 
	<a href="https://www.heckjj.com/attachment.php?fid=247" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=247" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>问题产生的原因：<br/>由于我远程的Oracle是 11g的，但是Navicat本身的Oracle oci.dll文件是10g的，数据库与链接库的版本不一致，所以会报错。<br/><br/>打开navicat的安装目录，查看有没有以下文件：<br/><a href="https://www.heckjj.com/attachment.php?fid=246" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=246" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>解决方案：<br/>1、我们去Oracle官网下载对应的Instant Client Package -Version 11.2.0.4.0 - Basic的文件即可<br/><br/>下载地址：<a href="https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html#license-lightbox" target="_blank">Instant Client for Microsoft Windows (x64) 64-bit</a><br/>或者自行网上搜索下载<br/> 下载好之后，解压到navicat安装目录下<br/><br/>2、启动navicat客户端 找到【工具】-&gt;【选项】-&gt;【环境】<br/> 将OCI环境更改为11_2下的oci.dll即可，重启之后即可生效！<br/><a href="https://www.heckjj.com/attachment.php?fid=245" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=245" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/602/</link>
<title><![CDATA[将excel数据通过navicat导入mysql并生成uuid]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 06 Jul 2022 09:05:36 +0000</pubDate> 
<guid>https://www.heckjj.com/post/602/</guid> 
<description>
<![CDATA[ 
	需求分析<br/><br/>1、将excel数据导入数据库中；<br/>2、利用uuid生成的32随机数作主键；<br/>3、利用CURRENT_TIMESTAMP()生成时间；<br/><br/>具体操作<br/><br/>1、根据需要创建表结构<br/>2、将excel数据导入刚创建的表中<br/>注意： 为了能够将excel中的数据导入数据库中，创建数据库表时，暂时将主键去掉，只保留主键字段。<br/>3、在id中生成uuid<br/><br/>UPDATE 表名 SET 列名= UUID();<br/><br/>注意：生成id时，不要直接将uuid()产生的序列中的’’-“通过Replace方法去掉，不然生成的id的将全部相同。<br/>4、将生成的id中的”-"替换掉<br/><br/>UPDATE 表名 SET 列名 = (SELECT REPLACE(对应列名,'-',''));<br/><br/>5、生成时间<br/><br/>UPDATE 表名 SET 列名1 = CURRENT_TIMESTAMP();<br/>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/584/</link>
<title><![CDATA[MySQL分区建索引和唯一索引]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 19 Jan 2022 06:22:55 +0000</pubDate> 
<guid>https://www.heckjj.com/post/584/</guid> 
<description>
<![CDATA[ 
	mysql分区后每个分区成了独立的文件，虽然从逻辑上还是一张表其实已经分成了多张独立的表，从“information_schema.INNODB_SYS_TABLES”系统表可以看到每个分区都存在独立的TABLE_ID,由于Innodb数据和索引都是保存在&quot;.ibd&quot;文件当中（从INNODB_SYS_INDEXES系统表中也可以得到每个索引都是对应各自的分区(primary key和unique也不例外）），所以分区表的索引也是随着各个分区单独存储。<br/><br/>在INNODB_SYS_INDEXES系统表中type代表索引的类型;<br/><br/>0:一般的索引,<br/><br/>1:(GEN_CLUST_INDEX)不存在主键索引的表,会自动生成一个6个字节的标示值，<br/><br/>2:unique索引,<br/><br/>3:primary索引;<br/><br/>所以当我们在分区表中创建索引时其实也是在每个分区中创建索引，每个分区维护各自的索引（其实也就是local index）；对于一般的索引(非主键或者唯一)没什么问题由于索引树中只保留了索引key和主键key(如果存在主键则是主键的key否则就是系统自动生成的6个的key)不受分区的影响；但是如果表中存在主键就不一样了，虽然在每个分区文件中都存在主键索引但是主键索引需要保证全局的唯一性就是所有分区中的主键的值都必须唯一（唯一键也是一样的道理），所以在创建分区时如果表中存在主键或者唯一键那么分区列必须包含主键或者唯一键的部分或者全部列（全部列还好理解，部分列也可以个人猜测是为了各个分区和主键建立关系），由于需要保证全局性又要保证插入数据更新数据到具体的分区所以就需要将分区和主键建立关系,由于通过一般的索引进行查找其它非索引字段需要通过主键如果主键不能保证全局唯一性的话那么就需要去每个分区查找了，这样性能可想而知。<br/><br/>To enforce the uniqueness we only allow mapping of each unique/primary key value to one partition.If we removed this limitation it would mean that for every insert/update we need to check in every partition to verify that it is unique. Also PK-only lookups would need to look into every partition.<br/> <br/>索引方式：<br/>性能依次降低<br/>1.主键分区<br/><br/>主键分区即字段是主键同时也是分区字段，性能最好<br/><br/>2. 部分主键+分区索引<br/><br/>使用组合主键里面的部分字段作为分区字段，同时将分区字段建索引（见下面详细说明）<br/><br/>3.分区索引<br/><br/>没有主键，只有分区字段且分区字段建索引<br/><br/>4.分区+分区字段没有索引<br/><br/>只建了分区，但是分区字段没有建索引<br/><br/>总结<br/><br/>因为每一个表都需要有主键这样可以减少很多锁的问题，由于上面讲过主键需要解决全局唯一性并且在插入和更新时可以不需要去扫描全部分区，造成主键和分区列必须存在关系；所以最好的分区效果是使用主键作为分区字段其次是使用部分主键作为分区字段且创建分区字段的索引，其它分区方式都建议不采取。<br/><br/><br/>MYSQL的分区字段，必须包含在主键字段内<br/>在对表进行分区时，如果分区字段没有包含在主键字段内，如表A的主键为ID,分区字段为createtime ，按时间范围分区，代码如下： <br/><br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL AUTO_INCREMENT,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRIMARY KEY (id)<br/>) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/>复制代码<br/>错误提示：#1503<br/><br/>MySQL主键的限制，每一个分区表中的公式中的列，必须在主键/unique key 中包括，在MYSQL的官方文档里是这么说明的<br/>18.5.1. Partitioning Keys, Primary Keys, and Unique Keys<br/>This section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have. <br/> <br/>In other words,every unique key on the table must use every columnin the table&#039;s partitioning expression. (This also includes the table&#039;s primary key, since it is by definition a unique key. This particular case is discussed later in this section.) For example, each of the following table creation statements is invalid: <br/>分区字段必须包含在主键字段内，至于为什么MYSQL会这样考虑，我觉得是这样的：为了确保主键的效率。否则同一主键区的东西一个在Ａ分区，一个在Ｂ分区，显然会比较麻烦。<br/> <br/>下面讨论解决办法，毕竟在一张表里，日期做主键的还是不常见。 <br/>方法1： <br/>顺应MYSQL的要求，就把分区字段加入到主键中，组成复合主键<br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL AUTO_INCREMENT,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRIMARY KEY (id,createtime)<br/>) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/> 测试通过，分区成功。<br/> <br/>方法2： <br/>既然MYSQL要把分区字段包含在主键内才能创建分区，那么在创建表的时候，先不指定主键字段，是否可以呢？？<br/>测试如下：<br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL ,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/>测试通过，分区成功。OK<br/>继续添加上主键<br/>alter table t1 add PRIMARY KEY(ID)<br/>错误1503，和前面一样的错误。<br/>alter table t1 add PRIMARY KEY(ID,createtime)<br/>创建主键成功，但还是复合主键，看来是没办法了，必须听指挥了。<br/>主键创建成功，把ID加上自增字段设置<br/>alter table t1 change id id int not null auto_increment;<br/>alter table t1 auto_increment=1;<br/> <br/>最后结论，MYSQL的分区字段，必须包含在主键字段内。 <br/> <br/>分区表中创建唯一索引：<br/>例如，按create_time进行月分区的表里，唯一索引可能是orderNo，按照上面的要求，唯一索引就成为(order_no,create_time)了。但这样不满足业务需求。<br/>解决办法：<br/>为分区表增加一个before insert触发器，在插入前查询下是否已存在即可。<br/><br/>CREATE TRIGGER `trig_insert_t_order` BEFORE INSERT ON `t_order` FOR EACH ROW BEGIN<br/>&nbsp;&nbsp;DECLARE v_count TINYINT UNSIGNED;<br/>&nbsp;&nbsp;DECLARE v_mess_str varchar(100);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;SELECT COUNT(1) INTO @v_count<br/>&nbsp;&nbsp;FROM t_order<br/>&nbsp;&nbsp;WHERE order_no = new.order_no<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and new.create_time&gt;=date_sub(SYSDATE(),INTERVAL 2 DAY);&nbsp;&nbsp;## 2天是否足够<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;IF (@v_count &gt; 0) THEN<br/>&nbsp;&nbsp;&nbsp;&nbsp; SELECT concat(&#039;Duplicate entry &#039;,new.order_no) INTO @v_mess_str;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIGNAL SQLSTATE &#039;23000&#039; SET MESSAGE_TEXT = @v_mess_str, MYSQL_ERRNO = 1022;<br/>&nbsp;&nbsp;END IF;<br/>&nbsp;&nbsp;<br/>END;
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/583/</link>
<title><![CDATA[mysql 删除分区但不删除数据]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Tue, 18 Jan 2022 08:41:47 +0000</pubDate> 
<guid>https://www.heckjj.com/post/583/</guid> 
<description>
<![CDATA[ 
	大家都知道删除分区的语句如下：<br/>ALTER TABLE &#039;表名&#039; DROP PARTITION &#039;分区名&#039; ; 但是这样的话只能一个个分区去删除，而且删除分区同时会把数据也给删除掉了，那么mysql删除表的所有分区如何操作呢？<br/>只要使用ALTER TABLE 表名 REMOVE PARTITIONING 就可以删除所有分区，但数据不会被删除。<br/><br/>查询指定表的分区信息以及每个分区的行数<br/>SELECT PARTITION_NAME,TABLE_ROWS<br/>FROM INFORMATION_SCHEMA.PARTITIONS<br/>WHERE TABLE_NAME = &#039;表名&#039;;<br/><br/>SELECT<br/>&nbsp;&nbsp;PARTITION_NAME,PARTITION_DESCRIPTION,<br/>FROM_UNIXTIME(PARTITION_DESCRIPTION,&#039;%Y-%m-%d&#039;) EXPIRYDATE,TABLE_ROWS<br/>FROM<br/>INFORMATION_SCHEMA.PARTITIONS where TABLE_NAME =&#039;表名&#039;;
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/582/</link>
<title><![CDATA[Mysql分区键和唯一索引主键的关系]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Tue, 18 Jan 2022 02:00:56 +0000</pubDate> 
<guid>https://www.heckjj.com/post/582/</guid> 
<description>
<![CDATA[ 
	今天主要介绍下面这几个错误:<br/><br/><span style="color: #DC143C;">ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table&#039;s partitioning</span><br/><br/><span style="color: #DC143C;">ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table&#039;s partitioning function</span><br/><br/>主键必须包括表的分区函数中的所有列，一个惟一的索引必须包括表的分区函数中的所有列，是不是不太好理解意思<br/><br/>其实就是这么个意思：表上的每一个唯一性索引必须用于分区表的表达式上（其中包括主键索引） <br/><br/>下面我来举几个例子：<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id)&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>[Err] 1503 - A PRIMARY KEY must include all columns in the table&#039;s partitioning function<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (id)&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>[Err] 1503 - A PRIMARY KEY must include all columns in the table&#039;s partitioning function<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id,bid)&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>Query OK, 0 rows affected (0.535 sec)<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (id,bid)&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>Query OK, 0 rows affected (0.625 sec)<br/><br/>这一组例子说明了如果要用bid作为分区key，则bid至少要包含在主键或者唯一键。<br/><br/>再来看一组例子：<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id,aid),<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (bid)&nbsp;&nbsp;&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>[Err] 1503 - A PRIMARY KEY must include all columns in the table&#039;s partitioning function<br/><br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id,aid),<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (bid)&nbsp;&nbsp;&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(aid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>[Err] 1503 - A PRIMARY KEY must include all columns in the table&#039;s partitioning function<br/><br/>这2个分区表是无法建立的，因为没有一个分区键，可以同时属于两个唯一性索引的键。<br/><br/>下面再来看一组例子：<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id,aid,bid)&nbsp;&nbsp; <br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>Query OK, 0 rows affected (0.475 sec)<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (id,aid,bid)&nbsp;&nbsp; <br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>Query OK, 0 rows affected (0.532 sec)<br/><br/><br/>CREATE TABLE t1 (&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; id INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; aid DATE NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; bid INT NOT NULL,&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY (id,aid,bid),<br/>&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE KEY (bid)&nbsp;&nbsp;&nbsp;&nbsp;<br/>)&nbsp;&nbsp;<br/>PARTITION BY KEY(bid)&nbsp;&nbsp;<br/>PARTITIONS 10;<br/>Query OK, 0 rows affected (0.526 sec)<br/><br/>这三个都建立分区表成功了，因为bid同时属于一个或以上的唯一性索引的键<br/><br/>通过上面这些例子，大家可以深入的理解Mysql分区中分区键和唯一索引主键的关系了，希望对大家有所帮助。
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/577/</link>
<title><![CDATA[分库后跨库之间的关联查询]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 29 Dec 2021 09:19:25 +0000</pubDate> 
<guid>https://www.heckjj.com/post/577/</guid> 
<description>
<![CDATA[ 
	一、所有库在同一节点上<br/>　当业务垂直拆分出很多业务库的时候，如果都部署在同一个源上（同一个机器节点上），那么这种情况是最好办的，直接库名+表名 join就可以。<br/><br/>二、不在一个节点<br/> <br/>当不同的库来自不同的节点，那么就没办法直接join查询了，有以下方式和思路：<br/>1.全局表<br/>　　所谓全局表，就是有可能系统中所有模块都可能会依赖到的一些表。比较类似我们理解的“数据字典”。为了避免跨库join查询，我们可以将这类表在其他每个数据库中均保存一份。同时，这类数据通常也很少发生修改（甚至几乎不会），<br/>　　所以也不用太担心“一致性”问 题。<br/><br/>2.字段冗余<br/>　　这是一种典型的反范式设计，在互联网行业中比较常见，通常是为了性能来避免join查询。字段冗余能带来便利，是一种“空间换时间”的体现。但其适用场景也比较有限，比较适合依赖字段较少的情况。<br/><br/>　　最复杂的还是数据一致性问题，这点很难保证，可以借助数据库中的触发器或者在业务代码层面去保证。当然，也需要结合实际业务场景来看一致性的要求。<br/><br/><br/>3.数据同步<br/>　　定时A库中的tab_a表和B库中tbl_b有关联，可以定时将指定的表做同步。当然，同步本来会对数据库带来一定的影响，需要性能影响和数据时效性中取得一个平衡。这样来避免复杂的跨库查询。笔者曾经在项目中是通过ETL工具来实施的。<br/><br/><br/>4.第三方插件<br/>　　　　1.cobar<br/>　　　　2.MyCAT<br/>　　　　3.shardding-jdbc<br/>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post/570/</link>
<title><![CDATA[还在用Text类型吗？Mysql8.0增强的JSON类型,它不好吗？]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Tue, 07 Dec 2021 02:45:05 +0000</pubDate> 
<guid>https://www.heckjj.com/post/570/</guid> 
<description>
<![CDATA[ 
	一、前言<br/>MySQL 支持由 RFC 7159 定义的原生JSON 数据类型，该数据类型可以有效访问 JSON（JavaScript Object Notation）中的元素数据。与将JSON 格式的字符串存储为单个字符串类型相比，JSON 数据类型具有以下优势：<br/><br/>自动验证存储在JSON列中的JSON数据格式。无效格式会报错。<br/>优化的存储格式。存储在JSON列中的JSON文档被转换为允许快速读取访问文档元素的内部格式。内部是以二进制格式存储JSON数据。<br/>对JSON文档元素的快速读取访问。当服务器读取JSON文档时，不需要重新解析文本获取该值。通过key或数组索引直接查找子对象或嵌套值，而不需要读取整个JSON文档。<br/>存储JSON文档所需的空间，大致与LONGBLOB或LONGTEXT相同<br/>存储在JSON列中的任何JSON文档的大小都仅限于设置的系统变量max_allowed_packet的值<br/>MySQL 8.0.13之前，JSON列不能有非null的默认值。<br/>在 MySQL 8.0 中，优化器可以对 JSON 列执行部分就地更新，而不是删除旧JSON串并将新串完整地写入列。<br/>MYSQL 8.0，除了提供JSON 数据类型，还有一组 SQL 函数可用于操作 JSON 的值，例如创建JSON对象、增删改查JSON数据中的某个元素。<br/><br/>二、常用JSON函数<br/>首先，创建表列时候，列要设置为JSON类型：<br/><br/>1<br/>CREATE TABLE t1 (content JSON);<br/>插入数据，可以像插入varchar类型的数据一样，把json串添加单引号进行插入：<br/><br/>1<br/>INSERT INTO t1 VALUES(&#039;&#123;&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;&#125;&#039;);<br/>当然mysql也提供了创建JSON对象的函数：<br/><br/>1<br/>INSERT INTO t1 VALUES(JSON_OBJECT(&quot;key1&quot;,&quot;value1&quot;,&quot;key2&quot;,&quot;value2&quot;));<br/>使用JSON_EXTRACT函数查询JSON类型数据中某个元素的值：<br/><br/><br/>mysql&gt; SELECT&nbsp;&nbsp;JSON_EXTRACT(content,&quot;$.key1&quot;) from t1;<br/><br/>+--------------------------------+<br/>&#124; JSON_EXTRACT(content,&quot;$.key1&quot;) &#124;<br/>+--------------------------------+<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+--------------------------------+<br/>2 rows in set (0.00 sec)<br/>lamba表达式风格查询：<br/><br/><br/>mysql&gt; SELECT content-&gt;&quot;$.key1&quot; from t1;<br/>+-------------------+<br/>&#124; content-&gt;&quot;$.key1&quot; &#124;<br/>+-------------------+<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+-------------------+<br/>2 rows in set (0.00 sec)<br/>使用JSON_SET函数更新JSON中某个元素的值，如果不存在则添加：<br/><br/><br/>mysql&gt; update t1 set content=JSON_SET(content,&quot;$.key1&quot;,&#039;value111&#039;);<br/>Query OK, 2 rows affected (0.00 sec)<br/><br/>Rows matched: 2&nbsp;&nbsp;Changed: 2&nbsp;&nbsp;Warnings: 0<br/>更多JSON类型数据操作函数，可以参考：https://dev.mysql.com/doc/refman/8.0/en/json.html<br/><br/>三、MyBatis中使用JSON类型及其操作函数<br/>比如Device表里面有个JSON类型的content字段，其中含有名称为name的元素，我们来修改和查询name元素对应的值。<br/><br/>ExtMapper中定义修改和查询接口：<br/><br/>@Mapper<br/>public interface DeviceDOExtMapper extends com.zlx.user.dal.mapper.DeviceDOMapper &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//更新JSON串中名称为name的key的值<br/>&nbsp;&nbsp;&nbsp;&nbsp;int updateName(@Param(&quot;name&quot;) String name, @Param(&quot;query&quot;) DeviceQuery query);<br/>&nbsp;&nbsp;&nbsp;&nbsp;//查询JSON串中名称为name的key的值<br/>&nbsp;&nbsp;&nbsp;&nbsp;String selectName(DeviceQuery query);<br/>&#125;<br/>ExtMapper.xml中定义查询sql:<br/><br/>&lt;mapper namespace=&quot;com.zlx.user.dal.mapper.ext.DeviceDOExtMapper&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--更新JSON串中名称为name的key的值--&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;update id=&quot;updateName&quot; parameterType=&quot;map&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;update device<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;set&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;name != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content = JSON_SET(content, &#039;$.name&#039;, #&#123;name,jdbcType=VARCHAR&#125;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/set&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;_parameter != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;include refid=&quot;Update_By_Example_Where_Clause&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/update&gt;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--查询JSON串中名称为name的key的值--&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;select id=&quot;selectName&quot; parameterType=&quot;com.zlx.user.dal.model.DeviceQuery&quot; resultType=&quot;java.lang.String&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`content`-&gt;&#039;$.name&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from device<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;_parameter != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;include refid=&quot;Example_Where_Clause&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;<br/>&lt;/mapper&gt;<br/>总结<br/>虽然我们实践上不建议把所有扩展字段都放到一个大字段里面。但是即使有原因一定到放，那么也建议选择JSON类型，而不是varcahr和Text类型。<br/><br/><br/>参考：<br/><a href="https://dev.mysql.com/doc/refman/8.0/en/json.html" target="_blank">https://dev.mysql.com/doc/refman/8.0/en/json.html</a>
]]>
</description>
</item>
</channel>
</rss>