Skip to content

Commit

Permalink
workload/tpcc: avoid validating foreign keys after dataset generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nvanbenschoten committed Jul 11, 2019
1 parent 3d822b4 commit facc618
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/workload/tpcc/tpcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit facc618

Please sign in to comment.