mysql 优化实例之索引创建

mysql 优化实例之索引创建

优化前:

pt-query-degist分析结果:

# Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7EE47 at byte 394687
# This item is included in the report because it matches --limit.
# Scores: V/M = 3.27
# Time range: 2016-09-29T11:46:22 to 2016-10-01T12:45:02
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count          0       3
# Exec time      0     78s     19s     39s     26s     39s      9s     19s
# Lock time      0   328us    66us   136us   109us   131us    30us   125us
# Rows sent      0       6       1       3       2    2.90    0.78    1.96
# Rows examine   0       6       1       3       2    2.90    0.78    1.96
# Query size     0     348     116     116     116     116       0     116
# String:
# Databases    wechat_prod
# Hosts        localhost
# Users        test
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms
#  10ms
# 100ms
#    1s
#  10s+  ################################################################
# Tables
#    SHOW TABLE STATUS FROM `wechat_prod` LIKE 'sys_files'\G
#    SHOW CREATE TABLE `wechat_prod`.`sys_files`\G
# EXPLAIN /*!50100 PARTITIONS*/
SELECT `id`, `url_small`, `url_big`, `bus_id` FROM `sys_files` WHERE (`bus_type`='wp_goods') AND (`bus_id`='32597')\G

表结构

CREATE TABLE `sys_files` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url_small` varchar(255) DEFAULT NULL,
  `url_big` varchar(255) DEFAULT NULL,
  `bus_type` varchar(255) DEFAULT NULL,
  `bus_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_name` (`bus_type`),
  KEY `index_name2` (`bus_id`)
) ENGINE=InnoDB AUTO_INCREMENT=207750 DEFAULT CHARSET=utf8

sql执行分析

mysql> explain SELECT `id`, `url_small`, `url_big`, `bus_id` FROM `sys_files` WHERE (`bus_type`='wp_goods') AND (`bus_id`='32597');
+----+-------------+------------+------------+------+------------------------+-------------+---------+-------+------+----------+------------------------------------+
| id | select_type | table      | partitions | type | possible_keys          | key         | key_len | ref   | rows | filtered | Extra                              |
+----+-------------+------------+------------+------+------------------------+-------------+---------+-------+------+----------+------------------------------------+
|  1 | SIMPLE      | sys_files | NULL       | ref  | index_name,index_name2 | index_name2 | 9       | const |    3 |    50.00 | Using index condition; Using where |
+----+-------------+------------+------------+------+------------------------+-------------+---------+-------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)

根据业务逻辑,索引创建错误:不应该创建两个单独的索引bus_typebus_id,此处虽然创建了两个索引,但真正用到的索引只是index_name2,索引index_name没有任何作用。会占用空间,并影响写入和更新的性能。

alter table sys_files drop index index_name;

修改后索引使用情况:

mysql> explain SELECT `id`, `url_small`, `url_big`, `bus_id` FROM `sys_files` WHERE (`bus_type`='wp_goods') AND (`bus_id`='32597');
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+------+----------+------------------------------------+
| id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra                              |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+------+----------+------------------------------------+
|  1 | SIMPLE      | sys_files | NULL       | ref  | index_name2   | index_name2 | 9       | const |    3 |    10.00 | Using index condition; Using where |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)

sql写法

SELECT `id`, `url_small`, `url_big`, `bus_id` FROM `sys_files` WHERE (`bus_type`='wp_goods') AND (`bus_id`='32597')

改为

SELECT `id`, `url_small`, `url_big`, `bus_id` FROM `sys_files` WHERE (`bus_id`='32597') AND (`bus_type`='wp_goods')

sql编写的原则:把辨识度高的重复率低的写在左边。

时间: 2024-08-01 15:53:48

mysql 优化实例之索引创建的相关文章

探究MySQL优化器对索引和JOIN顺序的选择_Mysql

本文通过一个案例来看看MySQL优化器如何选择索引和JOIN顺序.表结构和数据准备参考本文最后部分"测试环境".这里主要介绍MySQL优化器的主要执行流程,而不是介绍一个优化器的各个组件(这是另一个话题).    我们知道,MySQL优化器只有两个自由度:顺序选择:单表访问方式:这里将详细剖析下面的SQL,看看MySQL优化器如何做出每一步的选择. explain select * from employee as A,department as B where A.LastName

MySQL 5.7 : 索引创建优化(Bulk Load)

先来看一组测试数据 使用sysbench,生成一张大约10,000,000行记录的表,数据全部在buffer pool中 创建索引(k,pad) 5.7.5 alter table sbtest1 add key(k,pad); Query OK, 0 rows affected (44.14 sec) Records: 0  Duplicates: 0  Warnings: 0 5.7.4 root@sbb 04:25:11>alter table sbtest1 add key(k,pad)

MySQL数据库优化技术之索引使用技巧总结_Mysql

本文实例总结了MySQL数据库优化技术的索引用法.分享给大家供大家参考,具体如下: 这里紧接上一篇<MySQL数据库优化技术之配置技巧总结>,进一步分析索引优化的技巧: (七)表的优化 1. 选择合适的数据引擎 MyISAM:适用于大量的读操作的表 InnoDB:适用于大量的写读作的表 2.选择合适的列类型 使用 SELECT * FROM TB_TEST PROCEDURE ANALYSE()可以对这个表的每一个字段进行分析,给出优化列类型建议 3.对于不保存NULL值的列使用NOT NUL

mysql优化之路----hash索引优化_Mysql

创建表 CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `msg` varchar(20) NOT NULL DEFAULT '', `crcmsg` int(15) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 //插入数据 insert into t1 (msg) values('w

MySQL SQL优化之覆盖索引

前些天,有个同事跟我说:"我写了个SQL,SQL很简单,但是查询速度很慢,并且针对查询条件创建了索引,然而索引却不起作用,你帮我看看有没有办法优化?". 我对他提供的case进行了优化,并将优化过程整理了下来. 优化前的表结构.数据量.SQL.执行计划.执行时间 表结构 CREATE TABLE `t_order` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `order_code` char(12) NOT NULL, `o

mysql sql优化实例

mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B5 at byte 628331 # This item is included in the report because it matches --limit. # Scores: V/M = 0.19 # Time range: 2016-09-24T15:14:24 to 2016-10-0

案例:MySQL优化器如何选择索引和JOIN顺序

我们知道,MySQL优化器只有两个自由度:顺序选择:单表访问方式:这里将详细剖析下面的SQL,看看MySQL优化器如何做出每一步的选择. explain select * from employee as A,department as B where A.LastName = 'zhou' and B.DepartmentID = A.DepartmentID and B.DepartmentName = 'TBX'; 1. 可能的选择 这里看到JOIN的顺序可以是A|B或者B|A,单表访问方

[MySQL 源码] innodb如何创建二级索引

以下为分析问题时的随笔.写的很凌乱,仅做记录,以备后用...... ////////////////////////////////////////////////////////////// ha_innobase::add_index是innodb创建索引的接口函数. 以下所有的讨论都是基于创建一个非聚集的二级索引.因此一些过程是被省略掉了. 1.获取数据词典信息           indexed_table = dict_table_get(prebuilt->table->name,

Mysql性能优化案例 - 覆盖索引分享_Mysql

场景 产品中有一张图片表,数据量将近100万条,有一条相关的查询语句,由于执行频次较高,想针对此语句进行优化 表结构很简单,主要字段: 复制代码 代码如下: user_id 用户ID picname 图片名称 smallimg 小图名称 一个用户会有多条图片记录 现在有一个根据user_id建立的索引:uid 查询语句也很简单:取得某用户的图片集合 复制代码 代码如下: select picname, smallimg from pics where user_id = xxx; 优化前 执行查