- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个我似乎无法解决的 MySQL 问题。为了能够快速执行用于报告目的的 GROUP BY 查询,我已经将几个表非规范化为以下内容(该表由其他表上的触发器维护,我已经同意了与此):
DROP TABLE IF EXISTS stats;
CREATE TABLE stats (
`id` int(11) NOT NULL AUTO_INCREMENT,
`datetime` datetime NOT NULL,
`datetime_hour` datetime NOT NULL,
`datetime_day` datetime NOT NULL,
`step_id` int(11) NOT NULL,
`check_id` int(11) NOT NULL,
`probe_id` int(11) NOT NULL,
`execution_step_id` int(11) NOT NULL,
`value_of_interest` int(11) DEFAULT NULL,
`internal` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `index_stats_on_execution_step_id` (`execution_step_id`),
CONSTRAINT `stats_step_id_fk` FOREIGN KEY (`step_id`) REFERENCES `steps` (`id`) ON DELETE CASCADE,
CONSTRAINT `stats_check_id_fk` FOREIGN KEY (`check_id`) REFERENCES `checks` (`id`) ON DELETE CASCADE,
CONSTRAINT `stats_probe_id_fk` FOREIGN KEY (`probe_id`) REFERENCES `probes` (`id`) ON DELETE CASCADE,
CONSTRAINT `stats_execution_step_id_fk` FOREIGN KEY (`execution_step_id`) REFERENCES `execution_steps` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
无论我在表上放置什么索引,以下查询最终仍将使用 Using where; 进行解释;使用临时的;使用 filesort
或它们的任意组合(这些都会导致查询以 Not Acceptable 性能运行):
SELECT
datetime_day,
step_id,
CAST(AVG(value_of_interest) AS UNSIGNED) AS value_of_interest
FROM
stats
WHERE
check_id = 78
AND probe_id = 1
AND (datetime_day >= '2014-03-28 15:58:00' AND datetime_day <= '2014-10-28 15:58:00')
AND (internal = 0)
GROUP BY
datetime_day, step_id
ORDER BY
datetime_day, step_id
我需要在表定义中设置哪些索引和/或我需要如何修改查询才能以合理的查询执行计划执行?
环境规范:
Fedora 版本 19(薛定谔的猫)
mysql Ver 15.1 Distrib 5.5.34-MariaDB,适用于 Linux (x86_64),使用 readline 5.1
非常感谢您的帮助!
PS:第一次发帖,对于任何违反最佳实践的行为表示歉意。我很高兴学习...
编辑:
其中一个答案建议
ALTER TABLE `stats` ADD INDEX newindex (check_id, probe_id, internal, datetime_day, step_id);
这稍微改善了情况。我之前已经尝试过这个索引并得到以下结果:
+------+-------------+---------------------------+-------+---------------+----------+---------+------+--------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------------------------+-------+---------------+----------+---------+------+--------+------------------------------------+
| 1 | SIMPLE | stats | range | newindex | newindex | 17 | NULL | 605682 | Using index condition; Using where |
+------+-------------+---------------------------+-------+---------------+----------+---------+------+--------+------------------------------------+
但是不应该有一种方法可以使用“松散/紧密索引扫描”执行查询,如 link 中提到的那样?不过,我似乎无法让它工作,而且我不确定我是否正确理解了提到的文章。
最佳答案
您有 600K 行需要扫描,因此它无法立即运行。
为什么需要CAST(AVG(value_of_interest) AS UNSIGNED)
?可以通过在插入之前清理数据来避免这种情况吗?
该索引将使其“使用索引”,这将使其更快。但是,如果这不是您唯一的查询,那么添加它似乎很愚蠢。
INDEX newindex (check_id, probe_id, internal, datetime_day, step_id, value_of_interest)
开始/结束时间不正常有什么原因吗? (15:58:00)
汇总数据仓库表的“真正”解决方案是构建和维护“汇总表”。对于相关查询,这样的表将包含 check_id、probe_id、internal、step_id、datetime_hour、SUM(value_of_interest)、COUNT(*)。前 5 个是主键。您每小时都会向表中添加另一行。报告(小时、天、周、月)将通过 SUM(sums)/SUM(counts) 获得 AVG。
更多讨论my Summary Table blog .
关于mysql - 优化 'GROUP BY'-查询,消除 'Using where; Using temporary; Using filesort',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30077213/
我想编写一个 linq 表达式,该表达式将返回不包含特定值的 ID。例如,我想返回所有不具有 Value = 30 的不同 ID。 ID, Value 1, 10 1, 20 1, 30 2,
我正在尝试使用 Regexp 匹配 Nmap 命令的输出。可以有两种不同的格式。 第一种格式(当 nmap 可以找到主机名时) Nmap scan report for 2u4n32t-n4 (192
我正在 Visual Studio 2012 上使用 C# 开发一个软件。我使用 MySQL Connector 6.9.1 进行 MySQL 连接。我的软件在我的操作系统(Win8 x64)上运行顺
在 Django 中(使用 django.contrib.auth 时)我可以添加一个 Group到另一个 Group ?即一个Group成为另一个成员(member) Group ? 如果是这样,我
我试图通过使用动态组参数对数据进行分组来循环。 我们可以在循环的 WHERE 条件上使用动态查询,但我不知道是否可以在组条件中使用动态字符串。 以下是用户决定按哪个字段分组,然后根据决定放置其他逻辑的
我有这样的字符串 s = 'MR1|L2-S1x' 模式总是相同的:一个或两个字符,在 [|.+:x-] 中可选地后跟一个数字和一个分隔符。此模式可以重复 6 次。 所以匹配模式很明确。 p = r'
我有一个带有时间戳字段“bar”的表“foo”。如何仅获取查询的最旧时间戳,例如: SELECT foo.bar from foo?我尝试执行以下操作: SELECT MIN(foo.bar) fro
在我的 Django 项目中,我有一个 user_manage 应用程序。 我在 user_manage 应用的 model.py 中创建了一个名为 UserManage 的模型: from djan
所以我有这样的输入: 还有一个模板指令,例如: 看来我只获得了 foo 和 bar 的组。 (为什么?我预计我可能会得到第三组 current-group-key() = '')。
我正在尝试扩展 django.contrib.auth 并遇到将用户添加到组中的情况,这可以通过两种方式完成。我只是想知道为什么会这样,以及其中一种相对于另一种的优势是什么。 最佳答案 他们做完全相同
我使用的是旧的 PHP 脚本,并且此查询有错误。由于我没有使用 mysql 的经验,因此无法修复它。 "SELECT COUNT(p.postid) AS pid, p.*, t.* FROM ".T
我有几行 Objective-C 代码,例如: ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddre
我正在使用 MariaDB IMDB 电影数据集,我试图解决以下问题。电影表包含 id、名称、排名和年份列 A decade is a sequence of 10 consecutive years
让我从数据开始,以便更好地描述我的需求。我有一个名为 SUPERMARKET 的表,其中包含以下字段: Field 1: StoreID Field 2: ProductCategory Field
你好我有这个查询: SELECT DISTINCT a.id, a.runcd, (SELECT SUM(b.CALVAL) FROM GRS b WHERE b.PCode=11000 AND a.
我想在 xquery 中使用 Group By。有人可以告诉我如何在 Marklogic 中使用 Group By 吗? 最佳答案 或者,您可以使用 xdmp:xslt-invoke 调用 XSLT或
因此,当通过 from sequelize 请求组时,如下所示: return models.WorkingCalendar .findAll({
我希望我解释正确。 我有 2 个表,有 第一个表(table1) +------------+------+-------+-------+ | Date | Item | Block |
我的表 MYTABLE 有 2 列:A 和 B 我有以下代码片段: SELECT MYTABLE.A FROM MYTABLE HAVING SUM(MYTABLE.B) > 100
我有一个简单的行分组查询,需要 0.0045 秒。 300.000 行 从表 GROUP BY cid 中选择 cid 当我添加 MAX() 进行查询时,需要 0.65 秒才能返回。 从表 GROUP
我是一名优秀的程序员,十分优秀!