顯示基於Web的監控項目的實時數據和歷史數據。有近16個採樣頻率爲50Hz的傳感器。傳感器的所有原始數據都必須存儲在數據庫中,每秒鐘可達到近900個數據。數據必須保存至少三年。數據庫是oracle 11g。實時數據表到歷史數據表的數據丟失
我的工作是爲傳感器硬件公司的工程師設計數據庫結構,他將編寫數據採集程序並將數據存儲到數據庫中。
設計了實時數據表和歷史數據表。從實時數據表中讀取實時數據,並從歷史數據表中讀取歷史數據。
實數據表如下,僅存儲一分鐘數據。
Create Table real_data(
record_time timestamp(3),
ac_1 Float,
ac_2 Float,
ac_3 Float,
ac_4 Float,
ac_5 Float,
ac_6 Float,
ac_7 Float,
ac_8 Float,
ac_9 Float,
ac_10 Float,
ac_11 Float,
ac_12 Float,
ac_13 Float,
ac_14 Float,
ac_15 Float,
ac_16 Float
)
Tablespace data_test;
歷史數據表的結構是與真實數據,它由主鍵和分區
Create Table history_data(
record_time timestamp(3),
ac_1 Float,
ac_2 Float,
ac_3 Float,
ac_4 Float,
ac_5 Float,
ac_6 Float,
ac_7 Float,
ac_8 Float,
ac_9 Float,
ac_10 Float,
ac_11 Float,
ac_12 Float,
ac_13 Float,
ac_14 Float,
ac_15 Float,
ac_16 Float
)
Tablespace data_test
PARTITION BY RANGE(record_time)
INTERVAL(numtodsinterval(1,'day'))
(
PARTITION P1 VALUES LESS THAN (TO_DATE('2016-08-01', 'YYYY-MM-DD'))
);
alter table history_data add constraint RECORD_DATE primary key (RECORD_TIME);
間隔分區被選擇用於兩個原因相同:
sql查詢是基於web客戶端的時間記錄,如
select ac_1來自ac_test where record_time> = to_timestamp('2016-08-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and record_time < = to_timestamp('2016-08-01 00 :30:00','yyyy-mm-dd hh24:mi:ss');
間隔分區的範圍是天。在一天數據測試期間,每天近430萬數據的成本爲近40秒。
執行作業以每一分鐘將實際數據傳送到歷史數據表。傳輸過程由oracle過程完成,傳輸時間由另一個表記錄:real_data_top_backup_date。
create or replace procedure copy_to_history_test is
d_top_backup_date timestamp(3);
begin
select top_backup_date into d_top_backup_date from real_data_top_backup_date;
Insert Into history_data Select * From real_data where record_time <d_top_backup_date;
delete from real_data where record_time <d_top_backup_date;
Update real_data_top_backup_date Set top_backup_date=(d_top_backup_date+1/24/60);
commit;
end copy_to_history_test;
並編寫仿真程序來模擬傳感器數據採集和插入。
Declare
time_index Number;
start_time Timestamp(3);
tmp_time Timestamp(3);
tmp_value1 Float;
tmp_value2 Float;
tmp_value3 Float;
tmp_value4 Float;
tmp_value5 Float;
tmp_value6 Float;
tmp_value7 Float;
tmp_value8 Float;
tmp_value9 Float;
tmp_value10 Float;
tmp_value11 Float;
tmp_value12 Float;
tmp_value13 Float;
tmp_value14 Float;
tmp_value15 Float;
tmp_value16 Float;
Begin
--initiaze the variable
time_index:=0;
SELECT to_timestamp('2016-08-01 00:00:00:000', 'yyyy-mm-dd h24:mi:ss:ff') Into start_time FROM DUAL;
While time_index<(50*60*60*24*7)
Loop
-- add 20 millionseconds
SELECT start_time+numtodsinterval((0.02*time_index),'SECOND') Into tmp_time FROM DUAL;
-- dbms_output.put_line(tmp_time);
-- create random number
select dbms_random.value Into tmp_value1 from dual ;
select dbms_random.value Into tmp_value2 from dual ;
select dbms_random.value Into tmp_value3 from dual ;
select dbms_random.value Into tmp_value4 from dual ;
select dbms_random.value Into tmp_value5 from dual ;
select dbms_random.value Into tmp_value6 from dual ;
select dbms_random.value Into tmp_value7 from dual ;
select dbms_random.value Into tmp_value8 from dual ;
select dbms_random.value Into tmp_value9 from dual ;
select dbms_random.value Into tmp_value10 from dual ;
select dbms_random.value Into tmp_value11 from dual ;
select dbms_random.value Into tmp_value12 from dual ;
select dbms_random.value Into tmp_value13 from dual ;
select dbms_random.value Into tmp_value14 from dual ;
select dbms_random.value Into tmp_value15 from dual ;
select dbms_random.value Into tmp_value16 from dual ;
--dbms_output.put_line(tmp_value);
-- Insert Into ac_data (sensor_id,data,record_time) Values(sensor_index,tmp_value,tmp_time);
Insert Into real_data Values(tmp_time,tmp_value1,tmp_value2,tmp_value3,tmp_value4,tmp_value5,tmp_value6,tmp_value7,tmp_value8,tmp_value9,tmp_value10,tmp_value11,tmp_value12,tmp_value13,tmp_value14,tmp_value15,tmp_value16);
if mod(time_index,50)=0 then
commit;
dbms_lock.sleep(1);
End If;
time_index:=time_index+1;
End Loop;
-- dbms_output.put_line(c);
Exception
WHEN OTHERS THEN
log_write('insert data failure!');
End;
問題是,在傳輸數據過程中,接近0.1%的傳感器數據量將會丟失。我認爲傳輸數據(插入數據和刪除數據)的並行操作會導致數據丟失。如何處理這個問題?
在這種情況下,數據庫結構是否可行?數據庫有更好的設計嗎?
你怎麼知道數據已經丟失? –
@EvgeniyK。我發現有一天有4316850個傳感器數據,它應該由432000個數據組成。 – skyspeed