mysql 查询和性能

发布于 2024-11-09 12:42:22 字数 478 浏览 0 评论 0原文

我想知道如果在以下条件下运行此查询对性能的影响。

查询:

select   `players`.*, count(`clicks`.`id`) as `clicks_count` 
from     `players` left join `clicks` on `clicks`.`player_id` = `players`.`id`
group by `players`.`id`
order by `clicks_count` desc 
limit    1

条件:

  1. 在点击表中我期望得到 1 分钟内插入 1000 次
  2. clicks 表将包含更多 那么 1,000,000 行
  3. 玩家表将包含 10,000 行
  4. 玩家表每 5 行插入一次 分钟

我想知道如果我在 1 分钟内运行查询 1000 次,性能方面会发生什么。

谢谢

I would like to know the impact on performance if I run this query in the following conditions.

Query:

select   `players`.*, count(`clicks`.`id`) as `clicks_count` 
from     `players` left join `clicks` on `clicks`.`player_id` = `players`.`id`
group by `players`.`id`
order by `clicks_count` desc 
limit    1

Conditions:

  1. In the clicks table I expect to get
    insert 1000 times in a 1 minute
  2. The clicks table will contain more
    then 1,000,000 rows
  3. The players table will contain
    10,000 rows
  4. The players table get inserted into every 5
    minutes

I would like to know what to expect performance-wise if I run the query 1000 times in 1 minute.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-11-16 12:42:22

对于表中任何有意义的数据量,该查询将永远在几毫秒内运行。它将运行两次全表扫描,将两者连接在一起,聚合混乱的数据,然后从中获取顶行。

使用触发器将总计存储在播放器中,并为该字段建立索引。然后您将能够完全避免连接:

select p.* from players p order by clicks_count desc limit 1

That query will never run in milliseconds with any meaningful amounts of data in your tables. It'll run two full table scans, join the two together, aggregate the mess, and fetch the top row from that.

Use a trigger to store the total in the players, and index that field. You'll then be able to avoid the join altogether:

select p.* from players p order by clicks_count desc limit 1
千紇 2024-11-16 12:42:22

第一&最重要的是,如果您希望在这么多记录和频繁写入的情况下获得良好的性能,那么您应该担心您的架构;即,如果尚未到位,则必须创建适当的索引和约束。

接下来,查询本身,选择所需的最小字段数(因此,如果不需要所有玩家字段,请避免使用“players.*”)。

个人偏好,我会重组表(例如用playerID代替id)并像这样查询:

SELECT p.*, COUNT(c.id) as clicks_count
FROM players p
JOIN clicks c USING(playerID)
GROUP BY p.playerID
ORDER BY clicks_count desc 
LIMIT 1

再次,看看你是否真的需要所有玩家表字段;如果不是,则省略“p.*”并替换为 p.foo、p.bar 等。

First & foremost, you should worry about your schema if you want decent performance with that number of records and frequent writes; i.e. proper indexes and constraints must be created if not already in place.

Next, the query itself, select the minimum number of fields needed (so if you do not need ALL players field, avoid using "players.*").

Personal pref, I'd restructure tables (e.g. playerID in place of id) and query like so:

SELECT p.*, COUNT(c.id) as clicks_count
FROM players p
JOIN clicks c USING(playerID)
GROUP BY p.playerID
ORDER BY clicks_count desc 
LIMIT 1

Again, see if you really need ALL player table fields; if not, omit "p.*" and replace with p.foo, p.bar, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文