【发布时间】:2016-10-05 11:22:58
【问题描述】:
我的问题与this 问题有关,但它有所不同,因为每个搜索过滤器的日期时间不同。
MYSQL/MARIADB 架构和示例数据:
CREATE DATABASE IF NOT EXISTS `puzzle` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
USE `puzzle`;
DROP TABLE IF EXISTS `event`;
CREATE TABLE `event` (
`eventId` bigint(20) NOT NULL AUTO_INCREMENT,
`sourceId` bigint(20) NOT NULL COMMENT 'think of source as camera',
`carCode` varchar(40) NOT NULL COMMENT 'ex: A',
`carNumber` varchar(40) NOT NULL COMMENT 'ex: 5849',
`createdOn` datetime DEFAULT NULL,
PRIMARY KEY (`eventId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `event` (`eventId`, `sourceId`, `carCode`, `carNumber`, `createdOn`) VALUES
(1, 44,'A', '4456', '2016-09-20 20:24:05'),
(2, 26,'B', '26484', '2016-09-20 20:24:05'),
(3, 5,'A', '4456', '2016-09-20 20:24:06'),
(4, 3,'C', '72704', '2016-09-20 20:24:15'),
(5, 3,'D' ,'399606', '2016-09-20 20:26:15'),
(6, 5, 'A', '4456', '2016-09-20 20:27:25'),
(7, 44,'C', '72704', '2016-09-20 20:29:25'),
(8, 3,'A' ,'4456', '2016-09-20 20:30:55'),
(9, 44,'B' ,'26484', '2016-09-20 20:34:55'),
(10, 26,'B' ,'4456', '2016-09-20 20:35:15'),
(11, 3, 'C','72704', '2016-09-20 20:35:15'),
(12, 3,'D', '399606', '2016-09-20 20:44:35'),
(13, 26,'A' ,'4456', '2016-09-20 20:49:45');
要求:
用户有两个搜索过滤器。
在他说的第一个搜索过滤器中,让我在特定日期和时间获取所有在 (44,3) 中具有 sourceId 的汽车。假设(e.createdOn > '2016-09-20 20:24:00' 和 e.createdOn
在第二个搜索过滤器中,他说,将 sourceId 在 (26,3,5) 中的所有汽车获取另一个日期和时间。假设(e.createdOn > '2016-09-20 20:36:00' 和 e.createdOn
现在我想获取两个搜索结果中可用的所有(carNumbers、carCode)。
查询需要快速,因为真实表包含超过 3 亿条记录。
到目前为止,以下是我可以查询的最大值(它甚至没有产生有效的结果)
select distinct e1.carNumber, e1.carCode from event e1
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (44,3) ) e3 on e1.carNumber= e3.carNumber
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (44,3) ) e4 on e1.carCode= e4.carCode
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (26,3,5) ) e5 on e1.carNumber= e5.carNumber
inner join (select e2.carNumber, e2.carCode from event e2 where e2.sourceId in (26,3,5) ) e6 on e1.carCode= e6.carCode
我想知道 SQL 是否能够解决这个问题,还是我应该使用后端编码?
正在使用的 MySQL / MariaDB 版本:
mariadb-5.5.50
mysql-5.5.51
【问题讨论】:
-
你为什么要做4个join?用你的约束做一个 where 子句。每个子查询将读取 300M。意思是你读了 5*300M 行。
-
基本上我想获得两个搜索过滤器的相交结果。