Updating table from temp table with many rows in postgres

George L

I have a main table called repos, and a temp table from a csv, that I want to match the name, and update a value with the new value from the temp table. My code works, but is slow.

CREATE TEMP TABLE tmp(name text, language text);
COPY tmp FROM 'path/to/csv';

UPDATE repos
    SET language = x.language
FROM (
    SELECT * FROM tmp) x
WHERE repos.name = x.name
Gordon Linoff

First, the subquery is unnecessary. It doesn't affect performance but it is awkward:

UPDATE repos
    SET language = x.language
FROM tmp x
WHERE repos.name = x.name;

For this query, you want an index on tmp(name, language):

create index idx_tmp_name_language on tmp(name, language);

Creating an index is usually faster after the table has been populated (as opposed to creating the index first and then loading the table).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

postgres doesn't recognize temp table in function

From Dev

Temp table not dropping from DB

From Dev

Postgres Copy select rows from CSV table

From Dev

How To Delete Rows From Temp Table With EntityDelete In CFSCRIPT

From Dev

Keep inserted rows in Temp table - after a rollback

From Dev

insert duplicate rows in temp table

From Dev

How to insert into a table from temp table?

From Dev

MySQL - Query a temp table to retrieve 2 rows from table

From Dev

Returning values from a temp table if another temp table returns no rows

From Dev

After how many rows is temp table column size determined?

From Dev

Updating a many to many table with SQL

From Dev

Updating table with multiple rows

From Dev

Find missing rows from table through a many to many relation

From Dev

IF statement from a temp table

From Dev

Postgres Copy select rows from CSV table

From Dev

Looping through a SQL QUERY record result from a Temp Table and Updating another table based on SQL QUERY results

From Dev

Updating Records in Temp table by using cursors

From Dev

insert duplicate rows in temp table

From Dev

Temp Table Space "no rows selected"

From Dev

Returning values from a temp table if another temp table returns no rows

From Dev

Copy from one temp table to another temp table on condition base?

From Dev

Insert many rows from a table into one unique row in another table

From Dev

Postgres : get min and max rows count in many to many relation table

From Dev

Dynamically add columns to temp table based on rows in another temp table

From Dev

Get All Rows From a Postgres Table

From Dev

Inserting multiple rows in temp table without loop

From Dev

Postgres select from one to many table to single table rows

From Dev

SQL - Updating rows with individual rows from another table

From Dev

Insert data from a table to a temporary table, and then select from the temp table specific rows

Related Related

  1. 1

    postgres doesn't recognize temp table in function

  2. 2

    Temp table not dropping from DB

  3. 3

    Postgres Copy select rows from CSV table

  4. 4

    How To Delete Rows From Temp Table With EntityDelete In CFSCRIPT

  5. 5

    Keep inserted rows in Temp table - after a rollback

  6. 6

    insert duplicate rows in temp table

  7. 7

    How to insert into a table from temp table?

  8. 8

    MySQL - Query a temp table to retrieve 2 rows from table

  9. 9

    Returning values from a temp table if another temp table returns no rows

  10. 10

    After how many rows is temp table column size determined?

  11. 11

    Updating a many to many table with SQL

  12. 12

    Updating table with multiple rows

  13. 13

    Find missing rows from table through a many to many relation

  14. 14

    IF statement from a temp table

  15. 15

    Postgres Copy select rows from CSV table

  16. 16

    Looping through a SQL QUERY record result from a Temp Table and Updating another table based on SQL QUERY results

  17. 17

    Updating Records in Temp table by using cursors

  18. 18

    insert duplicate rows in temp table

  19. 19

    Temp Table Space "no rows selected"

  20. 20

    Returning values from a temp table if another temp table returns no rows

  21. 21

    Copy from one temp table to another temp table on condition base?

  22. 22

    Insert many rows from a table into one unique row in another table

  23. 23

    Postgres : get min and max rows count in many to many relation table

  24. 24

    Dynamically add columns to temp table based on rows in another temp table

  25. 25

    Get All Rows From a Postgres Table

  26. 26

    Inserting multiple rows in temp table without loop

  27. 27

    Postgres select from one to many table to single table rows

  28. 28

    SQL - Updating rows with individual rows from another table

  29. 29

    Insert data from a table to a temporary table, and then select from the temp table specific rows

HotTag

Archive