简体   繁体   中英

Postgres: How to update the column?

I use the postgres library to work with the database. I have a very large database. I need to change the character in the column. I do it like this:

  void replace(String col, String from, String to) async {
    String queryStr = "UPDATE $tab SET $col = replace($col, '$from', '$to');";
    await connection.query(queryStr);
  }

If the database has more than 1,000,000 rows, I get an error:

Unhandled exception: TimeoutException after 0:00:30.000000: Future not completed 0 _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin._enqueue (package:postgres/src/connection.dart:402:24) 1 _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin.query (package:postgres/src/connection.dart:318:12) 2 PG.replace (file:///home/vas/IdeaProjects/infovizion_platform/pg/bin/pg.dart:50:22) 3 main (file:///home/vas/IdeaProjects/infovizion_platform/pg/bin/main.dart:81:14) 4 _startIsolate. (dart:isolate/runtime/libisolate_patch.dart:287:32) 5 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

When I do the same requests manually there is no error.

How can I fix this?

You might try this version:

UPDATE $tab
    SET $col = replace($col, '$from', '$to')
    WHERE $col LIKE '%' || '$from' || '%';

That is, only update columns where the value actually appears, instead of all column.

Reducing the number of columns that actually change may speed the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM