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

相关推荐
FeBaby21 分钟前
mysql为什么使用b+树不使用红黑树
数据库·b树·mysql
ZHZCE1 小时前
在 Ubuntu 20.04 上安装 MySQL 8.0
mysql
令狐少侠20111 小时前
如何使用navicat连接容器中的mysql数据库
mysql·docker
青草地溪水旁1 小时前
`mysql_query()` 数据库查询函数
数据库·mysql·c
玩转数据库管理工具FOR DBLENS1 小时前
精准测试的密码:解密等价类划分,让Bug无处可逃
数据库·单元测试·测试用例·bug·数据库开发
AAA修煤气灶刘哥1 小时前
踩完 10 个坑后,我把多表查询 + MyBatis 动态 SQL 写成了干货
java·数据库·后端
怕浪猫2 小时前
MySQL 多表查询的应用
sql·mysql
秦jh_2 小时前
【MySQL】基本查询
linux·数据库·c++·mysql
烟雨归来3 小时前
升级openssh后ORACLE RAC EM 安装失败处理
数据库·oracle
TDengine (老段)3 小时前
TDengine IDMP 应用场景:电动汽车
大数据·数据库·物联网·ai·时序数据库·iot·tdengine