当前位置:网站首页>【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,联合索引的其他字段顺序对也是无法走索引的。这就叫最佳做前缀法则,联合索引的开头字段必须存在。
边栏推荐
- C. The Third Problem(找规律)
- Lombok原理和同时使⽤@Data和@Builder 的坑
- HotSpot VM
- Chinese brand hybrid technology: there is no best technical route, only better products
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
- Lora gateway Ethernet transmission
- Determine which week of the month the day is
- Stable Huawei micro certification, stable Huawei cloud database service practice
- asp. Core is compatible with both JWT authentication and cookies authentication
猜你喜欢
食品行业仓储条码管理系统解决方案
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
Practical development of member management applet 06 introduction to life cycle function and user-defined method
[Key shake elimination] development of key shake elimination module based on FPGA
Web components series (VII) -- life cycle of custom components
Lombok principle and the pit of ⽤ @data and @builder at the same time
[FPGA tutorial case 11] design and implementation of divider based on vivado core
20、 EEPROM memory (AT24C02) (similar to AD)
Lora gateway Ethernet transmission
Overturn your cognition? The nature of get and post requests
随机推荐
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
51nod 1130 n factorial length V2 (Stirling approximation)
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Practical development of member management applet 06 introduction to life cycle function and user-defined method
MySQL transaction isolation level
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
How to execute an SQL statement in MySQL
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
【leetcode】1189. Maximum number of "balloons"
HotSpot VM
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
POI add border
Stable Huawei micro certification, stable Huawei cloud database service practice
About some basic DP -- those things about coins (the basic introduction of DP)
DM8 archive log file manual switching
Mysql数据库慢sql抓取与分析
Mlapi series - 04 - network variables and network serialization [network synchronization]
脚本生命周期