我有一个MYSQL数据库,其中有两个表A和B,它们都有一个StartDateTime和EndDateTime列(它们是DATETIME变量)。
然而,表B获得的更多条目(持续时间更短)比A.现在,表A有11000行,而表B有84000行。
当我尝试加入2个表(共享索引)时,查询速度很慢。
有没有办法将这两个表与一个不会永远存在的查询联系起来?
以下是表A日期时间的前20行:
+---------------------+---------------------+
| StartDateTime | endDateTime |
+---------------------+---------------------+
| 2013-10-04 13:48:11 | 2013-10-04 14:10:23 |
| 2013-10-04 13:48:19 | 2013-10-04 13:48:29 |
| 2013-10-04 13:48:28 | 2013-10-04 14:10:06 |
| 2013-10-04 13:48:41 | 2013-10-04 13:48:42 |
| 2013-10-04 13:48:49 | 2013-10-04 14:10:23 |
| 2013-10-04 13:49:19 | 2013-10-04 14:10:23 |
| 2013-10-04 13:52:40 | 2013-10-04 13:52:40 |
| 2013-10-04 13:52:46 | 2013-10-04 13:52:46 |
| 2013-10-04 13:53:16 | 2013-10-04 13:53:26 |
| 2013-10-04 13:54:16 | 2013-10-04 13:54:26 |
| 2013-10-04 13:54:26 | 2013-10-04 13:54:26 |
| 2013-10-04 13:54:56 | 2013-10-04 13:54:56 |
| 2013-10-04 13:55:21 | 2013-10-04 13:55:51 |
| 2013-10-04 13:56:37 | 2013-10-04 13:56:37 |
| 2013-10-04 13:56:51 | 2013-10-04 13:56:51 |
| 2013-10-04 13:56:56 | 2013-10-04 13:56:57 |
| 2013-10-04 13:57:21 | 2013-10-04 13:57:21 |
| 2013-10-04 13:57:27 | 2013-10-04 13:57:27 |
| 2013-10-04 13:58:58 | 2013-10-04 13:59:18 |
| 2013-10-04 13:59:08 | 2013-10-04 14:10:06 |
+---------------------+---------------------+
这里是表B日期的前20行:
+---------------------+---------------------+
| StartDateTime | endDateTime |
+---------------------+---------------------+
| 2013-10-04 13:48:13 | 2013-10-04 13:48:14 |
| 2013-10-04 13:48:14 | 2013-10-04 13:48:15 |
| 2013-10-04 13:48:17 | 2013-10-04 13:48:18 |
| 2013-10-04 13:48:26 | 2013-10-04 13:48:27 |
| 2013-10-04 13:48:30 | 2013-10-04 13:48:42 |
| 2013-10-04 13:48:33 | 2013-10-04 13:48:33 |
| 2013-10-04 13:48:34 | 2013-10-04 13:48:35 |
| 2013-10-04 13:48:36 | 2013-10-04 13:48:38 |
| 2013-10-04 13:48:38 | 2013-10-04 13:48:39 |
| 2013-10-04 13:48:40 | 2013-10-04 13:48:41 |
| 2013-10-04 13:48:47 | 2013-10-04 13:48:48 |
| 2013-10-04 13:48:48 | 2013-10-04 13:48:50 |
| 2013-10-04 13:48:49 | 2013-10-04 13:48:50 |
| 2013-10-04 13:48:51 | 2013-10-04 13:48:53 |
| 2013-10-04 13:48:55 | 2013-10-04 13:48:58 |
| 2013-10-04 13:49:03 | 2013-10-04 13:49:06 |
| 2013-10-04 13:49:13 | 2013-10-04 13:49:15 |
| 2013-10-04 13:49:21 | 2013-10-04 13:49:22 |
| 2013-10-04 13:49:24 | 2013-10-04 13:49:25 |
| 2013-10-04 13:49:26 | 2013-10-04 13:49:27 |
+---------------------+---------------------+
编辑:我正在进行查询和查询的2个视图。该查询完美无缺,但需要5分48才能得到结果。
查看1:
CREATE OR REPLACE VIEW DurationOfUseByApp AS
SELECT fkDeviceUserKey, StartDateTime, EndDateTime,
TIMESTAMPDIFF(SECOND, StartDateTime, EndDateTime) AS duration,
ExecutableName
FROM ProcessInfo
INNER JOIN ExecutableInfo
ON ProcessInfo.fkExecutableId = ExecutableInfo.idExecutableId;
视图2:
CREATE OR REPLACE VIEW InputUseEvents AS
SELECT fkDeviceUserKey, StartDateTime, EndDateTime,
TIMESTAMPDIFF(SECOND, StartDateTime, EndDateTime) AS duration,
DeviceType, EventCount1, EventCount2, EventCount3, EventCount4
FROM InputUse
INNER JOIN InputDeviceName
ON InputDeviceName.idInputDeviceName = InputUse.fkInputDeviceName
ORDER BY StartDateTime;
查询:
SELECT '2013-01-01 00:00:00' + INTERVAL FLOOR(TIMESTAMPDIFF(minute, '2013-01-01 00:00:00', DurationOfUseByApp.StartDateTime)/15) * 15 minute AS TimeInterval,
SUM(IF(DeviceType = 4, EventCount1+EventCount2+EventCount3+EventCount4, 0)) AS nbOfTrackpadEvent,
SUM(IF(DeviceType = 3, EventCount1+EventCount2+EventCount3+EventCount4, 0)) AS nbOfMouseEvent,
SUM(IF(DeviceType = 5, EventCount1+EventCount2+EventCount3+EventCount4, 0)) AS nbOfHIDEvent,
IF(ExecutableName = 'chrome', DurationOfUseByApp.duration/60, 0) AS chrome
FROM InputUseEvents INNER JOIN DurationOfUseByApp USING (fkDeviceUserKey)
WHERE DurationOfUseByApp.StartDateTime >= '2013-01-01 00:00:00' AND DurationOfUseByApp.StartDateTime <= '2014-01-01 23:59:59' AND DurationOfUseByApp.StartDateTime <= InputUseEvents.StartDateTime AND DurationOfUseByApp.EndDateTime >= InputUseEvents.EndDateTime
GROUP BY TimeInterval
HAVING chrome > 0
谢谢, Zohm