每天进步一点点—SQL优化
一、 SQL优化
1. 通过show status 命令了解各种SQL的运行频率
mysql>show status like 'Com_%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_db_upgrade | 0 |
| Com_alter_event | 0 |
| Com_alter_function | 0 |
| Com_alter_procedure | 0 |
| Com_alter_server | 0 |
| Com_alter_table | 0 |
| Com_alter_tablespace | 0 |
| Com_alter_user | 0 |
| Com_analyze | 0 |
| Com_begin | 0 |
| Com_binlog | 0 |
| Com_call_procedure | 0 |
| Com_change_db | 4 |
| Com_change_master | 0 |
| Com_check | 0 |
| Com_checksum | 0 |
| Com_commit | 30 |
| Com_create_db | 1 |
| Com_create_event | 0 |
| Com_create_function | 3 |
| Com_create_index | 0 |
| Com_create_procedure | 3 |
| Com_create_server | 0 |
| Com_create_table | 16 |
| Com_create_trigger | 6 |
| Com_create_udf | 0 |
| Com_create_user | 0 |
| Com_create_view | 7 |
| Com_dealloc_sql | 0 |
| Com_delete | 0 | ——运行delete操作的次数
| Com_delete_multi | 0 |
| Com_do | 0 |
| Com_drop_db | 1 |
| Com_drop_event | 0 |
| Com_drop_function | 0 |
| Com_drop_index | 0 |
| Com_drop_procedure | 0 |
| Com_drop_server | 0 |
| Com_drop_table | 0 |
| Com_drop_trigger | 0 |
| Com_drop_user | 0 |
| Com_drop_view | 0 |
| Com_empty_query | 0 |
| Com_execute_sql | 0 |
| Com_flush | 0 |
| Com_get_diagnostics | 0 |
| Com_grant | 0 |
| Com_ha_close | 0 |
| Com_ha_open | 0 |
| Com_ha_read | 0 |
| Com_help | 0 |
| Com_insert | 1017 | ——运行insert操作次数。批量插入insert仅仅加1
| Com_insert_select | 0 |
| Com_install_plugin | 0 |
| Com_kill | 0 |
| Com_load | 0 |
| Com_lock_tables | 0 |
| Com_optimize | 0 |
| Com_preload_keys | 0 |
| Com_prepare_sql | 0 |
| Com_purge | 0 |
| Com_purge_before_date | 0 |
| Com_release_savepoint | 0 |
| Com_rename_table | 0 |
| Com_rename_user | 0 |
| Com_repair | 0 |
| Com_replace | 0 |
| Com_replace_select | 0 |
| Com_reset | 0 |
| Com_resignal | 0 |
| Com_revoke | 0 |
| Com_revoke_all | 0 |
| Com_rollback | 0 |
| Com_rollback_to_savepoint | 0 |
| Com_savepoint | 0 |
| Com_select | 7 | ——运行SELECT操作的次数。每次加1
| Com_set_option | 48 |
| Com_signal | 0 |
| Com_show_binlog_events | 0 |
| Com_show_binlogs | 0 |
| Com_show_charsets | 0 |
| Com_show_collations | 0 |
| Com_show_create_db | 0 |
| Com_show_create_event | 0 |
| Com_show_create_func | 0 |
| Com_show_create_proc | 0 |
| Com_show_create_table | 0 |
| Com_show_create_trigger | 0 |
| Com_show_databases |2 |
| Com_show_engine_logs | 0 |
| Com_show_engine_mutex | 0 |
| Com_show_engine_status | 0 |
| Com_show_events | 0 |
| Com_show_errors | 0 |
| Com_show_fields | 0 |
| Com_show_function_code | 0 |
| Com_show_function_status | 0 |
| Com_show_grants | 0 |
| Com_show_keys | 0 |
| Com_show_master_status | 0 |
| Com_show_open_tables | 0 |
| Com_show_plugins | 0 |
| Com_show_privileges | 0 |
| Com_show_procedure_code | 0 |
| Com_show_procedure_status | 0 |
| Com_show_processlist | 0 |
| Com_show_profile | 0 |
| Com_show_profiles | 0 |
| Com_show_relaylog_events | 0 |
| Com_show_slave_hosts | 0 |
| Com_show_slave_status | 0 |
| Com_show_status | 1 |
| Com_show_storage_engines | 0 |
| Com_show_table_status | 0 |
| Com_show_tables | 2 |
| Com_show_triggers | 0 |
| Com_show_variables | 0 |
| Com_show_warnings | 0 |
| Com_slave_start | 0 |
| Com_slave_stop | 0 |
| Com_stmt_close | 0 |
| Com_stmt_execute | 0 |
| Com_stmt_fetch | 0 |
| Com_stmt_prepare | 0 |
| Com_stmt_reprepare | 0 |
| Com_stmt_reset | 0 |
| Com_stmt_send_long_data | 0 |
| Com_truncate | 0 |
| Com_uninstall_plugin | 0 |
| Com_unlock_tables | 0 |
| Com_update | 0 | ——运行update操作次数
| Com_update_multi | 0 |
| Com_xa_commit | 0 |
| Com_xa_end | 0 |
| Com_xa_prepare | 0 |
| Com_xa_recover | 0 |
| Com_xa_rollback | 0 |
| Com_xa_start | 0 |
| Compression | OFF |
+---------------------------+-------+
142 rows in set (0.00 sec)
mysql>show status like 'Innodb_%';
+---------------------------------------+-------------+
| Variable_name | Value |
+---------------------------------------+-------------+
| Innodb_buffer_pool_dump_status | not started |
| Innodb_buffer_pool_load_status |not started |
| Innodb_buffer_pool_pages_data | 4436 |
| Innodb_buffer_pool_bytes_data | 72679424 |
| Innodb_buffer_pool_pages_dirty | 0 |
| Innodb_buffer_pool_bytes_dirty | 0 |
| Innodb_buffer_pool_pages_flushed | 2188 |
| Innodb_buffer_pool_pages_free | 3744 |
| Innodb_buffer_pool_pages_misc | 11 |
| Innodb_buffer_pool_pages_total | 8191 |
| Innodb_buffer_pool_read_ahead_rnd | 0 |
| Innodb_buffer_pool_read_ahead | 3328 |
| Innodb_buffer_pool_read_ahead_evicted |0 |
| Innodb_buffer_pool_read_requests | 2182160 |
| Innodb_buffer_pool_reads | 552 |
| Innodb_buffer_pool_wait_free | 0 |
| Innodb_buffer_pool_write_requests | 237898 |
| Innodb_data_fsyncs | 1721 |
| Innodb_data_pending_fsyncs | 0 |
| Innodb_data_pending_reads | 0 |
| Innodb_data_pending_writes | 0 |
| Innodb_data_read | 65753088 |
| Innodb_data_reads | 3910 |
| Innodb_data_writes | 3630 |
| Innodb_data_written | 79650304 |
| Innodb_dblwr_pages_written | 2188 |
| Innodb_dblwr_writes | 76 |
| Innodb_have_atomic_builtins | ON |
| Innodb_log_waits | 0 |
| Innodb_log_write_requests | 15742 |
| Innodb_log_writes | 897 |
| Innodb_os_log_fsyncs | 980 |
| Innodb_os_log_pending_fsyncs | 0 |
| Innodb_os_log_pending_writes | 0 |
| Innodb_os_log_written | 7911424 |
| Innodb_page_size | 16384 |
| Innodb_pages_created | 557 |
| Innodb_pages_read | 3879 |
| Innodb_pages_written | 2188 |
| Innodb_row_lock_current_waits | 0 |
| Innodb_row_lock_time | 0 |
| Innodb_row_lock_time_avg | 0 |
| Innodb_row_lock_time_max | 0 |
| Innodb_row_lock_waits | 0 |
| Innodb_rows_deleted | 0 |——运行delete删除的行数
| Innodb_rows_inserted | 46350 |——运行insert操作插入的行数
| Innodb_rows_read | 1733860 | ——select查询返回的行数。
| Innodb_rows_updated | 0 |——运行update更新的行数
| Innodb_num_open_files | 70 |
| Innodb_truncated_status_writes | 0 |
| Innodb_available_undo_logs | 128 |
+---------------------------------------+-------------+
51 rows in set (0.00 sec)
mysql>show status like 'connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 36 | ——试图连接MYSQLserver的次数
+---------------+-------+
1 row in set (0.00 sec
mysql>show status like 'uptime';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Uptime | 421477 |——数据库执行时间
+---------------+--------+
1 row in set (0.00 sec)
mysql>show status like 'slow_queries';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 0 | ——慢查次数
+---------------+-------+
1 row in set (0.00 sec)
2. 定位运行效率较低的SQL
1、通过慢查日志定位哪些运行效率较低的SQL语句:--log-slow-queries=filename 启动时,mysqld写一个包括全部运行时间超过long_query_time秒的SQL语句的日志文件。
2、慢查日志在查询结束后才记录,所以应用反映运行效率出现故障的时候,查询慢查询日志并不能定位问题,能够通过show processlist命令来查看当前mysql的进行线程。包含线程状态和是否锁表等,能够时时查看SQL的运行情况,同事对锁表操作进行优化。
3. 通过explain分析SQL的运行计划
mysql>explain select sum(amount) from customer a,payment b where 1=1 anda.customer_id=b.customer_id and email='JANE.BENNETT@sakilacustomer.org'\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: a
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 599
Extra: Using where
*************************** 2. row***************************
id: 1
select_type: SIMPLE
table: b
type: ref
possible_keys: idx_fk_customer_id
key: idx_fk_customer_id
key_len: 2
ref: sakila.a.customer_id
rows: 13
Extra: NULL
2 rows in set (0.00 sec)
ERROR:
No query specified
解释:
select_type:表示select的类型。常见的取值有SIMLE(简单表。不使用表连接或者子查询)
PRIMARY(主查询,即外层的查询)、UNION(UNION中第二个或者后面的查询语句)、SUBQUERY(子查询中的第一个SELECT)
table:输出结果集的表
type:表示MySQL在表中找到所需行的方式。或者叫訪问类型,常见类型例如以下:
ALL:全表扫描,遍历全部行来找到匹配行
index:索引全扫描,遍历整个索引来查询匹配的行
range:索引范围扫描。常见于<、<=、>、>=、between
ref:使用非唯一索引扫描或唯一索引的前缀扫描,返回匹配某个单独值的记录行
eq_ref:使用的索引是唯一索引,对于每一个索引键值,表中仅仅有一条记录匹配(多表一般使用parimary key 和unique index作为关联条件)
const/system:单表中最多有一行匹配。查询速度很迅速,所以这个匹配行中其它列的值能够被优化器在当前查询中当作常量来处理。
NULL:mysql不用訪问表或者索引,直接就能得到结果。
ref_or_null:与ref类似。差别在于条件中包括对NULL的查询。
index_merge:索引合并优化
unique_subquery:in的后面是一个查询主键字段的子查询
index_subquery:与uniue_subquery类似,差别在于IN的后面是查询非唯一索引字段的子查询。
possible_keys:表示查询时可能使用的索引。
key:表示实际使用的索引
key_len:使用到索引字段的长度
rows:扫描行的数量
extra:运行情况的说明和描写叙述,包括不适合在其它列中显示,可是对运行计划很重要的额外信息。
4. 通过explain extended和show warnings查看优化器运行情况
mysql>explain extended select * from t99 where 1=1 and id=10414\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: t99
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
通过showwarnings来查运行计划
mysql>show warnings\G;
*************************** 1. row***************************
Level: Note
Code: 1003
Message: /* select#1 */ select '10414' AS`id`,'rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin' AS `name` from`test2`.`t99` where 1
1 row in set (0.00 sec)
ERROR:
No query specified
能够看到运行计划会把1=1去掉,而且在explain extended多了一个filered字段。
5. 通过explain partitions来查看分区内容
mysql>explain partitions select * from emp1 where id=24088\G;
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: emp1
partitions: p0
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 69667
Extra: Using where
1 row in set (0.00 sec)
ERROR:
No query specified
每天进步一点点—SQL优化的更多相关文章
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十二)数据层优化-explain关键字及慢sql优化
本文提要 从编码角度来优化数据层的话,我首先会去查一下项目中运行的sql语句,定位到瓶颈是否出现在这里,首先去优化sql语句,而慢sql就是其中的主要优化对象,对于慢sql,顾名思义就是花费较多执行时 ...
- SQL优化之列裁剪和投影消除
列裁剪 对于没用到的列,则没有必要读取它们的数据去浪费无谓的IO 比如我们有一张表table1,它含有四列数据(a,b,c,d).当我们执行查询select a from table1 where c ...
- SQL优化案例—— RowNumber分页
将业务语句翻译成SQL语句不仅是一门技术,还是一门艺术. 下面拿我们程序开发工程师最常用的ROW_NUMBER()分页作为一个典型案例来说明. 先来看看我们最常见的分页的样子: WITH CTE AS ...
- sql 优化
1.选择最有效率的表名顺序(只在基于规则的优化器中有效): oracle的解析器按照从右到左的顺序处理 from 子句中的表名,from子句中写在最后的表(基础表driving table)将被最先处 ...
- SQL 优化总结
SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects SELECT name as '函数名称',xtype as XType FROM s ...
- (转)SQL 优化原则
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...
- sql优化阶段性总结以及反思
Sql优化思路阶段性心得: 这段时间的优化做了好几个案例,其实有很多的类似点,都是好几张大表的相互连接,然后执行长达好几个小时,甚至都跑不出来. 自己差不多的思路就是Parallel full tab ...
- mysql sql优化实例
mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...
- SQL优化技巧
我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...
随机推荐
- 1507: [NOI2003]Editor(块状链表)
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4157 Solved: 1677[Submit][Stat ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- HDU 5536 Chip Factory Trie
题意: 给出\(n(3 \leq n \leq 1000)\)个数字,求\(max(s_i+s_j) \bigoplus s_k\),而且\(i,j,k\)互不相等. 分析: 把每个数字看成一个\(0 ...
- JavaScript日期时间格式化函数
这篇文章主要介绍了JavaScript日期时间格式化函数分享,需要的朋友可以参考下 这个函数经常用到,分享给大家. 函数代码: //格式化参数说明: //y:年,M:月,d:日,h:时,m分,s:秒, ...
- 细嚼慢咽 Mongoose 5
此文已由作者黄锴授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 由于Mongoose一直没有中文文档,加上现在市面上充斥着太多"快速上手",很多中文 ...
- python算法-排列组合
排列组合 一.递归 1.自己调用自己 2.找到一个退出的条件 二.全排列:针对给定的一组数据,给出包含所有数据的排列的组合 1:1 1,2:[[1,2],[2,1]] 1,2,3:[[1,2,3],[ ...
- Xpath - Xpath定位
selenium 提供的xpath定位方法名为:find_element_by_xpath(xpath表达式) Xpath基本定位语法: / 绝对定位,从根节点选取 // 相对定位,从匹配选择的当前 ...
- 全套Office办公软件WORD/PPT/EXCEL视频教程 每日更新中
详情见Processon分享链接:https://www.processon.com/view/link/5b3f40abe4b09a67415e2bfc
- PHP define() 定义常量
PHP define()函数定义了运行时的常量, 具体语法如下所示: (PHP 4, PHP 5, PHP 7) define — Defines a named constant bool defi ...
- 慢查询阻塞了xtrabackup进而阻塞以后的sql导致的系统瘫痪问题
收到开发反应一库的sql频繁超时,系统几乎瘫痪,无法执行任何操作,我登上库先查看到当前的线程,发现有大量的线程状态是 Waiting for table flush 查看当前的事务 从昨天开始执行,到 ...