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

相关推荐
DROm RAPS几秒前
SQL 实战:复杂数据去重与唯一值提取
前端·数据库·sql
m0_737539373 分钟前
MYSQL源码安装和备份
数据库·mysql·adb
214396510 分钟前
golang如何使用expvar暴露运行时指标_golang expvar运行时指标暴露步骤
jvm·数据库·python
翻斗包菜18 分钟前
【MongoDB 从入门到实战:安装、配置、CRUD、权限、备份恢复全教程】
数据库·mongodb
weixin_4249993623 分钟前
如何用SQL按条件计算移动求和_结合CASE与窗口函数
jvm·数据库·python
214396524 分钟前
持久化存储如何适配不同浏览器?解决隐私模式下存储失败的指南
jvm·数据库·python
justjinji25 分钟前
c++怎么读取大端序设备的固件bin文件_字节反转与位移操作【详解】
jvm·数据库·python
m0_5150984230 分钟前
如何处理视图中的Definer_视图创建者权限变更对视图有效性的影响
jvm·数据库·python
2401_8836002531 分钟前
如何创建物化视图_CREATE MATERIALIZED VIEW基本语法与数据填充
jvm·数据库·python
xxjj998a32 分钟前
MySQL无法连接到本地localhost的解决办法2024.11.8
数据库·mysql·adb