mysql - 如何优化在 130,000 行上运行的联接查询?

标签 mysql performance

我使用以下 SQL 语句:

SELECT
  SUM(t.Points) AS `Points`,
  CONCAT(s.Firstname, " ", s.Surname) AS `Name`
FROM transactions t
INNER JOIN student s
  ON t.Recipient_ID = s.Frog_ID
GROUP BY t.Recipient_ID

运行查询需要21 秒。奇怪的是,即使我LIMIT 0, 30,它仍然需要20.7秒才能运行!

如果我对此语句运行 EXPLAIN,结果如下:

id  select_type table   type    possible_keys   key     key_len     ref     rows        Extra
1   SIMPLE      s       ALL     PRIMARY         NULL    NULL        NULL    877         Using temporary; Using filesort
1   SIMPLE      t       ALL     NULL            NULL    NULL        NULL    135140      Using where

交易采用以下形式:

Transaction_ID  Datetime    Giver_ID    Recipient_ID    Points  Category_ID     Reason
1               2011-09-07  36754       34401           5       6               Gave excellent feedback on the new student noteboo...

transactions 表中有 130,000 行


学生采用以下形式:

Frog_ID UPN             Firstname   Surname     Intake_Year
101234  K929221234567   Madeup      Student     2010

student 表中有 835 行


索引

transactions indexes

student indexes


有什么办法可以让这个查询更高效吗?

最佳答案

你们都使用Recepient_ID加入并按它分组,但它没有索引,所以我认为这就是问题所在。

尝试添加 transactions.Recepient_ID 作为索引。

关于mysql - 如何优化在 130,000 行上运行的联接查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479525/

相关文章:

使用 Big O(n log n) 或 Big O(log n) 复杂度的 Java 程序

php - 循环遍历 MySQL 行并拆分。最初是 Excel 电子表格

python - 以特定方式删除 mysqldb 中的重复行

jquery - 在 jQuery 中选择后代元素最快的方法是什么?

ruby - 在 Ruby 中,是否有必要内存一个返回常量值/对象的方法?

performance - 基数排序的最佳基础

php - 从输出中删除\r\n

mysql - 多列计数

MySql - 如何从另一个表插入数据?

python - 非零值的 Numpy 总和运行长度