使 LEFT JOIN 查询更高效

Make LEFT JOIN query more efficient

以下使用 LEFT JOIN 的查询占用了太多内存 (~4GB),但主机只允许此进程使用大约 120MB。

SELECT grades.grade, grades.evaluation_id, evaluations.evaluation_name, evaluations.value, evaluations.maximum FROM grades LEFT JOIN evaluations ON grades.evaluation_id = evaluations.evaluation_id WHERE grades.registrar_id = ?

为成绩创建 table 语法:

  CREATE TABLE `grades` (
  `grade_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `evaluation_id` int(10) unsigned DEFAULT NULL,
  `registrar_id` int(10) unsigned DEFAULT NULL,
  `grade` float unsigned DEFAULT NULL,
  `entry_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`grade_id`),
  KEY `registrarGrade_key` (`registrar_id`),
  KEY `evaluationKey` (`evaluation_id`),
  KEY `grades_id_index` (`grade_id`),
  KEY `eval_id_index` (`evaluation_id`),
  KEY `grade_index` (`grade`),
  CONSTRAINT `evaluationKey` FOREIGN KEY (`evaluation_id`) REFERENCES `evaluations` (`evaluation_id`),
  CONSTRAINT `registrarGrade_key` FOREIGN KEY (`registrar_id`) REFERENCES `registrar` (`reg_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1627 DEFAULT CHARSET=utf8;

评价table:

CREATE TABLE `evaluations` (
      `evaluation_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `instance_id` int(11) unsigned DEFAULT NULL,
      `evaluation_col` varchar(255) DEFAULT NULL,
      `evaluation_name` longtext,
      `evaluation_method` enum('class','email','online','lab') DEFAULT NULL,
      `evaluation_deadline` date DEFAULT NULL,
      `maximum` int(11) unsigned DEFAULT NULL,
      `value` float DEFAULT NULL,
      PRIMARY KEY (`evaluation_id`),
      KEY `instanceID_key` (`instance_id`),
      KEY `eval_name_index` (`evaluation_name`(3)),
      KEY `eval_method_index` (`evaluation_method`),
      KEY `eval_deadline_index` (`evaluation_deadline`),
      KEY `maximum` (`maximum`),
      KEY `value_index` (`value`),
      KEY `eval_id_index` (`evaluation_id`),
      CONSTRAINT `instanceID_key` FOREIGN KEY (`instance_id`) REFERENCES `course_instance` (`instance_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=utf8;

Php 拉取数据的代码:

$sql = "SELECT grades.grade, grades.evaluation_id, evaluations.evaluation_name, evaluations.value, evaluations.maximum FROM grades LEFT JOIN evaluations ON grades.evaluation_id = evaluations.evaluation_id WHERE grades.registrar_id = ? AND YEAR(entry_date) = YEAR(CURDATE())";
                    $result = $mysqli->prepare($sql);
                    if($result === FALSE)
                        die($mysqli->error);
                    $result->bind_param('i',$reg_ids[$i]);
                    $result->execute();
                    $result->bind_result($grade, $eval_id, $evalname, $evalval, $max);
                    while($result->fetch()){ 

以及致命错误消息

有没有办法大幅减少此查询的内存负载?

谢谢!

  1. 奇怪的是,更改 MySQL 查询并没有更改尝试分配的内存量

请为两个 table 提供 SHOW CREATE TABLE;我想看看你有没有这样的东西:

成绩:INDEX(registration_id)

评价:PRIMARY KEY(evaluation_id)

编辑

您现在在两个 table 中都有冗余索引 -- 可能是因为我的建议。也就是说,您已经拥有有助于查询的两个索引。

由于您有 LONGTEXT 并且它正在尝试分配恰好 4GB,即 LONGTEXT 的最大大小,我想这就是问题所在。建议您将该列更改为 TEXT(最大 64KB)或 MEDIUMTEXT(最大 16MB)。我之前在 PHP 中从未见过这种行为,但后来我很少使用比 TEXT 更大的东西。