在 postgresQL 中自动插入大量记录 table
Insert automatically large amount of records in postgresSQL table
我需要用 PostgresQL 中的大量记录随机填充我的 table,例如 200k
CREATE TABLE qr_code.tbl_transaction (
transaction_id varchar NOT NULL,
importo numeric NOT NULL,
alias varchar NOT NULL,
order_id varchar NOT NULL,
filiale varchar NOT NULL,
descrizione varchar NOT NULL,
data_creazione timestamp NOT NULL,
terminale varchar NOT NULL,
data_esecuzione timestamp NULL,
chiave_movimento_prenotata varchar NULL,
stato varchar NULL,
codice_fiscale varchar(16) NULL,
CONSTRAINT tbl_transaction_pk PRIMARY KEY (transaction_id)
);
我怎样才能快速做到这一点?
您可以使用generate_series()
生成大量行并使用random()生成随机值。
类似于:
insert into tbl_transaction (transaction_id, importo, alias, order_id, filiale, descrizione, data_creazione, terminale, data_esecuzione, chiave_movimento_prenotata, stato, codice_fiscale)
select g.id::text,
random() * 1000,
'some alias',
(random()*10000 + 1)::text,
md5(random()::text),
md5(random()::text),
timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
md5(random()::text),
timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
md5(random()::text),
case (id % 5) + 1
when 1 then 'one'
when 2 then 'two'
when 3 then 'three'
when 4 then 'four'
when 5 then 'five'
else 'unknonw'
end,
'some_codice'
from generate_series(1,200000) as g(id);
我需要用 PostgresQL 中的大量记录随机填充我的 table,例如 200k
CREATE TABLE qr_code.tbl_transaction (
transaction_id varchar NOT NULL,
importo numeric NOT NULL,
alias varchar NOT NULL,
order_id varchar NOT NULL,
filiale varchar NOT NULL,
descrizione varchar NOT NULL,
data_creazione timestamp NOT NULL,
terminale varchar NOT NULL,
data_esecuzione timestamp NULL,
chiave_movimento_prenotata varchar NULL,
stato varchar NULL,
codice_fiscale varchar(16) NULL,
CONSTRAINT tbl_transaction_pk PRIMARY KEY (transaction_id)
);
我怎样才能快速做到这一点?
您可以使用generate_series()
生成大量行并使用random()生成随机值。
类似于:
insert into tbl_transaction (transaction_id, importo, alias, order_id, filiale, descrizione, data_creazione, terminale, data_esecuzione, chiave_movimento_prenotata, stato, codice_fiscale)
select g.id::text,
random() * 1000,
'some alias',
(random()*10000 + 1)::text,
md5(random()::text),
md5(random()::text),
timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
md5(random()::text),
timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
md5(random()::text),
case (id % 5) + 1
when 1 then 'one'
when 2 then 'two'
when 3 then 'three'
when 4 then 'four'
when 5 then 'five'
else 'unknonw'
end,
'some_codice'
from generate_series(1,200000) as g(id);