Postgresql Insert loop execution time is too long

16 ビュー (過去 30 日間)
Vincent
Vincent 2023 年 3 月 22 日
コメント済み: Vincent 2023 年 3 月 24 日
Hi,
I'm currently trying to save test data into a PostGreSQL database. I loop on my table and insert my data as the following :
for i=1:size(Data,1)
exec(obj.conn,['INSERT INTO...........'])
end
The size of my data is about 234000 rows. The copy in the database take 2h30...
I'm using the database toolbox
Thank you in advance for you help !

採用された回答

Sachin
Sachin 2023 年 3 月 23 日
Hi
I understand that you are facing an issue with execution time. The following information might be of good help to you.
Inserting data into a PostgreSQL data row by row can be a slow process, especially if you are dealing with a large amount of data.
One way to improve the performance is to use the COPY command instead of INSERT statements.
Here is an example of how you could use the COPY command in MATLAB.
  1. Create a CSV file with your data.
  2. Use the COPY command to load the data into the database.
exec(conn, COPY mytable FROM /path to myData.csvWITH (FORMAT CSV);
You can find more information about COPY command in PostgreSQL here :
  1 件のコメント
Vincent
Vincent 2023 年 3 月 24 日
Worked really well, thanks !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by