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

相关推荐
笑梦无境几秒前
mysql基础篇二(多年前整理)
数据库·mysql
cqsztech2 分钟前
基于ORACLE LINUX 10.1 MYSQL 8.4 源码安装
linux·mysql·oracle
乐之者v2 分钟前
mysql 的查询条件放在 join on 和 Where 的区别:
mysql
艾伦_耶格宇6 分钟前
【zabbix】-2 zabbix本地部署
数据库·zabbix
麻花201311 分钟前
Oracle 数据泵导出与还原操作指南
数据库·oracle
卤炖阑尾炎13 分钟前
MySQL 全量、增量备份与恢复实战指南
mysql·adb
邂逅you18 分钟前
SQL温故与知新
数据库·sql
zhangren0246820 分钟前
Laravel7.x新特性全面解析
数据库·mysql·adb·php
鸽芷咕24 分钟前
Oracle替换工程实践深度解析:金仓数据库破解PL/SQL“零改造”迁移难题
数据库·sql·oracle
猿小喵24 分钟前
MySQL数据库参数解读-第一篇
数据库·mysql·性能优化