我有一张桌子可以存储大约80辆车的GPS数据。该表每30秒保持GPS传输。我需要删除一些数据,并至少每60秒保持一次GPS转换。所以我有以下过程读取每一行,每个det_id,当当前行距前一行少于59秒时,FLAG列更新为'0'。在此之后,我将删除那些FLAG ='0'的行。
问题是这个程序需要很长时间(300万条记录大约需要40个小时)。 我用ts_id和ts_dh做了一个索引,但它没有那么大的变化。
如何改进此程序? 我需要做一个特殊的索引吗? 我在手术中遗漏了什么吗?
DECLARE
WriteZero NUMBER:= 0;
CURSOR CUR_TABLE_SOURCE IS
SELECT
FLAG, ANT_TS_DH,ts_id,TS_DH,TS_DHS
FROM TABLE_SOURCE
where ts_id = in_ts_id
order by ts_id, TS_DH;
V_count number:=0;
V_Ant number:=0;
V_TABLESOURCE_REC CUR_TABLE_SOURCE%ROWTYPE;
BEGIN
OPEN CUR_TABLE_SOURCE; --- for update;
loop
FETCH CUR_TABLE_SOURCE INTO V_TABLESOURCE_REC;
EXIT WHEN CUR_TABLE_SOURCE%NOTFOUND;
V_count := V_count + 1;
Begin
IF V_TABLESOURCE_REC.ANT_TS_DH is null then
UPDATE TABLE_SOURCE SET
FLAG = '1'
WHERE ANT_TS_DH = V_TABLESOURCE_REC.ANT_TS_DH
AND ts_id = V_TABLESOURCE_REC.ts_id
AND TS_DHS = V_TABLESOURCE_REC.TS_DHS;
commit;
V_Ant :=1;
ELSE
IF V_TABLESOURCE_REC.ANT_TS_DH + 59/86400 >= V_TABLESOURCE_REC.TS_DH THEN
IF V_Ant = 1 THEN
UPDATE TABLE_SOURCE SET
FLAG = '0' -- delete data with flag = 0
WHERE ANT_TS_DH = V_TABLESOURCE_REC.ANT_TS_DH
AND ts_id = V_TABLESOURCE_REC.ts_id
AND TS_DHS = V_TABLESOURCE_REC.TS_DHS;
V_Ant :=0;
commit;
ELSE
UPDATE TABLE_SOURCE SET
FLAG = '1' -- keep data with flag = 0
WHERE ANT_TS_DH = V_TABLESOURCE_REC.ANT_TS_DH
AND ts_id = V_TABLESOURCE_REC.ts_id
AND TS_DHS = V_TABLESOURCE_REC.TS_DHS;
V_Ant :=1;
commit;
End If;
ELSE
UPDATE TABLE_SOURCE SET
FLAG = '1'
WHERE ANT_TS_DH = V_TABLESOURCE_REC.ANT_TS_DH
AND ts_id = V_TABLESOURCE_REC.ts_id
AND TS_DHS = V_TABLESOURCE_REC.TS_DHS;
V_Ant :=1;
commit;
End If;
End If;
End;
END LOOP;
CLOSE CUR_TABLE_SOURCE;
COMMIT;
END;
这是table_source:
TS_ID TS_DH LATI LONG
----- --------------------- ---------- ----------
3412 15-DEC-16 11.05.09 AM 11.42341 -81.95251
3412 15-DEC-16 11.05.39 AM 11.42344 -81.92435
3412 15-DEC-16 11.06.09 AM 11.43567 -81.85355
3412 15-DEC-16 11.06.39 AM 11.47851 -81.95246
3412 15-DEC-16 11.07.10 AM 11.45123 -81.63451
3412 15-DEC-16 11.07.40 AM 11.43451 -81.95351
3412 15-DEC-16 11.08.10 AM 11.45251 -81.46785
3412 15-DEC-16 11.08.40 AM 11.45678 -81.95551
3412 15-DEC-16 11.09.10 AM 11.43451 -81.93241
3412 15-DEC-16 11.09.40 AM 11.45456 -81.95251
3412 15-DEC-16 11.10.10 AM 11.42890 -81.95451
3412 15-DEC-16 11.15.40 AM 11.43451 -81.95351
3412 15-DEC-16 11.16.10 AM 11.45251 -81.46785
3412 15-DEC-16 11.16.40 AM 11.45678 -81.95551
3412 15-DEC-16 11.17.10 AM 11.43451 -81.93241
3412 15-DEC-16 11.17.40 AM 11.45456 -81.95251
3412 15-DEC-16 11.18.10 AM 11.42890 -81.95451