简体   繁体   English

MySQL:如何在单个查询中查询多个“统计信息”?

[英]MySQL: How to query multiple 'statistics' in a single query?

This is more of a question rather than a problem that I need to solve. 这更像是一个问题,而不是我需要解决的问题。 My backend is fast and my queries running great, so it's not that important. 我的后台是快速和我的查询运行很好,所以它不是那么重要。 Okay, but let's get to it. 好的,但是让我们开始吧。

I have 4 panels of statistics on my dashboard regarding the number of views from today, yesterday, this week and this month; 我的仪表板上有4个统计小组,关于今天,昨天,本周和本月的观看次数; each taking up one query in my database. 每个人在我的数据库中占用一个查询。 What I was wondering is, how would one put all those queries together to ease up the load on the database/server? 我想知道的是,如何将所有这些查询放在一起以减轻数据库/服务器上的负载?

I was looking through Stackoverflow before asking and saw one saying something like: 我在询问之前正在查看Stackoverflow,看到一个人说的话:

SUM(case when status = 'open' then 1 else 0 end) as [Open],
SUM(case when status = 'closed' then 1 else 0 end) as [Closed]

Source: Gathering multiple statistics about a table in a single query 来源: 在单个查询中收集有关表的多个统计信息

Which could be what I need, could be something like: 这可能是我需要的,可能是这样的:

SUM(case when DATE(created_at) = '2015-07-23' then 1 else 0 end) as today,
SUM(case when DATE(created_at) = '2015-07-22' then 1 else 0 end) as yesterday,
SUM(case when WEEK(created_at) = '29' then 1 else 0 end) as week,
SUM(case when MONTH(created_at) = '7' then 1 else 0 end) as month

I was just wondering if anyone has some better suggestions, as I have applied this to my function and it works just fine. 我只是想知道是否有人有更好的建议,因为我已将此应用于我的功能,它工作得很好。

The comments are giving good hints. 评论提供了很好的提示。 I'll provide here another idea, that conceptually is used in most of the banks I've been working with. 我将在这里提供另一个想法,概念上用于我一直在使用的大多数银行。

When there are billions of rows, and you do not need exact instant snapshots for every request (meaning: you have a tolerance for slightly outdated data), it is worth exploring batch processes. 如果有数十亿行,并且您不需要为每个请求提供准确的即时快照(意味着:您对稍微过时的数据有容忍度),则值得探索批处理过程。

This is how it works: 这是它的工作原理:

  1. You define your lag tolerance: for example: "I'm ok with data outdated 8 hours". 您可以定义滞后容差:例如:“我可以将数据过时8小时”。 This is the periodicity of your batch process. 这是批处理的周期性。
  2. You denormalize the database to add a "redundant" column/table storing running totals of your choice. 您对数据库进行非规范化以添加存储您选择的运行总计的“冗余”列/表。 For example, you would add a table called statistics_snapshot with 4 columns: ( timestamp , month , day , week ) or something of the like. 例如,您将添加一个名为statistics_snapshot的表,其中包含4列:( timestampmonthdayweek )或类似的内容。
  3. You create a stored procedure in mysql that fills up this table with your 4 queries, or with one global queries as you suggested. 您在mysql中创建一个存储过程,用您的4个查询填充此表,或者按照您的建议填充一个全局查询。 A timestamp is also registered so you know when it was taken. 时间戳也会被注册,因此您知道它何时被拍摄。
  4. You create a user with EXECUTE grant for that procedure, ONLY . 为该过程创建一个具有EXECUTE授权的用户。

     CREATE USER 'cron_mysql_user'@'localhost' IDENTIFIED BY 'strongpassword'; GRANT EXECUTE ON PROCEDURE db_name.proc_name TO 'cron_mysql_user'@'localhost'; 
  5. You use 你用 DBMS_JOB a cron job to connect mysql and run this procedure with periodicity defined in point #1. 连接mysql并使用第1点中定义的周期运行此过程的cron作业。 Usually you can run scripts from the command line like this: 通常,您可以从命令行运行脚本,如下所示:

     mysql --user='cron_mysql_user'@'localhost' -pstrongpassword --execute="call proc_name()" db_name 
  6. You create nice reports based on your periodic snapshots :-) 您可以根据定期快照创建漂亮的报告:-)

The advantage of doing so is that you centralized I/O a few times a day only, a controlled manner, so you only have a very light SELECT statement to do when you need to know the statistics. 这样做的好处是,您每天只能以受控方式集中I / O几次,因此当您需要了解统计信息时,您只需要执行非常轻松的SELECT语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM