mysql优化查询
使用索引查询
MariaDB [test]> explain select * from te where id=22; #在没有增加索引情况下,rows为7,即查询行数
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | te | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.01 sec)
MariaDB [test]> alter table te add index tindex(id); #增加索引
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> explain select * from te where id=22; #rows变为1
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
| 1 | SIMPLE | te | ref | tindex | tindex | 5 | const | 1 | |
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
1 row in set (0.00 sec)
MariaDB [test]>
索引使用注意事项
使用like关键字查询时,%号位于首位会导致无法使用索引查询,否则可以正常使用索引查询,如下
多列索引问题
MariaDB [test]> alter table te add index indextwo(id,name); #创建索引
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> show create table te\G;
*************************** 1. row ***************************
Table: te
Create Table: CREATE TABLE `te` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`se` varchar(3) DEFAULT 'man',
`city` varchar(3) DEFAULT 'gx',
KEY `indextwo` (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR: No query specified
MariaDB [test]> explain select * from te where name='ds'; #rows为7
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | te | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
MariaDB [test]> explain select * from te where id=3 and name='ds'; #增加多列索引的首列后,可以正常使用索引
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
| 1 | SIMPLE | te | ref | indextwo | indextwo | 28 | const,const | 1 | Using index condition |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)
MariaDB [test]>
mysql优化查询的更多相关文章
- MySQL优化查询相关
[查询优化相关] 1.如何定位相关慢的查询: a.可以开启慢查询日志,也可以使用show profiles 去记录相关查询到一个临时表再分析. b.show processlist 看看有没有大量等 ...
- MySQL优化查询 5.7版本
1. 变更参数 : query_cache_type 如果何配置查询缓存: query_cache_type 这个系统变量控制着查询缓存工能的开启的关闭.query_cache_type=0时表示关闭 ...
- MySql优化方案
mysql优化方案总结 u Mysql数据库的优化技术 对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索 ...
- 深入理解MySql子查询IN的执行和优化
IN为什么慢? 在应用程序中使用子查询后,SQL语句的查询性能变得非常糟糕.例如: SELECT driver_id FROM driver where driver_id in (SELECT dr ...
- mysql explain的使用(优化查询)
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 1.创建数据库 创建的sql语句如下: /* Navicat MySQL D ...
- mysql使用索引优化查询效率
索引的概念 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度.在没 ...
- mysql数据库添加索引优化查询效率
项目中如果表中的数据过多的话,会影响查询的效率,那么我们需要想办法优化查询,通常添加索引就是我们的选择之一: 1.添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `t ...
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
- MySQL优化技巧之五(mysql查询性能优化)
对于高性能数据库操作,只靠设计最优的库表结构.建立最好的索引是不够的,还需要合理的设计查询.如果查询写得很糟糕,即使库表结构再合理.索引再合适,也无法实现高性能.查询优化.索引优化.库表结构优化需要齐 ...
随机推荐
- day34 线程池 协程
今日内容: 1. 线程的其他方法 2.线程队列(重点) 3.线程池(重点) 4.协程 1.线程的其他方法 语法: Threading.current_thread() # 当前正在运行的线程对象的一个 ...
- 运算类实现 及 GNU Makefile基本结构
1.运算类的实现,代码如下: (1)operator.cpp #include<iostream> #include "operator.h" using names ...
- Java线程的三种方式
创建线程有三种方式: 1.继承Thread类 2.实现Runnable接口 3.使用Callable和Future创建线程 三种方式详解如下: ---------------------------- ...
- matlab中文本文件与图像转化
一 将图片转化为txt文本文件 a=imread('picture.bmp'); //读取picture.bmp图片 b=rgb2gray(a); //由rgb图 ...
- BBbacktrace installation
1: get the installation package https://backtrackbb.readthedocs.io/en/latest/Method.html#overview ht ...
- day 96 关于分页的使用
分页的学习主要四步骤 from django.db import models # Create your models here. class Book(models.Model): title = ...
- intellij怎么导入MySQL的驱动包
1.下载zip格式的驱动包:https://dev.mysql.com/downloads/connector/j/ 2.解压zip,放到任意位置.其中的mysql-connector-java.ja ...
- array_multisort
项目中用到这个函数了 ,起初对这个函数一直是懵逼状态,文档都看的朦朦胧胧的 网上无意间看到这篇文章 ,写的超级详细,收藏了 . 当然要先放原地址:https://www.cnblogs.com/WuN ...
- django面试三
1.Django. Flask.Tornado框架的比较? Django: 对于django,大而全的框架它的内部组件比较多,内部提供:ORM.Admin.中间件.Form.ModelForm.Ses ...
- 莫烦tensorflow(3)-Variable
import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.ad ...