濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > postgres 使用存儲(chǔ)過程批量插入數(shù)據(jù)的操作

postgres 使用存儲(chǔ)過程批量插入數(shù)據(jù)的操作

熱門標(biāo)簽:電銷機(jī)器人能補(bǔ)救房產(chǎn)中介嗎 電話機(jī)器人怎么換人工座席 廣州電銷機(jī)器人公司招聘 400電話申請(qǐng)客服 移動(dòng)外呼系統(tǒng)模擬題 濟(jì)南外呼網(wǎng)絡(luò)電話線路 天津開發(fā)區(qū)地圖標(biāo)注app 江蘇400電話辦理官方 地圖標(biāo)注要花多少錢

參考官方文檔

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千萬(wàn)條數(shù)據(jù)耗時(shí)610s,當(dāng)然字段不多的情況下。

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

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

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

考慮到性能在此時(shí)沒什么太大約束,而后面存儲(chǔ)過程的接口方式更漂亮些,不用應(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;

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 使用postgresql 模擬批量數(shù)據(jù)插入的案例
  • PostgreSQL upsert(插入更新)數(shù)據(jù)的操作詳解
  • 使用Postgresql 實(shí)現(xiàn)快速插入測(cè)試數(shù)據(jù)
  • postgreSQL數(shù)據(jù)庫(kù) 實(shí)現(xiàn)向表中快速插入1000000條數(shù)據(jù)
  • Python隨機(jī)生成數(shù)據(jù)后插入到PostgreSQL

標(biāo)簽:海西 辛集 濮陽(yáng) 榆林 杭州 昭通 溫州 寶雞

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《postgres 使用存儲(chǔ)過程批量插入數(shù)據(jù)的操作》,本文關(guān)鍵詞  postgres,使用,存儲(chǔ),過程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《postgres 使用存儲(chǔ)過程批量插入數(shù)據(jù)的操作》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于postgres 使用存儲(chǔ)過程批量插入數(shù)據(jù)的操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    镇江市| 新晃| 西林县| 宝清县| 青海省| 嵊泗县| 岐山县| 华亭县| 河池市| 五河县| 宝兴县| 蓬溪县| 山阳县| 晋江市| 枣强县| 西贡区| 朝阳县| 安庆市| 安岳县| 安龙县| 庆城县| 出国| 绥化市| 东港市| 安图县| 新干县| 太原市| 句容市| 延边| 丘北县| 湘潭县| 莱芜市| 宕昌县| 山阴县| 定日县| 修水县| 察隅县| 育儿| 高青县| 两当县| 阿拉善盟|