From facc6186ee5981103b08dce9a0eeed2cd56bcb72 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Thu, 11 Jul 2019 18:07:35 -0400 Subject: [PATCH] workload/tpcc: avoid validating foreign keys after dataset generation Foreign key validation was adding a significant amount of time to the initialization of TPC-C. This was especially true on very large datasets. This commit uses the new (as of #38663) ability to create Foreign Key constraints in the Unvalidated state using the NOT VALID syntax to avoid this extra work. This will work with old versions of CockroachDB as well, because the NOT VALID syntax has been around for a very long time, it was just ignored up until #38663. Release note: None --- pkg/workload/tpcc/tpcc.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/workload/tpcc/tpcc.go b/pkg/workload/tpcc/tpcc.go index 0c7d944022d4..6c8ac5e92a43 100644 --- a/pkg/workload/tpcc/tpcc.go +++ b/pkg/workload/tpcc/tpcc.go @@ -230,17 +230,24 @@ func (w *tpcc) Hooks() workload.Hooks { }, PostLoad: func(db *gosql.DB) error { if w.fks { + // We avoid validating foreign keys because we just generated + // the data set and don't want to scan over the entire thing + // again. Unfortunately, this means that we leave the foreign + // keys unvalidated for the duration of the test, so the SQL + // optimizer can't use them. + // TODO(lucy-zhang): expose an internal knob to validate fk + // relations without performing full validation. See #38833. fkStmts := []string{ - `alter table district add foreign key (d_w_id) references warehouse (w_id)`, - `alter table customer add foreign key (c_w_id, c_d_id) references district (d_w_id, d_id)`, - `alter table history add foreign key (h_c_w_id, h_c_d_id, h_c_id) references customer (c_w_id, c_d_id, c_id)`, - `alter table history add foreign key (h_w_id, h_d_id) references district (d_w_id, d_id)`, - `alter table "order" add foreign key (o_w_id, o_d_id, o_c_id) references customer (c_w_id, c_d_id, c_id)`, - `alter table new_order add foreign key (no_w_id, no_d_id, no_o_id) references "order" (o_w_id, o_d_id, o_id)`, - `alter table stock add foreign key (s_w_id) references warehouse (w_id)`, - `alter table stock add foreign key (s_i_id) references item (i_id)`, - `alter table order_line add foreign key (ol_w_id, ol_d_id, ol_o_id) references "order" (o_w_id, o_d_id, o_id)`, - `alter table order_line add foreign key (ol_supply_w_id, ol_i_id) references stock (s_w_id, s_i_id)`, + `alter table district add foreign key (d_w_id) references warehouse (w_id) not valid`, + `alter table customer add foreign key (c_w_id, c_d_id) references district (d_w_id, d_id) not valid`, + `alter table history add foreign key (h_c_w_id, h_c_d_id, h_c_id) references customer (c_w_id, c_d_id, c_id) not valid`, + `alter table history add foreign key (h_w_id, h_d_id) references district (d_w_id, d_id) not valid`, + `alter table "order" add foreign key (o_w_id, o_d_id, o_c_id) references customer (c_w_id, c_d_id, c_id) not valid`, + `alter table new_order add foreign key (no_w_id, no_d_id, no_o_id) references "order" (o_w_id, o_d_id, o_id) not valid`, + `alter table stock add foreign key (s_w_id) references warehouse (w_id) not valid`, + `alter table stock add foreign key (s_i_id) references item (i_id) not valid`, + `alter table order_line add foreign key (ol_w_id, ol_d_id, ol_o_id) references "order" (o_w_id, o_d_id, o_id) not valid`, + `alter table order_line add foreign key (ol_supply_w_id, ol_i_id) references stock (s_w_id, s_i_id) not valid`, } for _, fkStmt := range fkStmts {