mysql加索引优化sql_MySQL添加索引优化SQL
在慢查询日志中有一条慢SQL,执行时间约为3秒mysql> SELECT
-> t.total_meeting_num,
-> r.voip_user_num
-> FROM
-> (
-> SELECT
-> count(*) total_meeting_num
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND billingcode != 651158
-> AND billingcode != 651204
-> ) t,
-> (
-> SELECT
-> count(userID) voip_user_num
-> FROM
-> (
-> SELECT
-> conferenceID,
-> userID,
-> isOnline,
-> createdTime
-> FROM
-> (
-> SELECT
-> *
-> FROM
-> ConferenceUser
-> WHERE
-> createdTime >= ADDDATE(now(), - 1)
-> AND userID > 1000
-> ORDER BY
-> userID,
-> createdTime DESC
-> ) t
-> GROUP BY
-> userID
-> ) t,
-> (
-> SELECT
-> *
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND conferenceName NOT LIKE 'evmonitor%'
-> ) r
-> WHERE
-> t.isOnline = 1
-> AND t.conferenceID = r.conferenceID
-> ) r;
+-------------------+---------------+
| total_meeting_num | voip_user_num |
+-------------------+---------------+
| 29 | 48 |
+-------------------+---------------+
1 row in set (3.01 sec)
查看执行计划mysql> explain SELECT
-> t.total_meeting_num,
-> r.voip_user_num
-> FROM
-> (
-> SELECT
-> count(*) total_meeting_num
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND billingcode != 651158
-> AND billingcode != 651204
-> ) t,
-> (
-> SELECT
-> count(userID) voip_user_num
-> FROM
-> (
-> SELECT
-> conferenceID,
-> userID,
-> isOnline,
-> createdTime
-> FROM
-> (
-> SELECT
-> *
-> FROM
-> ConferenceUser
-> WHERE
-> createdTime >= ADDDATE(now(), - 1)
-> AND userID > 1000
-> ORDER BY
-> userID,
-> createdTime DESC
-> ) t
-> GROUP BY
-> userID
-> ) t,
-> (
-> SELECT
-> *
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND conferenceName NOT LIKE 'evmonitor%'
-> ) r
-> WHERE
-> t.isOnline = 1
-> AND t.conferenceID = r.conferenceID
-> ) r;
+----+-------------+----------------+--------+----------------+----------------+---------+------+---------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+--------+----------------+----------------+---------+------+---------+---------------------------------+
| 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
| 3 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 18 | |
| 3 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 12667 | Using where; Using join buffer |
| 6 | DERIVED | Conference | range | ind_start_time | ind_start_time | 5 | NULL | 889 | Using where |
| 4 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 18918 | Using temporary; Using filesort |
| 5 | DERIVED | ConferenceUser | ALL | NULL | NULL | NULL | NULL | 6439656 | Using where; Using filesort |
| 2 | DERIVED | Conference | range | ind_start_time | ind_start_time | 5 | NULL | 889 | Using where |
+----+-------------+----------------+--------+----------------+----------------+---------+------+---------+---------------------------------+
8 rows in set (3.04 sec)
查看索引mysql> show index from ConferenceUser;
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ConferenceUser | 0 | PRIMARY | 1 | recordID | A | 6439758 | NULL | NULL | | BTREE | | |
| ConferenceUser | 0 | PRIMARY | 2 | conferenceID | A | 6439758 | NULL | NULL | | BTREE | | |
| ConferenceUser | 1 | ind_conference_userID | 1 | conferenceID | A | 804969 | NULL | NULL | | BTREE | | |
| ConferenceUser | 1 | ind_conference_userID | 2 | userID | A | 3219879 | NULL | NULL | | BTREE | | |
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
在表的列上添加索引mysql> alter table ConferenceUser add index index_createdtime(createdTime);
Query OK, 6439784 rows affected (38.46 sec)
Records: 6439784 Duplicates: 0 Warnings: 0
查看索引
mysql> show index from ConferenceUser;
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ConferenceUser | 0 | PRIMARY | 1 | recordID | A | NULL | NULL | NULL | | BTREE | | |
| ConferenceUser | 0 | PRIMARY | 2 | conferenceID | A | 6439794 | NULL | NULL | | BTREE | | |
| ConferenceUser | 1 | ind_conference_userID | 1 | conferenceID | A | 715532 | NULL | NULL | | BTREE | | |
| ConferenceUser | 1 | ind_conference_userID | 2 | userID | A | 3219897 | NULL | NULL | | BTREE | | |
| ConferenceUser | 1 | index_createdtime | 1 | createdTime | A | 6439794 | NULL | NULL | | BTREE | | |
+----------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)
再次执行时间缩短为0.17秒mysql> SELECT
-> t.total_meeting_num,
-> r.voip_user_num
-> FROM
-> (
-> SELECT
-> count(*) total_meeting_num
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND billingcode != 651158
-> AND billingcode != 651204
-> ) t,
-> (
-> SELECT
-> count(userID) voip_user_num
-> FROM
-> (
-> SELECT
-> conferenceID,
-> userID,
-> isOnline,
-> createdTime
-> FROM
-> (
-> SELECT
-> *
-> FROM
-> ConferenceUser
-> WHERE
-> createdTime >= ADDDATE(now(), - 1)
-> AND userID > 1000
-> ORDER BY
-> userID,
-> createdTime DESC
-> ) t
-> GROUP BY
-> userID
-> ) t,
-> (
-> SELECT
-> *
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND conferenceName NOT LIKE 'evmonitor%'
-> ) r
-> WHERE
-> t.isOnline = 1
-> AND t.conferenceID = r.conferenceID
-> ) r;
+-------------------+---------------+
| total_meeting_num | voip_user_num |
+-------------------+---------------+
| 29 | 52 |
+-------------------+---------------+
1 row in set (0.17 sec)
查看执行计划mysql> explain SELECT
-> t.total_meeting_num,
-> r.voip_user_num
-> FROM
-> (
-> SELECT
-> count(*) total_meeting_num
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND billingcode != 651158
-> AND billingcode != 651204
-> ) t,
-> (
-> SELECT
-> count(userID) voip_user_num
-> FROM
-> (
-> SELECT
-> conferenceID,
-> userID,
-> isOnline,
-> createdTime
-> FROM
-> (
-> SELECT
-> *
-> FROM
-> ConferenceUser
-> WHERE
-> createdTime >= ADDDATE(now(), - 1)
-> AND userID > 1000
-> ORDER BY
-> userID,
-> createdTime DESC
-> ) t
-> GROUP BY
-> userID
-> ) t,
-> (
-> SELECT
-> *
-> FROM
-> Conference
-> WHERE
-> isStart = 1
-> AND startTime >= ADDDATE(now(), - 1)
-> AND conferenceName NOT LIKE 'evmonitor%'
-> ) r
-> WHERE
-> t.isOnline = 1
-> AND t.conferenceID = r.conferenceID
-> ) r;
+----+-------------+----------------+--------+-------------------+-------------------+---------+------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+--------+-------------------+-------------------+---------+------+-------+---------------------------------+
| 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | PRIMARY | | system | NULL | NULL | NULL | NULL | 1 | |
| 3 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 20 | |
| 3 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 12682 | Using where; Using join buffer |
| 6 | DERIVED | Conference | range | ind_start_time | ind_start_time | 5 | NULL | 879 | Using where |
| 4 | DERIVED | | ALL | NULL | NULL | NULL | NULL | 18951 | Using temporary; Using filesort |
| 5 | DERIVED | ConferenceUser | range | index_createdtime | index_createdtime | 4 | NULL | 31455 | Using where; Using filesort |
| 2 | DERIVED | Conference | range | ind_start_time | ind_start_time | 5 | NULL | 879 | Using where |
+----+-------------+----------------+--------+-------------------+-------------------+---------+------+-------+---------------------------------+
8 rows in set (0.18 sec)
原文:http://blog.51cto.com/11784929/2091627
mysql加索引优化sql_MySQL添加索引优化SQL相关推荐
- 在mysql中如何为连接添加索引_在MySQL中如何为连接添加索引
http://hackmysql.com/case4 译文: 我先通过一个简单的例子说明在MySQL中如何为连接添加索引,然后再看一个有挑战性的例子. 简单的3个表的连接 表结构很简单,3个表tblA ...
- 小蚂蚁学习mysql性能优化(5)--SQL以及索引优化--需要添加索引的列
2019独角兽企业重金招聘Python工程师标准>>> 在哪些列上添加索引? 1. 通常选择在where从句中,group by从句,order by从句,on从句中出现的列添 ...
- mysql优化(添加索引)
问题描述: 最近有一台虚拟机CPU运行很高,平常超过50%,到有定时任务时,达到100%. 下图为CPU使用率 cpu utilization 原因分析: 进入到MySQL数据库, 使用查询命令:sh ...
- mysql怎么加索引_mysql怎么添加索引
在mysql中可以通过使用alter table这个SQL语句来为表中的字段添加索引. 1.添加PRIMARY KEY(主键索引)mysql>ALTER TABLE `table_name` A ...
- mysql 如何加索引_mysql如何添加索引
mysql添加索引的方法:可以通过[create table]语句来添加,如[CONSTRAINT PRIMARY KEY | INDEX [] [] ],表示创建一般索引. 在mysql中可以在创建 ...
- mysql添加临时索引_mysql 中添加索引的三种方法
在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以通过不同的应用场景来进行索引的新建,在此列出三种新建索引的方法 mysql 中添加索引的三种方法 1.1 新建表中添加索 ...
- mysql如何给text字段添加索引_MySQL 是如何利用索引的
阅读本文大概需要 4 分钟. 一.前言 在 MySQL 中进行 SQL 优化的时候,经常会在一些情况下,对 MySQL 能否利用索引有一些迷惑.例如: MySQL 在遇到范围查询条件的时候就停止匹配了 ...
- mysql添加索引后查询先用索引吗_mysql 添加索引后 在查询的时候是mysql就自动从索引里面查询了。还是查询的时候有单 独的参数查询索引?...
满意答案 llt1711 2014.06.20 采纳率:49% 等级:9 已帮助:614人 MYSQL创建索引对索引使用方式分两种: 1 由数据库查询优化器自动判断否使用索引: 2 用户写SQL ...
- php增加mysql索引_mysql怎么添加索引
在mysql中可以通过使用alter table这个SQL语句来为表中的字段添加索引. 1.添加PRIMARY KEY(主键索引)mysql>ALTER TABLE `table_name` A ...
最新文章
- Ice “Hello World”的实现
- J-Flash的使用
- 30秒就能理解的JavaScript优秀代码
- Nginx 下载限速
- Qt编程'hello world
- 案例:按照JSP Model2思想实现用户注册功能
- 年轻人,AI不想给你加薪升职
- 集合点(lr_rendezvous)
- 组词组合 php,PHP实现的简单组词算法示例
- Rust FFI 编程 - Rust 语言层面对 FFI 的支持
- 【TWVRP】基于matlab禁忌搜索和节约算法求解带时间窗的车辆路径规划问题【含Matlab源码 1229期】
- 计算机故障语言 英语怎么说,故障英语怎么说
- 2-2-2-webpack打包
- 软件测试与软件调试的区别
- 人、机客户服务质量 - 实时透视分析
- java的regex_java regex 简单使用
- switch语句(分支语句)
- “正信小宝”养老篇:坐享绿色稳定收益
- new Date()得到时间是东八区时间 存储到mysql里面少了八个小时 原来以为是 java new date 与系统时间相差8小时
- java servlet过滤器简解及实例
热门文章
- 第十六届全国大学智能汽车竞赛竞速比赛规则
- HCTL-2020正交码读写芯片
- Navicat中查询哪些表有指定的字段名(技巧)
- 南京微盟计算机,南京微盟 ME6118A50B3G ME6119A33PG ME6119A50PG 稳压IC
- 非抢占式优先算法例题_非抢占式高优先级调度算法
- php flock 死锁了,php – 防止由flock引起的死锁
- java风控系统规则引擎_如何设计一套规则引擎系统
- matepad什么时候升级鸿蒙,华为MatePad Pro迎来EMUI 11正式版升级 后续可直接升级鸿蒙系统...
- xilinx windows 下烧录脚本 稳定性超好
- mysql 修改上传文件大小限制吗_修改PHP上传文件大小限制的方法(转)