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

相关推荐
auspicious航11 分钟前
PostgreSQL的备份与恢复
数据库·postgresql
这个DBA有点耶37 分钟前
从 MySQL 到时序数据库:我帮朋友做迁移的完整记录
数据库·架构·dba
泡海椒44 分钟前
深度解析JQuick-SQL AST到逻辑计划转换机制|分布式SQL原理
数据库·分布式·sql
心静财富之门1 小时前
Python 数据共享四种方案完整实例(同进程 / JSON / Redis / HTTP)
数据库·python·oracle
略略略咯咯1 小时前
啊啊啊啊啊
java·服务器·数据库
奇牙coding1 小时前
GPT-5.5 报 401 invalid_api_key 怎么办?换密钥前按 4 层排查请求头、项目权限和端点
数据库·gpt·ios·ai
IvorySQL1 小时前
PostgreSQL 日报| WAL summarizer 断言失败(10 月 26 日)
数据库·postgresql·区块链
王仔小奶1 小时前
慢SQL治理与性能调优:DBA如何用Shell/Python提升运维效率?
python·sql·dba
这个DBA有点耶2 小时前
2026年国产数据库“实际用户增长最快”核验指南:口径优先级、可查数据与选型动作清单
数据库·mysql·架构
程序员塔塔喵2 小时前
【MySQL篇|第三篇】脏读,幻读,不可重复读
数据库·mysql·不可重复读·幻读·脏读