And*_*way 3 sql postgresql postgis
我想将十亿行浮动数据插入到postgresql数据库中,以便我可以测试各种postgis函数的性能。我下面的工作花费了很长时间,而且似乎效率很低,而且内存消耗似乎在膨胀。任何人都可以建议一种更好的方法来做到这一点 - 我认为每次插入插入一百万行会更好,但我无法弄清楚如何构建对象,例如:(a, b), (c, d)插入。
非常感谢任何帮助。请注意,我对 SQL 有点新手,所以我无法消化需要高级计算机科学学位才能吸收的超优化解决方案:)我正在寻找“足够好”。
塔,
安德鲁
do $$
declare
position float := 0;
measurement float := 0;
counting integer := 0;
begin
while position < 100 loop
INSERT into lat_longs values (counting, postition);
position := position + 0.0000001;
counting := counting + 1;
end loop;
raise notice 'count: %', counting;
end$$;
Run Code Online (Sandbox Code Playgroud)
小智 8
通常,在 PL/pgSQL 中使用generate_series()比使用循环更快。
要生成“位置”值,您可以使用random()
以下代码将插入 1 亿行,第二列具有随机值:
insert into lat_longs(c1, c2)
select g.id, random() * 100
from generate_series(1,100e6) as g(id);
Run Code Online (Sandbox Code Playgroud)
我更喜欢分块插入测试数据(例如一次 1000 万个)。如果您让 Postgres 为第一列生成唯一值,例如将其定义为标识列,那就更容易做到:
create table lat_longs
(
c1 bigint generated always as identity,
c2 float
)
insert into lat_longs(c2)
select random() * 100
from generate_series(1,10e6) as g(id);
insert into lat_longs(c2)
select random() * 100
from generate_series(1,10e6) as g(id);
...
Run Code Online (Sandbox Code Playgroud)
如果您确实需要第二列不断增加,如果您有标识列,则可以这样做:
insert into lat_longs(c2)
select g.position
from generate_series(0, 100, 0.0000001) as g(position);
Run Code Online (Sandbox Code Playgroud)
或者分块进行:
insert into lat_longs(c2)
select g.position
from generate_series(0, 10, 0.0000001) as g(position);
insert into lat_longs(c2)
select g.position
from generate_series(10, 20, 0.0000001) as g(position);
...
Run Code Online (Sandbox Code Playgroud)