SQL:学习SQL优化

学习 

1.语句

2.原则(三条快速记忆)

3.常见查询类型

试验

本次试验采用SQL表中的world 数据库中city表来试验

1.查询方法

explain SELECT * FROM city where ID>500 limit 10;
#1.all查询,主要是因为查询的键不是District,而且key中无数据
explain select `Name`,CountryCode,District from city where District='Utrecht';
#2.ref查询,键为CountryCode,key主要依存索引名称
explain select `Name`,CountryCode,District from city where CountryCode='NLD';
#3.const查询,常量
explain select `Name`,CountryCode,District from city where ID=5;
#4.range查询,范围查询
explain select `Name`,CountryCode,District from city where ID<5;

2.SQL优化

1.模糊查询优化

#基础语言
select `Name`,CountryCode,District from city where CountryCode like '%LD';
#错误查询,不走索引,查询开头是%,_
explain select `Name`,CountryCode,District from city where CountryCode like '%LD';
explain select `Name`,CountryCode,District from city where CountryCode like '_LD';
#正确查询
explain select `Name`,CountryCode,District from city where CountryCode like 'NL%';
explain select `Name`,CountryCode,District from city where CountryCode like 'NL_';

2.in优化

#基础语言
select `Name`,CountryCode,District from city where CountryCode in('NLD','PHL');
#基础in查询,缺点:慢
explain select `Name`,CountryCode,District from city where CountryCode in('NLD','PHL');
#or查询,稍快
explain select `Name`,CountryCode,District from city where CountryCode='NLD' or CountryCode='PHL';
#union联合查询,性能最快的方法,输出三条数据
explain
select `Name`,CountryCode,District from city where CountryCode='NLD'
union
select `Name`,CountryCode,District from city where CountryCode='PHL';

相关推荐

  1. sql优化学习笔记整理

    2024-05-25 23:22:23       36 阅读
  2. <span style='color:red;'>Sql</span><span style='color:red;'>优化</span>

    Sql优化

    2024-05-25 23:22:23      27 阅读
  3. SQL优化

    2024-05-25 23:22:23       27 阅读
  4. SQL优化

    2024-05-25 23:22:23       20 阅读
  5. sql优化

    2024-05-25 23:22:23       12 阅读
  6. <span style='color:red;'>SQL</span><span style='color:red;'>优化</span>

    SQL优化

    2024-05-25 23:22:23      8 阅读
  7. SQL 优化

    2024-05-25 23:22:23       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-25 23:22:23       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-25 23:22:23       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-25 23:22:23       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-25 23:22:23       18 阅读

热门阅读

  1. 【Muduo】网络库框架模型和各模块简介

    2024-05-25 23:22:23       12 阅读
  2. C# 中的 Dictionary<TKey, TValue> 类

    2024-05-25 23:22:23       14 阅读
  3. docker system prune命令详解

    2024-05-25 23:22:23       11 阅读
  4. MySql开源闪回工具MyFlash

    2024-05-25 23:22:23       12 阅读
  5. 使用Python从网站API下载视频并转换为MP4文件

    2024-05-25 23:22:23       10 阅读
  6. Pytorch-03 数据集与数据加载器

    2024-05-25 23:22:23       10 阅读