syntax for COPY in postgresql

拈花ヽ惹草 提交于 2019-12-04 02:56:11

问题


INSERT INTO contacts_lists (contact_id, list_id)
          SELECT contact_id, 67544
          FROM plain_contacts

Here I want to use Copy command instead of Insert command in sql to reduce the time to insert values. I fetched the data using select operation. How can i insert it into a table using Copy command in postgresql. Could you please give an example for it?. Or any other suggestion in order to achieve the reduction of time to insert the values.


回答1:


As your rows are already in the database (because you apparently can SELECT them), then using COPY will not increase the speed in any way.

To be able to use COPY you have to first write the values into a text file, which is then read into the database. But if you can SELECT them, writing to a textfile is a completely unnecessary step and will slow down your insert, not increase its speed

Your statement is as fast as it gets. The only thing that might speed it up (apart from buying a faster harddisk) is to remove any potential index on contact_lists that contains the column contact_id or list_id and re-create the index once the insert is finished.




回答2:


You can find the syntax described in many places, I'm sure. One of those is this wiki article.

It looks like it would basically be:

COPY plain_contacts (contact_id, 67544) TO some_file

And

COPY contacts_lists (contact_id, list_id) FROM some_file

But I'm just reading from the resources that Google turned up. Give it a try and post back if you need help with a specific problem.



来源:https://stackoverflow.com/questions/5778005/syntax-for-copy-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!