Session重疊問題學習(五)--最優化
週五晚上10點了.
這一週連續優化Session合併和拆分問題.每天都比前一天提升效能一倍以上.
終於在今天,用獨創的小花狸Session合併演算法達到了最優級別.
令人振奮的1.5秒到2秒級別.
時間已經很晚了,思路也有些不清晰了.先把程式碼貼出來.下週再仔細解釋一下這個奇妙演算法。
和之前的演算法比較,結果一致。基本上都在1.6秒左右.
這一週連續優化Session合併和拆分問題.每天都比前一天提升效能一倍以上.
終於在今天,用獨創的小花狸Session合併演算法達到了最優級別.
令人振奮的1.5秒到2秒級別.
時間已經很晚了,思路也有些不清晰了.先把程式碼貼出來.下週再仔細解釋一下這個奇妙演算法。
- DELIMITER $$
- CREATE DEFINER=`root`@`localhost` PROCEDURE `p`()
- BEGIN
- declare done int default 0;
- declare v_roomid bigint;
- declare v_time timestamp(6);
- declare v_cur_type smallint;
- declare v_before_roomid bigint default -1;
- declare v_before_type smallint default -1;
- declare v_before_time timestamp(6) ;
- declare v_num bigint default 0;
- declare cur_test CURSOR for select roomid,type,timepoint from tmp_time_point order by roomid,timepoint,type ;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- drop table if exists t1;
- drop table if exists t2;
- drop table if exists tmp_time_point;
- drop table if exists tmp_result;
- drop table if exists tmp_min_range;
- CREATE temporary TABLE `t1` (
- `roomid` int(11) NOT NULL DEFAULT '0',
- `userid` bigint(20) NOT NULL DEFAULT '0',
- `s` timestamp(6),
- `e` timestamp(6),
- primary KEY `roomid` (`roomid`,`s`,`e`,`userid`)
- ) ENGINE=memory;
- CREATE temporary TABLE `t2` (
- `roomid` int(11) NOT NULL DEFAULT '0',
- `s` timestamp(6),
- `e` timestamp(6),
- primary KEY `roomid` (`roomid`,`s`,`e`)
- ) ENGINE=memory;
- CREATE temporary TABLE `tmp_min_range` (
- `roomid` int(11) NOT NULL DEFAULT '0',
- `s` timestamp(6),
- `e` timestamp(6),
- primary KEY `roomid` (`roomid`,`s`,`e`),
- key(roomid,e)
- ) ENGINE=memory;
- create temporary table tmp_time_point(
- roomid bigint,
- timepoint timestamp(6),
- type smallint,
- key(roomid,timepoint)
- ) engine=memory;
- create temporary table tmp_result(
- roomid bigint,
- timepoint timestamp(6),
- c int
- ) engine=memory;
- SET @A=0;
- SET @B=0;
- insert into t1(roomid,userid,s,e)
- select distinct
- roomid, userid,
- if(date(s)!=date(e) and id>1,date(s+interval id-1 day),s) s,
- if(date(s+interval id-1 day)=date(e) ,e,date_format(s+interval id-1 day,'%Y-%m-%d 23:59:59')) e
- from (
- SELECT x.roomid,x.userid,s,e
- FROM
- (
- (
- SELECT @B:=@B+1 AS id,roomid,userid,s
- FROM (
- SELECT DISTINCT roomid, userid, roomstart AS s
- FROM u_room_log a
- WHERE NOT EXISTS (SELECT *
- FROM u_room_log b
- WHERE a.roomid = b.roomid
- AND a.userid = b.userid
- AND a.roomstart > b.roomstart
- AND a.roomstart <= b.roomend)
- ) AS p
- ) AS x,
- (
- SELECT @A:=@A+1 AS id,roomid,userid,e
- FROM
- (
- SELECT DISTINCT roomid, userid, roomend AS e
- FROM u_room_log a
- WHERE NOT EXISTS (SELECT *
- FROM u_room_log b
- WHERE a.roomid = b.roomid
- AND a.userid = b.userid
- AND a.roomend >= b.roomstart
- AND a.roomend < b.roomend)
- ) AS o
- ) AS y
- )
- WHERE x.id = y.id AND x.roomid = y.roomid AND x.userid = y.userid
- ) t1 ,
- nums
- where nums.id<=datediff(e,s)+1
- ;
- insert into t2 (roomid,s,e)
- select roomid,
- s+interval startnum/1000000 second s,
- e-interval endnum/1000000 second e
- from (
- select
- roomid,
- s,e,
- startnum,
- case when @eflag=eflag then @rn:=@rn+1 when @eflag:=eflag then @rn else @rn end endnum
- from (
- select * from (
- select case when @sflag=sflag then @rn:=@rn+1 when @sflag:=sflag then @rn else @rn end startnum,roomid,s,e,sflag,eflag from
- (
- select * from
- (
- select t1.*,concat('[',roomid,'],',s) sflag,concat('[',roomid,'],',e) eflag from t1 order by roomid ,sflag
- )a,(select @sflag:='',@rn:=0,@eflag:='') vars
- ) b
- ) bb order by roomid,eflag
- ) c
- ) d ;
- insert into tmp_time_point(roomid,timepoint,type) select roomid,s,1 from t2;
- insert into tmp_time_point(roomid,timepoint,type) select roomid,e,0 from t2;
- insert into tmp_min_range(roomid,s,e)
- select distinct roomid,starttime starttime, endtime endtime from (
- select
- if(@roomid=roomid,@d,'') as starttime,@d:=str_to_date(timepoint,'%Y-%m-%d %H:%i:%s.%f'),@roomid:=roomid,p.roomid,str_to_date(timepoint,'%Y-%m-%d %H:%i:%s.%f') endtime
- from tmp_time_point p,(select @d:='',@roomid:=-1) vars
- order by roomid,timepoint
- ) v4 where starttime!='' and date(starttime)=date(endtime);
- open cur_test;
- repeat
- fetch cur_test into v_roomid,v_cur_type,v_time;
- if done !=1 then
- -- 第一行或者每個房間的第一行
- if v_before_roomid=-1 or v_roomid!=v_before_roomid then
- set v_before_roomid:=v_roomid;
- set v_before_type:=1;
- set v_before_time:='0000-00-00 00:00:00';
- set v_num:=0;
- end if;
- if v_before_type=1 then
- set v_num:=v_num+1;
- insert into tmp_result(roomid,timepoint,c) values(v_roomid,v_time,v_num);
- end if;
- if v_before_type=0 then
- set v_num:=v_num-1;
- insert into tmp_result(roomid,timepoint,c) values(v_roomid,v_time,v_num);
- end if;
- set v_before_roomid:=v_roomid;
- set v_before_type:=v_cur_type;
- set v_before_time:=v_time;
- end if;
- until done end repeat;
- close cur_test;
- select roomid,date(s) dt,round(sum(timestampdiff(second,date_format(s,'%Y-%m-%d %H:%i:%s'),date_format(e,'%Y-%m-%d %H:%i:%s')))/60) ts,max(c)-1 c from (
- select a.roomid,a.s,a.e,r.c,r.timepoint from tmp_result r
- inner join
- tmp_min_range a on( r.timepoint=a.e and r.roomid=a.roomid)
- where c>2
- ) a group by roomid,date(s);
- END
和之前的演算法比較,結果一致。基本上都在1.6秒左右.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2150339/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Session重疊問題學習(三)--優化Session優化
- Session重疊問題學習(四)--再優化Session優化
- Session重疊問題學習(六)--極致優化Session優化
- Session重疊問題學習(一)Session
- Session重疊問題學習(八)--該問題第六次優化和Oracle版本Session優化Oracle
- Session重疊問題學習(七)--小花狸合併演算法和最後一次優化Session演算法優化
- Session重疊問題學習(九)--該問題第七次優化.優化合並演算法Session優化演算法
- 數值最優化—優化問題的解(二)優化
- webpack dll打包重複問題優化Web優化
- React Hooks學習之旅五:useMemo解決子元件重複執行問題ReactHook元件
- 學習動態效能表(五)-v$sessionSession
- 學習動態效能表(五)--V$SESSIONSession
- oracle performance tuning效能優化學習系列(五)OracleORM優化
- Shiro效能優化:解決Session頻繁讀寫問題優化Session
- 強化學習的數學原理-03貝爾曼最優公式強化學習公式
- 凸優化問題優化
- 效能優化問題優化
- 【調優】設計問題還是優化問題?優化
- 【OpenCV學習】計算兩幅影象的重疊區域OpenCV
- 強化學習-學習筆記11 | 解決高估問題強化學習筆記
- LayaAir引擎學習日誌14----LayaAir記憶體優化的問題AI記憶體優化
- matlab最優化問題的函式(fminbnd),fmincon,globalsearch,multistart(全域性區域性最優)Matlab優化函式
- 前端面試題(五)(安全、效能優化)前端面試題優化
- 如何避開jquery動態繫結的事件重疊問題jQuery事件
- 走近流行強化學習演算法:最優Q-Learning強化學習演算法
- MySQL優化學習手札(四) 單表訪問方法MySql優化
- session共享問題???Session
- 圖論 最小生成樹問題(最優連線問題)圖論
- java初學者最關心的五個問題Java
- 斜率優化(凸包優化)DP問題acm優化ACM
- 優思學院|DMAIC五步法的成本與效益問題AI
- 重複登入問題最終解決方法
- Fragment重影(重疊)白屏等問題原理解析,以及解決方案Fragment
- Android 百度地圖InfoWindow 出現重疊的問題Android地圖
- 學習opencv 習題答案-第五章OpenCV
- react 學習 問題React
- Oracle效能優化視訊學習筆記-資料庫配置和I/O問題Oracle優化筆記資料庫
- 03-凸優化問題優化