MySQL系列---sql优化

目录标题

join时以大表为基表

  • 查不动

    sql 复制代码
    explain  select u.id as useId, u.open_id as userOpenId, u.name as userName, u.phone as userPhone, s.id as shopId, s.shop_id as shopNum, s.shop_name as shopName from jml_mp_wechatuser u right join jml_shop s on u.id = shopkeeper_id where u.phone = '6D26B768750A90426AAA4CE04FA4CCBB'
  • 查不动

    sql 复制代码
    EXPLAIN select u.id as useId, u.open_id as userOpenId, u.name as userName, u.phone as userPhone, s.id as shopId, s.shop_id as shopNum, s.shop_name as shopName from jml_shop s LEFT JOIN jml_mp_wechatuser u on u.id = s.shopkeeper_id where u.phone = '6D26B768750A90426AAA4CE04FA4CCBB'
  • 5s

    sql 复制代码
    EXPLAIN select u.id as useId, u.open_id as userOpenId, u.name as userName, u.phone as userPhone, s.id as shopId, s.shop_id as shopNum, s.shop_name as shopName from jml_shop s LEFT JOIN jml_mp_wechatuser u on u.open_id = s.shopkeeper_openid where u.phone = '6D26B768750A90426AAA4CE04FA4CCBB'
  • 3s

    sql 复制代码
    explain  select u.id as useId, u.open_id as userOpenId, u.name as userName, u.phone as userPhone, s.id as shopId, s.shop_id as shopNum, s.shop_name as shopName from jml_mp_wechatuser u left join jml_shop s on u.id = shopkeeper_id where u.phone = '6D26B768750A90426AAA4CE04FA4CCBB'
  • 4s

    sql 复制代码
    explain  select u.id as useId, u.open_id as userOpenId, u.name as userName, u.phone as userPhone, s.id as shopId, s.shop_id as shopNum, s.shop_name as shopName from jml_mp_wechatuser u left join jml_shop s on u.open_id = s.shopkeeper_openid where u.phone = '6D26B768750A90426AAA4CE04FA4CCBB'

A left join B = B right join A; join的原理是以基准表为底,根据on的条件for循环去另一张表找到等值进而关联。所以显而易见,影响join效率的是等值的长度及for循环次数。以大表为基准可以有效减少for循环次数,选取主键关联可以更快的判断等值。

如何join? on主键+大表为基准!

相关推荐
极限实验室2 分钟前
Elasticsearch 备份:snapshot 镜像使用篇
数据库·elasticsearch
武子康42 分钟前
Java-145 深入浅出 MongoDB 基本操作详解:数据库查看、切换、创建集合与删除完整教程
java·数据库·sql·mysql·mongodb·性能优化·系统架构
阿巴~阿巴~1 小时前
Centos 7/8 安装 Redis
linux·服务器·数据库·redis·centos
刘大猫.1 小时前
mysql数据库压缩
数据库·mysql·压缩·mysql数据库压缩·数据库压缩·数据库备份与压缩
oracle04061 小时前
sql练习题单-知识点总结
数据库·sql
会飞的架狗师1 小时前
【MySQL体系】第4篇:MySQL 查询优化实用技巧
数据库·mysql
lypzcgf2 小时前
Coze源码分析-资源库-编辑数据库-前端源码-核心组件
前端·数据库·源码分析·coze·coze源码分析·ai应用平台·agent平台
wei_shuo2 小时前
KingbaseES聚焦产品上线
数据库·kingbasees
难以触及的高度2 小时前
Linux-CentOS 7 上安装 MySQL 8.0.43(保姆级教程)
linux·mysql·centos
AI浩2 小时前
Redis中的RPOP、BRPOP、LPOP 和 BLPOP
数据库·chrome·redis