› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
mart1nN
V2EX  ›  MySQL

sql 语句查询效率的问题

  •  
  •   mart1nN · May 2, 2019 · 5621 views
    This topic created in 2710 days ago, the information mentioned may be changed or developed.

    有两个表,score (成绩)和 student (学生),需求是查询学生李四的所有科目的成绩

    select score.c_name, score.grade from score,student where student.name = '李四' and score.stu_id = student.id

    select c_name, grade from score where stu_id= (select id from student where name='李四')

    这两句的查询结果是一样的,请问查询效率哪一条会高一点呢?有关 sql 效率这方面的东西有什么推荐的书 /博客吗?

    6 replies  •  2019-05-03 02:07:42 +08:00
    8FollowHeart
        1
    8FollowHeart  
       May 2, 2019
    1.加上索引 name 和 stu_id 后第一条快
    8FollowHeart
        2
    8FollowHeart  
       May 2, 2019
    第二种建议少用,student 数据少,性能一般
    winoros
        3
    winoros  
       May 2, 2019   ❤️ 1
    这两个 SQL 一个是 inner join 一个是 semi join。
    semi join 因为不能让外表的数据膨胀 所以总是只能拿外表查内表。而 inner join 则既可以拿左表查右表 也可以拿右表查左表。这个 case 的实际意义来看,右表(内表)肯定是小的一边。所以 semi join 会不占优势。
    当然优化器实际是可以让两个 SQL 优化成同样的执行计划,但是 MySQL 可能没有做?具体的就要看文档了
    chitanda
        4
    chitanda  
       May 2, 2019 via Android
    请善用 explain
    akagishigeru
        5
    akagishigeru  
       May 2, 2019 via iPhone
    explain 之后再来问
    mmdsun
        6
    mmdsun  
       May 3, 2019 via Android
    想问连表和子查询哪个更快?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   799 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 22:21 · PVG 06:21 · LAX 15:21 · JFK 18:21
    ♥ Do have faith in what you're doing.