当前位置:网站首页>【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,联合索引的其他字段顺序对也是无法走索引的。这就叫最佳做前缀法则,联合索引的开头字段必须存在。
边栏推荐
- Hashlimit rate control
- About some basic DP -- those things about coins (the basic introduction of DP)
- /usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
- Tips for using dm8huge table
- MySql数据库root账户无法远程登陆解决办法
- Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
- 51nod 1130 n factorial length V2 (Stirling approximation)
- E. Best Pair
- About some basic DP -- those things about coins (the basic introduction of DP)
猜你喜欢
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
About some basic DP -- those things about coins (the basic introduction of DP)
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
[disassembly] a visual air fryer. By the way, analyze the internal circuit
记一次excel XXE漏洞
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Stack and queue
lora网关以太网传输
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
Cross domain and jsonp details
随机推荐
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Redis (replicate dictionary server) cache
Database, relational database and NoSQL non relational database
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
Viewing and verifying backup sets using dmrman
Mixed development of QML and QWidget (preliminary exploration)
IDEA编译JSP页面生成的class文件路径
E. Best Pair
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
Maxay paper latex template description
SharedPreferences 源码分析
C. The Third Problem(找规律)
Explain in simple terms node template parsing error escape is not a function
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
P2102 地砖铺设(dfs&贪心)
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
Interface idempotency
Class A, B, C networks and subnet masks in IPv4
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share