gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:30:09 25 4
gpt4 key购买 nike

我正在使用以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com