当前位置:网站首页>【HBZ分享】云数据库如何定位慢查询
【HBZ分享】云数据库如何定位慢查询
2022-07-06 04:16:00 【hbz-】
如何配置Mysql慢查询
1. 查询慢日志相关信息:SHOW VARIABLES LIKE '%query%'
2. 开启慢日志记录文件:set global slow_query_log = 'ON'
3. 记录慢日志的文件名:slow_query_log_file
4. 配置达到慢查询时间:set global long_query_time = 1 // 修改慢查询时间1s,即查询超过1s就记录到慢查询日志中
5. 注意:修改慢查询时间后,记得需要重新连接才能生效
如何定位慢查询
- 通过EXPLAIN查询该语句是否走了索引,没走索引说明走了全表扫描
- 注意:生产环境一般不允许这样做,生产环境一般会通过自动化平台,通过可视化界面来看
EXPLAIN 如何使用?
- 使用方式:EXPLAIN SELECT * FROM test WHERE age = 10
EXPLAIN 中的Type字段含义
- type = all:直接全表扫描数据, 效率极低
- type = index:需要优化,虽然也是走索引,但表示全表扫描【索引】文件,不是扫数据
- type = range:sql最低的满足条件是range,只查询给定范围的行,使用一个索引来选择行
- type = ref:一般的要求该级别,当字段加了普通索引,而条件恰好也是该字段,则就是ref类型,比如使用name = ‘hbz’这个条件,而name创建了普通索引,此时就是ref
- type = eq_ref:通过主键 或 唯一索引进行关联表查询时就是eq_ref,算是除了const最好的结果了
- type = const:性能极高的级别,根据主键索引id找,一般都是const,无需优化
Mysql为什么做联合索引要遵从最佳左前缀法则?
- 联合索引会以前缀优先
- 如果联合顺序是 name, age, position
- 举例:
// 顺序:name, age, postion -->由于条件是按照联合索引顺序,所以会触发ref索引级别
SQL: SELECT name, age FROM people WHERE name = 'hbz' and age = 21 and positon = 'tetst'
// 只有name -->由于条件带有前缀name,所以会触发ref索引级别
SQL: SELECT name, age FROM people WHERE name = 'hbz'
// 只有name, position -->由于条件带有前缀name,所以会触发ref索引级别
SQL: SELECT name, age FROM people WHERE name = 'hbz' and positon = 'tetst'
// 只有position, 没有name -->由于没有前缀name,所以不会触发索引
SQL: SELECT name, age FROM people WHERE positon = 'tetst'
// 有name,但和联合索引顺序不一致 -->条件有name字段,所以会触发索引,虽然不是按顺序,但是mysql底层会把name给优化放到前面
SQL: SELECT name, age FROM people WHERE positon = 'tetst' and name = 'hbz'
- 综上所述,所谓最左前缀法则,就是,如果where后面带有name,那么必然会走索引,name的顺序在哪里都无所谓,因为mysql底层会优化,把name放到最前面。但如果没有name,联合索引的其他字段顺序对也是无法走索引的。这就叫最佳做前缀法则,联合索引的开头字段必须存在。
边栏推荐
- Mixed development of QML and QWidget (preliminary exploration)
- P2022 有趣的数(二分&数位dp)
- MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
- 2327. 知道秘密的人数(递推)
- [disassembly] a visual air fryer. By the way, analyze the internal circuit
- /usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
- Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Basic use of MySQL (it is recommended to read and recite the content)
- 20、 EEPROM memory (AT24C02) (similar to AD)
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Chinese brand hybrid technology: there is no best technical route, only better products

Query the number and size of records in each table in MySQL database

Web components series (VII) -- life cycle of custom components

食品行业仓储条码管理系统解决方案
![[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos](/img/41/27ce3741ef29e87c0f3b954fdef87a.png)
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos

DM8 backup set deletion

Interface idempotency

Record the pit of NETCORE's memory surge

20、 EEPROM memory (AT24C02) (similar to AD)

Redis (replicate dictionary server) cache
随机推荐
P2102 地砖铺设(dfs&贪心)
Stack and queue
Fedora/rehl installation semanage
Fundamentals of SQL database operation
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
E. Best Pair
Solve the compilation problem of "c2001: line breaks in constants"
Web components series (VII) -- life cycle of custom components
Python book learning notes - Chapter 09 section 01 create and use classes
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
20、 EEPROM memory (AT24C02) (similar to AD)
About some basic DP -- those things about coins (the basic introduction of DP)
1291_ Add timestamp function in xshell log
Tips for using dm8huge table
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
2/13 review Backpack + monotonic queue variant
VNCTF2022 WriteUp
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
Record the pit of NETCORE's memory surge