0

問題の説明: 過去 15 分間の最新のアラームを取得する次のクエリがあります。

SELECT
   AlmCode,OccurTime,ClearTime....columnN 
FROM 
   TB_ALM 
WHERE 
   AlmCode IN ('3236',....'5978') AND 
   OccurTime >= date_sub(NOW(),interval 15 minute);

テーブル構造:

CREATE TABLE `TB_ALM` (
  `Col1` smallint(2) DEFAULT NULL,
  `Col2` int(4) DEFAULT NULL,
  `Col3` int(2) DEFAULT NULL,
  `Col4` int(10) DEFAULT NULL,
  `Col5` int(10) unsigned DEFAULT NULL,
  `Col6` int(2) DEFAULT NULL,
  `Col7` int(2) DEFAULT NULL,
  `Col8` int(10) DEFAULT NULL,
  `Col9` int(10) unsigned DEFAULT NULL,
  `AlmCode` int(10) unsigned DEFAULT NULL,
  `Col10` int(2) NOT NULL,
  `Col11` int(10) unsigned DEFAULT NULL,
  `Col12` char(12) DEFAULT NULL,
  `Col13` int(2) unsigned DEFAULT NULL,
  `Col14` int(10) unsigned DEFAULT NULL,
  `Col15` int(10) unsigned DEFAULT NULL,
  `Col16` int(10) unsigned DEFAULT NULL,
  `OccurTime` datetime NOT NULL,
  `ClearTime` datetime DEFAULT NULL,
  `AlmDesc` varchar(500) DEFAULT NULL,
  `Col20` int(1) DEFAULT '0',
  `Col21` bigint(20) DEFAULT NULL,
  `Col22` char(120) DEFAULT NULL,
  `Col23` int(10) DEFAULT NULL,
  KEY `TB_ALM_IDX2` (`Col1`,`Col2`,`Col3`,`Col6`,`Col7`,`Col11`,`AlmCode`,`Col9`,`Col4`,`Col8`,`ClearTime`) USING BTREE,
  KEY `TB_ALM_IDX1` (`Col1`,`Col2`,`Col3`,`Col6`,`Col7`,`Col11`,`AlmCode`,`Col5`,`Col21`),
  KEY `TB_ALM_IDX3` (`Col1`,`Col2`,`Col3`,`Col5`) USING BTREE,
  KEY `TB_ALM_IDX4` (`Col1`,`Col2`,`Col3`,`OccurTime`,`ClearTime`,`Col21`) USING BTREE,
  KEY `TB_ALM_IDX5` (`Col23`),
  KEY `TB_ALM_IDX6` (`Col1`,`Col2`,`Col3`,`Col6`,`Col7`,`AlmCode`,`Col11`,`ClearTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

必要なもの: これを変更して、次の基準でアラームを取得する必要があります。

a. 過去 15 分間に発生したアラーム (AlmCodes) (元の要件) AND

b. 過去 6 時間の 15 分間のウィンドウのいずれにおいても、各アラーム (AlmCode) が 3 回以上発生していない場合のみ

試し たこと:次のアプローチを試しました:

  1. 過去 15 分間の DISTINCT(AlmCodes) を取得します。

    AlmCode IN ('3236','4002','4008','4036','4050','4051','4102','4108','4136','4150') の TB_ALM から、distinct(AlmCode) を選択します。 ,'4151','4202','4208','4236','4250','4251','4801','4802','4836','4848','4850','4851',' 4902','4936','4950','4951','5002','5008','5036','5050','5051','5102','5108','5136','5150' ,'5151','5202','5208','5236','5250','5251','5947','5950','5952','5975','5976','5977',' 5978') AND OccurTime >= date_sub(NOW(),interval 15 分) ;

  2. Item-1(上記)をサブクエリとして使用し、各 AlmCode の出現回数を取得します。

    select Almcode,concat(date(OccurTime),' ',HOUR(OccurTime)) as HR,count(*) from TB_ALM_HISTORY where AlmCode IN ( select distinct(s.AlmCode) from TB_ALM_HISTORY s where s.AlmCode IN ('3236' ,'4002','4008','4036','4050','4051','4102','4108','4136','4150','4151','4202','4208',' 4236','4250','4251','4801','4802','4836','4848','4850','4851','4902','4936','4950','4951' ,'5002','5008','5036','5050','5051','5102','5108','5136','5150','5151','5202','5208',' 5236','5250','5251','5947','5950','5952','5975','5976','5977','5978') AND s.OccurTime >= date_sub(NOW(),interval 15 分) ) AND OccurTime >= date_sub(NOW(),interval 15*4*24 分) AlmCode、HR でグループ化。

問題:

  1. Items-2 クエリは (サブクエリ) で永遠に実行され続けますが、2 つの個別のクエリとして実行した場合、以下のように即座に返されます。ここに何が欠けていますか?

クエリ 1: 一意のアラームを取得する

select distinct(AlmCode)
from TB_ALM_HISTORY 
where AlmCode IN ('3236','4002','4008','4036','4050','4051','4102','4108','4136','4150','4151','4202','4208','4236','4250','4251','4801','4802','4836','4848','4850','4851','4902','4936','4950','4951','5002','5008','5036','5050','5051','5102','5108','5136','5150','5151','5202','5208','5236','5250','5251','5947','5950','5952','5975','5976','5977','5978') 
AND OccurTime >= date_sub(NOW(),interval 15 minute) ;

    +---------+
    | AlmCode |
    +---------+
    |    3236 |
    |    5202 |
    |    5236 |
    +---------+

クエリ 2: 過去 6 時間の一意のアラームごとにカウントを取得する

select Almcode,concat(date(OccurTime),' ',LPAD(HOUR(OccurTime),2,'0')) as HR,count(*) from TB_ALM_HISTORY where AlmCode IN ('3236','5202','5236') AND OccurTime >= date_sub(NOW(),interval 15*4*7 minute) group by AlmCode,HR;
+---------+---------------+----------+
| Almcode | HR            | count(*) |
+---------+---------------+----------+
|    3236 | 2015-08-04 11 |        2 |
|    5202 | 2015-08-04 13 |        6 |
|    5202 | 2015-08-04 14 |        4 |
|    5202 | 2015-08-04 15 |        2 |
|    5202 | 2015-08-04 16 |        1 |
|    5202 | 2015-08-04 17 |        2 |
+---------+---------------+----------+

このクエリが東部標準時の午後 6 時に実行されたと仮定すると、AlmCode 5202 は過去 6 時間 (12 ~ 18 時間) に発生したため、この AlmCode の結果は最終的な選択クエリ (過去 15 分間に発生したもの) に含まれるべきではありません。一方、AlmCode 3236 は過去 6 時間に発生しなかったため、この特定の AlmCode で過去 15 分間に発生したすべてのアラームを含める必要があります。

  1. 1 つのクエリですべての最終出力を取得するにはどうすればよいですか?

a. OccurTime >= 過去 15 分で一意の AlmCode を取得します

b. これらの AlmCode ごとに、過去 6 時間に 3 回発生したかどうかを確認します

c. NO の場合、この AlmCode の OccurTime >= Last 15 Minutes のすべてのアラームをプルします (YES の場合は含めず、単にスキップします)

4

1 に答える 1