MySQL update every single row from a 3 million row table

Rn2dy

Requirements: I have to scan the whole table and update every single record, period.

As suggested by other people, I should create a temporary table with identical schema with the original one and do insertion on updated values, then rename table instead of updating on original one.

The idea is something like this:

ResultSet row = select * from old_table;
While row.next
  do something to update values in this row
  insert updated values in to a identical table (different name of course)
endWhile

The question here is I am using Java JDBC, and I have to deal with ResultSet object. So is there a way to prevent "ResultSet row = select * from old_table" generate a out of memory exception?

A potential solution is to paging, but that means I have to use ORDER BY and LIMIT, which can be very slow on a 3 millions rows table.

Is there some trick with ResultSet, like specifying some of the flags like FOWARD_ONLY | NON-SCROLLABLE, etc. Or does Mysql server have some configuration to do smart things like somehow mysql understands that I am doing a full table scan, so just sequentially return records for me, but not all at once.

Any suggestion is welcomed

[UPDATE] seems like MySQL connector/J has configuration parameter called useCursorFetch, if set to true then statement.setFetchSize(1000) will work. Not sure if this is the ultimate solution or not.

Bill Karwin

You can set a JDBC statement to non-buffered this way:

stmt.setFetchSize(Integer.MIN_VALUE); 

But I would also suggest tying to use an INSERT... SELECT statement, so you don't have to use a while loop and don't have to fetch anything. If you can use SQL expressions for your step of "do something to update values" then you can do the whole operation in one SQL statement.

PS: You'll have to be more specific about QuerySet. I find multiple classes called QuerySet in different libraries, e.g. org.dbunit.ant.QuerySet, org.gusdb.wdk.model.QuerySet, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to update an INT column in every single row in many multi-million row tables efficiently

From Dev

update table column of every row in mysql

From Dev

MySQL loop for every row and update

From Dev

MySQL update a row but a single field

From Dev

MySQL update a row but a single field

From Dev

update mysql table row with unique row id

From Dev

Adding a new row in a table after every 3 images from database

From Dev

mysql query to retrieve every single row of data

From Dev

How to find single row data from joined table in mysql?

From Dev

MySQL select photos linked with a single row from different table

From Dev

MySQL: combine 2 rows from another table into a single result row

From Dev

Update row in first table and insert a row into a second table in a single query

From Dev

MySQL: Update every second row string

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

MySQL loop through every row (big table)

From Dev

SQL - Delete table with 372 million rows starting from first row

From Dev

SQL Server previous and next date from a million row table

From Dev

Update every row in a table by setting a column to the sum of two other columns in MySql

From Dev

PrimeFaces update single row of datatable from dialogue

From Dev

How to run same query on every single row of a table? (MVC)

From Dev

MySQL - Update table with row number per group

From Dev

Update large number of row in MySQL table

From Dev

Update mysql table if row excist if not add new

From Dev

MySQL - Update table with row number per group

From Dev

extracting row values from a single table with a condition

From Dev

getting single row from table in sqlite android

From Dev

Multiple row count from single table

From Dev

Returning single row from pivot table

From Dev

extracting row values from a single table with a condition

Related Related

  1. 1

    How to update an INT column in every single row in many multi-million row tables efficiently

  2. 2

    update table column of every row in mysql

  3. 3

    MySQL loop for every row and update

  4. 4

    MySQL update a row but a single field

  5. 5

    MySQL update a row but a single field

  6. 6

    update mysql table row with unique row id

  7. 7

    Adding a new row in a table after every 3 images from database

  8. 8

    mysql query to retrieve every single row of data

  9. 9

    How to find single row data from joined table in mysql?

  10. 10

    MySQL select photos linked with a single row from different table

  11. 11

    MySQL: combine 2 rows from another table into a single result row

  12. 12

    Update row in first table and insert a row into a second table in a single query

  13. 13

    MySQL: Update every second row string

  14. 14

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  15. 15

    MySQL loop through every row (big table)

  16. 16

    SQL - Delete table with 372 million rows starting from first row

  17. 17

    SQL Server previous and next date from a million row table

  18. 18

    Update every row in a table by setting a column to the sum of two other columns in MySql

  19. 19

    PrimeFaces update single row of datatable from dialogue

  20. 20

    How to run same query on every single row of a table? (MVC)

  21. 21

    MySQL - Update table with row number per group

  22. 22

    Update large number of row in MySQL table

  23. 23

    Update mysql table if row excist if not add new

  24. 24

    MySQL - Update table with row number per group

  25. 25

    extracting row values from a single table with a condition

  26. 26

    getting single row from table in sqlite android

  27. 27

    Multiple row count from single table

  28. 28

    Returning single row from pivot table

  29. 29

    extracting row values from a single table with a condition

HotTag

Archive