Postgres FOR LOOP

编程入门 行业动态 更新时间:2024-10-25 10:30:12
本文介绍了Postgres FOR LOOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图从一个表格中随机抽取15000个ID。而不是每次手动按下运行,我试图做一个循环。我完全理解的不是Postgres的最佳使用,但它是我拥有的工具。这就是我到目前为止:

for i in 1..25 LOOP insert into playtime.meta_random_sample 从tbl 中选择i,ID by random()limit 15000 end loop

解决方案

loops 这样的程序元素不是SQL语言的一部分,只能在主体内部使用。 程序语言功能或者 DO 声明,其中这些附加元素由相应的过程语言定义。默认是 PL / pgSQL ,但还有其他。

plpgsql示例:

DO $ $ BEGIN FOR i IN 1..25 LOOP INSERT INTO playtime.meta_random_sample(col_i,col_id) - 使用列名 SELECT i,id FROM tbl ORDER BY random() LIMIT 15000; END LOOP; END $ do $;

如果您需要优化效果,请考虑以下相关问题: p>

  • 选择随机行PostgreSQL的最佳方式

I am trying to get 25 random samples of 15,000 IDs from a table. Instead of manually pressing run every time, I'm trying to do a loop. Which I fully understand is not the optimum use of Postgres, but it is the tool I have. This is what I have so far:

for i in 1..25 LOOP insert into playtime.meta_random_sample select i, ID from tbl order by random() limit 15000 end loop

解决方案

Procedural elements like loops are not part of the SQL language and can only be used inside the body of a procedural language function or a DO statement, where such additional elements are defined by the respective procedural language. The default is PL/pgSQL, but there are others.

Example with plpgsql:

DO $do$ BEGIN FOR i IN 1..25 LOOP INSERT INTO playtime.meta_random_sample (col_i, col_id) -- use col names SELECT i, id FROM tbl ORDER BY random() LIMIT 15000; END LOOP; END $do$;

If you need to optimize performance, consider this related question:

  • Best way to select random rows PostgreSQL

更多推荐

Postgres FOR LOOP

本文发布于:2023-10-25 18:28:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Postgres   LOOP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!