sql在有条件的日期之间查找行

8yparm6h  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(320)

需要一些帮助这个查询,我得到了它与一个sp工作,但它非常慢,我根本无法使用它。
我有一张名为cb\u flights的table,里面有-

pk      ucid        takeoff_time    end_time        end_event       kills
3321598 1828a3b47   15:17:21        15:17:39        eject           1
3324418 1828a3b47   15:18:12        15:42:24        landing         2
3341913 1828a3b47   19:00:06        19:00:57        pilot_death     0
3342956 1828a3b47   19:08:45        19:08:56        landing         0
3345329 1828a3b47   19:09:23        19:31:42        landing         2
3346649 1828a3b47   19:38:17        19:38:34        landing         0
3348150 1828a3b47   19:40:01        19:48:39        pilot_death     1

此表当前仅显示完整的2个生命周期。在pk 3324418-3341913之间有2次杀死,在pk 3342956-3348150之间有3次杀死。
我正在努力得到一个关于连续飞行的开始和结束时间的结果-

ucid        streak_start        streak_end      sum(kills)
1828a3b47   15:18:12            19:00:57        2
1828a3b47   19:08:45            19:48:39        3

我需要上面的表作为临时连接到另一个表,并得到杀死的描述。之后获得ucid的最佳连胜。
我有一个sp,已经做了最好的连击一般杀死没有结合其他表,但它是非常缓慢的。sp-

CREATE DEFINER=`123`@`%` PROCEDURE `bestStreak`(in t_ucid VARCHAR(50))
BEGIN
DECLARE streakstart DATETIME;
DECLARE streakend DATETIME;
DECLARE streakresult INT;
DECLARE beststreak INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET streakresult = 0;

-- FROM FIRST SORTIE TO FIRST DEATH BEST STREAK
select min(takeoff_time) into streakstart
from cb_flights
where ucid = t_ucid;

select min(end_time) into streakend
from cb_flights
where end_event <> 'landing'
and ucid = t_ucid;

if streakresult is null then
    select sum(kills) into beststreak
    from cb_flights
    where takeoff_time >= streakstart 
    and takeoff_time < streakend
    and ucid = t_ucid
    group by ucid;
elseif streakresult = 0 then
    select sum(kills) into beststreak
    from cb_flights
    where takeoff_time >= streakstart 
    and ucid = t_ucid
    group by ucid;
end if;

-- FROM SECOND SORTIE TO LAST DEATH - EVALUATE ALL AND PUT IN BEST STREAK
beststrk: WHILE (streakstart<>streakend) DO

    select min(end_time) into streakstart
    from cb_flights
    where end_event <> 'landing'
    and end_time > streakstart
    and ucid = t_ucid;

    select min(end_time) into streakend
    from cb_flights
    where end_event <> 'landing'
    and end_time > streakstart
    and ucid = t_ucid;

    select sum(kills) into streakresult
    from cb_flights
    where takeoff_time between streakstart and streakend
    and ucid = t_ucid
    group by ucid;

    if streakresult > beststreak then
        select streakresult into beststreak;
    end if;        
END WHILE;

select beststreak;
END

谢谢你的帮助!
编辑
添加-显示创建表cb\u flights

'CREATE TABLE `cb_flights` (
  `pk` int(11) NOT NULL AUTO_INCREMENT,
  `ucid` varchar(50) NOT NULL,
  `takeoff_time` datetime DEFAULT NULL,
  `end_time` datetime DEFAULT NULL,
  `end_event` varchar(45) DEFAULT NULL,
  `side` varchar(45) DEFAULT NULL,
  `kills` int(11) DEFAULT NULL,
  `type` varchar(45) DEFAULT NULL,
  `map_fk` int(11) DEFAULT NULL,
  `era_fk` int(11) DEFAULT NULL,
  PRIMARY KEY (`pk`),
  UNIQUE KEY `ucid_takeofftime` (`ucid`,`takeoff_time`),
  KEY `ucid_idx` (`ucid`) /*!80000 INVISIBLE */,
  KEY `end_event` (`end_event`) /*!80000 INVISIBLE */,
  KEY `side` (`side`)
) ENGINE=InnoDB AUTO_INCREMENT=7713276 DEFAULT CHARSET=utf8'
crcmnpdw

crcmnpdw1#

我在以下链接中找到了答案:
为每个组自动生成连续ID-http://www.mysqltutorial.org/mysql-row_number/
使用这两个堆栈溢出线程了解如何处理不同的记录-https://superuser.com/questions/1198455/use-mysql-to-select-the-next-one-record-after-a-matching-valuehttps://superuser.com/questions/1198483/use-mysql-to-select-the-next-one-record-after-a-matching-value-without-a-sequent
希望这能帮助那些陷入困境的人。

uyto3xhc

uyto3xhc2#

你试过用索引吗?索引用于快速从数据库检索数据。
样本索引

CREATE INDEX [YourTable_Index] 
ON YourTable (column1, column2, column3, column4) ON [PRIMARY]

或者你可以在你的开始下面用这个。 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

相关问题