$ time python3 foo.py
real 10m6.379s
user 0m0.612s
sys 0m0.174s
$ time BATCHED=true python3 foo.py
real 0m27.215s
user 0m0.487s
sys 0m0.131s
95.5% reduction in time if all those CREATE TABLE
statements are sent in bulk.
from google.cloud import spanner_v1 as spanner | |
def main(): | |
client = spanner.Client() | |
ins = client.instance('django-tests') | |
if not ins.exists(): | |
ins.configuration_name = 'projects/appdev-soda-spanner-staging/instanceConfigs/regional-us-west2' | |
_ = ins.create() | |
db = ins.database('dbx') | |
if not db.exists(): | |
db.create() | |
stmts = [ | |
"CREATE TABLE django_content_type (id INT64 NOT NULL, app_label STRING(100) NOT NULL, model STRING(100) NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_permission (id INT64 NOT NULL, name STRING(255) NOT NULL, content_type_id INT64 NOT NULL, codename STRING(100) NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_group (id INT64 NOT NULL, name STRING(150) NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_group_permissions (id INT64 NOT NULL, group_id INT64 NOT NULL, permission_id INT64 NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_user (id INT64 NOT NULL, password STRING(128) NOT NULL, last_login TIMESTAMP, is_superuser BOOL NOT NULL, username STRING(150) NOT NULL, first_name STRING(30) NOT NULL, last_name STRING(150) NOT NULL, email STRING(254) NOT NULL, is_staff BOOL NOT NULL, is_active BOOL NOT NULL, date_joined TIMESTAMP NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_user_groups (id INT64 NOT NULL, user_id INT64 NOT NULL, group_id INT64 NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE auth_user_user_permissions (id INT64 NOT NULL, user_id INT64 NOT NULL, permission_id INT64 NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE django_session (session_key STRING(40) NOT NULL, session_data STRING(MAX) NOT NULL, expire_date TIMESTAMP NOT NULL) PRIMARY KEY(session_key)", | |
"CREATE TABLE basic_article (id INT64 NOT NULL, headline STRING(100) NOT NULL, pub_date TIMESTAMP NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE basic_featuredarticle (id INT64 NOT NULL, article_id INT64 NOT NULL) PRIMARY KEY(id)", | |
"CREATE TABLE basic_selfref (id INT64 NOT NULL, selfref_id INT64, article_id INT64) PRIMARY KEY(id)", | |
"CREATE UNIQUE INDEX django_content_type_app_label_model_76bd3d3b_uniq ON django_content_type (app_label, model)", | |
"CREATE UNIQUE INDEX auth_permission_content_type_id_codename_01ab375a_uniq ON auth_permission (content_type_id, codename)", | |
"CREATE INDEX auth_permission_content_type_id_2f476e4b ON auth_permission (content_type_id)", | |
"CREATE UNIQUE INDEX auth_group_permissions_group_id_permission_id_0cd325b0_uniq ON auth_group_permissions (group_id, permission_id)", | |
"CREATE INDEX auth_group_permissions_group_id_b120cbf9 ON auth_group_permissions (group_id)", | |
"CREATE INDEX auth_group_permissions_permission_id_84c5c92e ON auth_group_permissions (permission_id)", | |
"CREATE UNIQUE INDEX auth_user_groups_user_id_group_id_94350c0c_uniq ON auth_user_groups (user_id, group_id)", | |
"CREATE INDEX auth_user_groups_user_id_6a12ed8b ON auth_user_groups (user_id)", | |
"CREATE INDEX auth_user_groups_group_id_97559544 ON auth_user_groups (group_id)", | |
"CREATE UNIQUE INDEX auth_user_user_permissions_user_id_permission_id_14a6b632_uniq ON auth_user_user_permissions (user_id, permission_id)", | |
"CREATE INDEX auth_user_user_permissions_user_id_a95ead1b ON auth_user_user_permissions (user_id)", | |
"CREATE INDEX auth_user_user_permissions_permission_id_1fbb5f2c ON auth_user_user_permissions (permission_id)", | |
"CREATE INDEX django_session_expire_date_a5c62663 ON django_session (expire_date)", | |
"CREATE INDEX basic_selfref_selfref_id_fa437744 ON basic_selfref (selfref_id)", | |
"CREATE INDEX basic_selfref_article_id_b9f2fc8c ON basic_selfref (article_id)", | |
] | |
if os.environ.get('BATCHED'): | |
lro = db.update_ddl(stmts) | |
print(lro.result()) | |
else: # Unbatched | |
for stmt in stmts: | |
lro = db.update_ddl([stmt]) | |
print(lro.result()) | |
db.drop() | |
if __name__ == '__main__': | |
main() |