如何检查高频次的插入数据操作?
高频次的数据插入一般来说是异常的.做为日常操作,数据插入的频次不可能是毫秒级的,由此可以判定,如果有高频次的数据插入,一定是程序什么地方控制出了问题.
首先,要找出是"谁","什么时间"进行了高频次的数据插入操作.
其次,所谓的"高频次",是要在两条数据记录之间的进行时间对比,如果时间小于某值,就认为是高频次.
再次,需要找到高频操作的时间点,对应地找到当时的操作日志,看看当时发生过什么情况.
以下以销售订单与物流订单为例,看看SQL语句如何编写?
--- 销售订单
with t as (
select top 200000 row_number() over(order by F0000) as F0 , -- 排序码
F0000 , -- 订单数据ID
F0001 , -- 公司编号
F0038 F0002 , -- 操作者
f0039 F0003 -- 插入时间
from SALESORDER
where t.F0001=1001 and F0038='yanshiqb'
order by F0000
)
select top 2000 t.*,(select top 1 b.F0003 from t b where b.F0 < t.F0 order by b.F0 desc) F03,
dif = case when t.F0=(select min(F0) from t) then null
else DATEDIFF(ss,(select top 1 b.F0003 from t b where b.F0 < t.F0 order by b.F0 desc),t.F0003)
end
from t
F0 F0000 F0001 F0002 F0003 dif
10 6900 1001 yanshiqb 2019-10-31 15:06:41.413 90502
11 6928 1001 yanshiqb 2019-11-05 16:43:01.917 437780
12 6957 1001 yanshiqb 2019-11-06 17:53:24.217 90623
13 6958 1001 yanshiqb 2019-11-06 17:54:29.373 65
14 7160 1001 yanshiqb 2019-11-28 14:13:05.157 1887516
15 7161 1001 yanshiqb 2019-11-28 14:21:16.433 491
16 7162 1001 yanshiqb 2019-11-28 14:27:10.500 354
17 7164 1001 yanshiqb 2019-11-28 15:47:29.130 4819
18 7205 1001 yanshiqb 2019-12-04 15:56:11.323 518922
19 7206 1001 yanshiqb 2019-12-04 16:06:14.380 603
20 7313 1001 yanshiqb 2019-12-16 13:57:20.460 1029066
21 7318 1001 yanshiqb 2019-12-16 16:37:22.087 9602
22 7321 1001 yanshiqb 2019-12-16 16:44:38.450 436
23 7322 1001 yanshiqb 2019-12-16 16:45:40.183 62
24 7326 1001 yanshiqb 2019-12-16 17:50:16.287 3876
25 7388 1001 yanshiqb 2019-12-20 16:46:38.443 341782
插入操作的最小间隔是62秒,不能算做是高频次插入操作.
--- 快递运单
with t as (
select top 200000 row_number() over(order by F0000) as F0 , -- 排序码
F0000 , -- 订单数据ID
F0001 , -- 公司编号
F0067 F0002, -- 操作者
f0068 F0003 -- 插入时间
from WAYBILLORDER
where F0001=10272 and F0067='zhang'
order by F0000
)
select top 2000 t.*,
dif = case when t.F0=(select min(F0) from t) then null
else DATEDIFF(ss,(select top 1 b.F0003 from t b where b.F0 < t.F0 order by b.F0 desc),t.F0003)
end
from t
以下为查询结果:
F0 F0000 F0001 F0002 F0003 dif
109 1394 10272 zhang 2020-01-12 13:20:02.613 0
110 1395 10272 zhang 2020-01-12 13:20:02.927 0
111 1396 10272 zhang 2020-01-12 13:20:04.880 2
112 1397 10272 zhang 2020-01-12 13:20:07.520 3
113 1398 10272 zhang 2020-01-12 13:20:07.973 0
114 1399 10272 zhang 2020-01-12 13:20:08.300 1
115 1400 10272 zhang 2020-01-12 13:20:08.597 0
116 1401 10272 zhang 2020-01-12 13:20:09.393 1
117 1402 10272 zhang 2020-01-12 13:20:09.677 0
118 1403 10272 zhang 2020-01-12 13:20:10.223 1
119 1404 10272 zhang 2020-01-12 13:20:10.567 0
120 1405 10272 zhang 2020-01-12 13:20:10.880 0
121 1406 10272 zhang 2020-01-12 13:20:11.177 1
122 1407 10272 zhang 2020-01-12 13:20:16.487 5
从结果看出,在0秒内插入了几条数据,显然是高频次的操作.
根据插入时间点,再跟踪找到用户的操作轨迹,直到最终找到原因.