Search code examples
google-cloud-platformgoogle-cloud-spanner

Bulk Update a million rows


Suppose I have a million rows in a table. I want to flip a flag in a column from true to false. How do I do that in spanner with a single statement?

That is, I want to achieve the following DML statement.

Update mytable set myflag=true where 1=1;


Solution

  • You can use this open source JDBC driver in combination with a standard JDBC tool like for example SQuirreL or SQL Workbench. Have a look here for a short tutorial on how to use the driver with these tools: http://www.googlecloudspanner.com/2017/10/using-standard-database-tools-with.html

    The JDBC driver supports both DML- and DDL-statements, so this statement should work out-of-the-box:

    Update mytable set myflag=true
    

    DML-statements operating on a large number of rows are supported, but the underlying transaction quotas of Cloud Spanner continue to apply (max 20,000 mutations in one transaction). You can bypass this by setting the AllowExtendedMode=true connection property (see the Wiki-pages of the driver). This breaks a large update into several smaller updates and executes each of these in its own transaction. You can also do this batching yourself by dividing your update statement into several different parts.