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主键+大表为基准!

相关推荐
ANGLAL18 分钟前
17.MyBatis动态SQL语法整理
java·sql·mybatis
懒羊羊不懒@21 分钟前
【MySQL | 基础】通用语法及SQL分类
数据库·oracle
llxxyy卢27 分钟前
基于时间的 SQL 盲注-延时判断和基于布尔的 SQL 盲注
数据库·sql·oracle
Yeats_Liao1 小时前
时序数据库系列(六):物联网监控系统实战
数据库·后端·物联网·时序数据库
珊珊而川2 小时前
MAC-SQL:黄金标准错误
数据库·sql
一 乐3 小时前
智慧党建|党务学习|基于SprinBoot+vue的智慧党建学习平台(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·学习
许愿OvO4 小时前
MySQL触发器
android·mysql·adb
lcanfly4 小时前
Mysql作业4
数据库·mysql
蓝象_5 小时前
docker安装配置mysql
mysql·docker·容器
喜欢踢足球的老罗5 小时前
认证与授权:详解大型系统中用户中心与RBAC的共生关系
数据库·rbac