每天进步一点点—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优化技巧
我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...
随机推荐
- 利用委托实现自己的数据缓存仓库(附上Demo)
Demo源码 写在前面的话 写完这篇博客后,总觉得少了些什么,后来想了下,感觉自己只是把结果给亮了出来,自己为什么想到这么做,这个类库出生的缘由未详述,因此,在本段作下说明,如有不足之处,希望能和大家 ...
- Selenium WebDriver- 操作JavaScript的prompt弹窗(使用率低)
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- python - unittest - testsuite and runner
前置条件: 测试用例部分或全部编写完成 一. 生成测试集 1. 方法1 - 通过加载函数来加载测试用例 import unittest from TestCase.test_login import ...
- [git 学习篇]工作区和暂存区
1 工作区,就是目录/User/my./learngit 2 版本库 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库. liuzhipeng@exdroid43:~/pad/pad- ...
- AtCoder Regular Contest 089
这场一边吃饭一边打,确实还是很菜的 C - Traveling Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem ...
- PHPSTORM杂技
PHPSTORM技巧 去掉右上角弹出浏览器条 settings->web browsers->show browser popup in the editor前的钩去掉 让class fu ...
- PHP curl 封装 GET及POST方法很不错的
<?php function curl_get($url, array $params = array(), $timeout = 5) { $ch = curl_init(); curl_se ...
- BZOJ4517 [Sdoi2016]排列计数 【组合数 + dp】
题目 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条件的 ...
- [中山市选]杀人游戏 (Tarjan缩点)
题目链接 Solution 可以考虑到如果知道环内一点的身份,如果凶手在其中就查出来了,同时不会有危险. 那么对警察造成威胁的就是那些身份不明且不能从其他点转移过来的点. 那么大部答案就是缩完点之后入 ...
- bzoj 3625小朋友和二叉树 多项式求逆+多项式开根 好题
题目大意 给定n种权值 给定m \(F_i表示权值和为i的二叉树个数\) 求\(F_1,F_2...F_m\) 分析 安利博客 \(F_d=F_L*F_R*C_{mid},L+mid+R=d\) \( ...