溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在postgres中使用存儲過程批量插入數(shù)據(jù)

發(fā)布時(shí)間:2021-02-03 16:43:09 來源:億速云 閱讀:283 作者:Leah 欄目:開發(fā)技術(shù)

如何在postgres中使用存儲過程批量插入數(shù)據(jù)?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

參考官方文檔

create or replace function creatData2() returns 
boolean AS
$BODY$
declare ii integer;
 begin
 II:=1;
 FOR ii IN 1..10000000 LOOP
 INSERT INTO ipm_model_history_data (res_model, res_id) VALUES (116, ii);
 end loop;
 return true;
 end;
$BODY$
LANGUAGE plpgsql;
select * from creatData2() as tab;

插入1千萬條數(shù)據(jù)耗時(shí)610s,當(dāng)然字段不多的情況下。

補(bǔ)充:Postgresql存儲過程--更新或者插入數(shù)據(jù)

要記錄某一時(shí)段機(jī)器CPU、內(nèi)存、硬盤的信息,展示的時(shí)間粒度為分鐘,但是為了精確,輸入數(shù)據(jù)源的時(shí)間粒度為6s。這個(gè)統(tǒng)計(jì)過程可以在應(yīng)用層做好,每分鐘插入一次,也可以在數(shù)據(jù)庫層寫個(gè)存儲過程來完成,根據(jù)傳入數(shù)據(jù)的時(shí)間來判斷是更新數(shù)據(jù)庫舊數(shù)據(jù)還是插入新數(shù)據(jù)。

同時(shí),這些數(shù)據(jù)只需要保留一周,更老的數(shù)據(jù)需要被刪除。刪除動(dòng)作可以每天定時(shí)執(zhí)行一次,也可以寫在存儲過程中每次檢查一下。

考慮到性能在此時(shí)沒什么太大約束,而后面存儲過程的接口方式更漂亮些,不用應(yīng)用層去關(guān)心數(shù)據(jù)到底組織成什么樣,因此實(shí)現(xiàn)了一個(gè)如下:

Postgresql V8.3
CREATE OR REPLACE FUNCTION insert_host_status(_log_date timestamp without time zone, _host inet, _cpu integer, _mem integer, _disk integer)
 RETURNS void AS
$BODY$
DECLARE
  new_start timestamp without time zone;
  current_start timestamp without time zone;
  c_id integer;
  c_log_date timestamp without time zone;
  c_cpu integer;
  c_mem integer;
  c_disk integer;
  c_count integer;
  date_span interval;
BEGIN
  -- insert or update
  SELECT id, log_date, cpu, mem, disk, count INTO c_id, c_log_date, c_cpu, c_mem, c_disk, c_count FROM host_status_byminute WHERE host=_host ORDER BY id DESC limit 1;
  SELECT timestamp_mi(_log_date, c_log_date) INTO date_span;
  IF date_span >= '00:00:60' OR c_id IS NULL THEN
    INSERT INTO host_status_byminute (log_date, host, cpu, mem, disk, count) values (_log_date, _host, _cpu, _mem, _disk, 1);
  ELSIF date_span >= '-00:00:60' THEN
    c_mem := ((c_mem * c_count) + _mem)/(c_count + 1);
    c_cpu := ((c_cpu * c_count) + _cpu)/(c_count + 1);
    c_disk := ((c_disk * c_count) + _disk)/(c_count + 1);
    c_count := c_count + 1;
    UPDATE host_status_byminute SET mem=c_mem, cpu=c_cpu, disk=c_disk, count=c_count WHERE id=c_id;
  END IF;
  -- delete old data
  SELECT date_mii(date(now()), 6) INTO new_start;
  SELECT date(log_date) from host_status_byminute limit 1 INTO current_start; -- omit a bug happened when date is disordered.
  IF new_start > current_start THEN
    DELETE FROM host_status_byminute where log_date < new_start;
  END IF;
END;
$BODY$
 LANGUAGE 'plpgsql' VOLATILE
 COST 100;
ALTER FUNCTION insert_host_status(timestamp without time zone, inet, integer, integer, integer) OWNER TO dbuser_test;

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI