从一张大表到其他相关表的大量插入
进入:
目前我已将所有数据抓取到一个 PostgreSQL 'Bigtable' 表中(大约有 120 万行)。现在我需要将设计拆分为单独的表,这些表都依赖于 Bigtable。一些表可能有子表。该模型看起来非常像雪花。
问题:
将数据插入表的最佳选择是什么?我想用“SQL”或 PLgSQL 编写的函数进行插入。但问题仍然出在自动生成的 ID 上。
另外,如果您知道哪些工具可以使这个问题的解决变得更容易,那么就发帖吧!
//编辑我添加了示例,这不是仅用于说明的真实情况
Into:
Currently i have scraped all the data into one PostgreSQL 'Bigtable' table(there are about 1.2M rows). Now i need to split the design into separate tables which all have dependency on the Bigtable. Some of the tables might have subtables. The model looks pretty much like snowflake.
Problem:
What would be best option to inserting data into tables? I thought to make the insertion with functions written in 'SQL' or PLgSQL. But the problem is still with auto-generated ID-s.
Also if you know what tools might make this problem solving easier then post!
//Edit i have added example, this not the real case just for illustration
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1.2 M 行并不算太多。最好的工具是从控制台“psql”执行的sql脚本。如果您有较新版本的 Pg,那么您可以在必要时使用内联函数(DO 语句)。但最有用的命令可能是 INSERT INTO SELECT 语句。
1.2 M rows is not too much. The best tool is sql script executed from console "psql". If you have a some newer version of Pg, then you can use inline functions (DO statement) when it is necessary. But probably the most useful command is INSERT INTO SELECT statement.
如果我理解你的问题,你可以使用这样的 psql 函数:
在这种情况下,假设每个表都运行自己的主键序列,并且我减少了表中的字段数量以简化。
我希望你有所帮助。
If I understand your question, you can use a psql function like this:
In this case it's assumed that each table runs its own primary key sequence and I have reduced the number of fields in the tables to simplify.
I hope you have been helpful.
不需要为此使用函数(除非我误解了你的问题)
如果你的 id 列全部定义为
serial
列(即它们自动生成值),那么这可以通过简单的 INSERT 语句来完成。这假设目标表都是空的。No need to use a function for this (unless I misunderstood your problem)
If your id columns are all defined as
serial
column (i.e. they automatically generate the values), then this can be done with simple INSERT statements. This assumes that the target tables are all empty.