此查询需要一些帮助,我使它可以与SP一起使用,但速度非常慢,我根本无法使用它。
我有一个名为cb_flights的表,其中包含-
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条生命。 介于3 PK的3324418-3341913之间,以及3杀死的3342956-3348150之间。
我正在尝试获得连胜飞行的开始和结束时间的结果-
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_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'
答案 0 :(得分:0)
您是否尝试过使用INDEX? Indexes are used to retrieve data from the database very fast.
样本索引
CREATE INDEX [YourTable_Index]
ON YourTable (column1, column2, column3, column4) ON [PRIMARY]
或者您可以在BEGIN下方使用它。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
答案 1 :(得分:0)
我在以下链接中找到了答案:
自动为每个组生成连续的ID-http://www.mysqltutorial.org/mysql-row_number/
使用这两个堆栈溢出线程来了解如何使用不同的记录- https://superuser.com/questions/1198455/use-mysql-to-select-the-next-one-record-after-a-matching-value https://superuser.com/questions/1198483/use-mysql-to-select-the-next-one-record-after-a-matching-value-without-a-sequent
希望这可以帮助在此线程中找到自己的人。