【问题标题】:MySQL Join PerformanceMySQL 连接性能
【发布时间】:2012-07-01 11:38:18
【问题描述】:

以下查询在 17 秒内在视图中执行。有 450,000 行。我在连接的两列上有一个索引,它们是 FK。连接列是 BIGINTS。有没有办法让这家伙加速?

SELECT c.id, sce.user_id
FROM sims_classroom c
JOIN sims_class_enrollment sce ON c.id = sce.classroom_id 

解释

'1', 'SIMPLE', 'c', 'index', 'PRIMARY', 'PRIMARY', '8', NULL, '211213', 'Using index'
'1', 'SIMPLE', 'sce', 'ref', 'fk_class_enrollment_classroom_id', 'fk_class_enrollment_classroom_id', '9', 'ngsp.c.id', '1', 'Using where'

sims_classroom = 200100
sims_class_enrollment = 476396

【问题讨论】:

  • 在您的查询中使用EXPLAIN,并将输出结果提供给我们。
  • sims_classroomsims_class_enrollment 也有多少行
  • 您也可以尝试分析您的查询吗? SET PROFILING = 1; SELECT ...; SHOW PROFILE FOR QUERY 1; SET PROFILING = 0;

标签: mysql performance join


【解决方案1】:

它会稍微减慢写入速度,但由于您的索引中只缺少一列所需的所有内容,因此我将为 sce 做一个两列索引:

classroom_id, user_id

这将导致 mysql 甚至不需要转到实际表(在说明中两者都是“使用索引”)。

【讨论】:

    猜你喜欢
    • 2016-09-11
    • 2018-05-09
    • 2015-07-29
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多