sql 优化基础篇

1. 筛选字段上没有加索引的话,查找存在:用limit 1 代替  count(*)

  优化前方案:select count(*) as  cnt  from  表名  where 顾虑条件

 优化后方案:  select 1 as  cnt  from  表名  where 顾虑条件 limit 1

说明:不用 count,而是用 limit 1,当数据库查到一条就返回,不要再继续查找多少条了

sql 如下:



select * from  boys;
# +--+----+---+---+
# |id|name|sex|age|
# +--+----+---+---+
# |1 |慕容皝 |男  |22 |
# |2 |慕容冲 |男  |33 |
# |3 |慕容垂 |男  |44 |
# |4 |慕容博 |男  |55 |
# +--+----+---+---+
# 普通的sql实现
# 用count 实现

select count(*) from  boys where name = '慕容博';
# +--------+
# |count(*)|
# +--------+
# |1       |
# +--------+
# 在name 上加上索引后,查看执行计划
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+
# |id|select_type|table|partitions|type|possible_keys  |key            |key_len|ref  |rows|filtered|Extra      |
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+
# |1 |SIMPLE     |boys |NULL      |ref |boys_name_index|boys_name_index|83     |const|1   |100     |Using index|
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+

# 不加索引 查看执行计划(全表扫描,扫了全部表的四行数据)
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type|possible_keys|key |key_len|ref |rows|filtered|Extra      |
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |1 |SIMPLE     |boys |NULL      |ALL |NULL         |NULL|NULL   |NULL|4   |25      |Using where|
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+


#  不存在
select count(*) from  boys where name = '乔峰';
# +--------+
# |count(*)|
# +--------+
# |0       |
# +--------+

# 优化方案  用 select 1   where  limit 代替
select 1 as cnt from boys where name = '慕容博' limit 1;
# +---+
# |cnt|
# +---+
# |1  |
# +---+
# name上加索引后查看执行计划
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+
# |id|select_type|table|partitions|type|possible_keys  |key            |key_len|ref  |rows|filtered|Extra      |
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+
# |1 |SIMPLE     |boys |NULL      |ref |boys_name_index|boys_name_index|83     |const|1   |100     |Using index|
# +--+-----------+-----+----------+----+---------------+---------------+-------+-----+----+--------+-----------+

# name上不加索引后查看执行计划
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type|possible_keys|key |key_len|ref |rows|filtered|Extra      |
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |1 |SIMPLE     |boys |NULL      |ALL |NULL         |NULL|NULL   |NULL|4   |25      |Using where|
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+

# 问题:如果查询  rownum 为 1 的数据:即 id 为 1 ,name为 慕容皝 的数据
 select 1 as cnt from boys where name = '慕容皝' limit 1;
# 查看执行计划 (name 未加索引)
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |id|select_type|table|partitions|type|possible_keys|key |key_len|ref |rows|filtered|Extra      |
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+
# |1 |SIMPLE     |boys |NULL      |ALL |NULL         |NULL|NULL   |NULL|4   |25      |Using where|
# +--+-----------+-----+----------+----+-------------+----+-------+----+----+--------+-----------+

 

2.  用 not exists 代替 not in (exitst 代替in)

没有女朋友的男生:sql 

not in 

select * from boys where id not in (select boy_id from girls) ;

 

  

select * from boys where not exists (select 1 from girls where girls.boy_id = boys.id) ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值