当前位置:网站首页>【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,联合索引的其他字段顺序对也是无法走索引的。这就叫最佳做前缀法则,联合索引的开头字段必须存在。
边栏推荐
- Web components series (VII) -- life cycle of custom components
- Leetcode32 longest valid bracket (dynamic programming difficult problem)
- P2022 有趣的数(二分&数位dp)
- Hashlimit rate control
- IDEA编译JSP页面生成的class文件路径
- /usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
- MySQL master-slave replication
- Path of class file generated by idea compiling JSP page
- Record the pit of NETCORE's memory surge
- pd. to_ numeric
猜你喜欢

DM8 backup set deletion

【FPGA教程案例11】基于vivado核的除法器设计与实现

Execution order of scripts bound to game objects

About some basic DP -- those things about coins (the basic introduction of DP)

Cross domain and jsonp details

Solution to the problem that the root account of MySQL database cannot be logged in remotely

Solution of storage bar code management system in food industry

Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts

STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储

Stack and queue
随机推荐
IDEA编译JSP页面生成的class文件路径
Database, relational database and NoSQL non relational database
10个 Istio 流量管理 最常用的例子,你知道几个?
Proof of Stirling formula
Lora gateway Ethernet transmission
绑定在游戏对象上的脚本的执行顺序
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
JVM garbage collector concept
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
10個 Istio 流量管理 最常用的例子,你知道幾個?
Chinese brand hybrid technology: there is no best technical route, only better products
Execution order of scripts bound to game objects
Slow SQL fetching and analysis of MySQL database
2/13 review Backpack + monotonic queue variant
1291_ Add timestamp function in xshell log
[disassembly] a visual air fryer. By the way, analyze the internal circuit
729. 我的日程安排表 I(set or 动态开点线段树)
[Key shake elimination] development of key shake elimination module based on FPGA
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND