github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/tpcc/ddls.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tpcc
    12  
    13  import (
    14  	gosql "database/sql"
    15  	"fmt"
    16  
    17  	"github.com/cockroachdb/errors"
    18  	"golang.org/x/sync/errgroup"
    19  )
    20  
    21  const (
    22  	// WAREHOUSE table.
    23  	tpccWarehouseSchema = `(
    24  		w_id        integer   not null primary key,
    25  		w_name      varchar(10),
    26  		w_street_1  varchar(20),
    27  		w_street_2  varchar(20),
    28  		w_city      varchar(20),
    29  		w_state     char(2),
    30  		w_zip       char(9),
    31  		w_tax       decimal(4,4),
    32  		w_ytd       decimal(12,2)
    33  	)`
    34  
    35  	// DISTRICT table.
    36  	tpccDistrictSchemaBase = `(
    37  		d_id         integer       not null,
    38  		d_w_id       integer       not null,
    39  		d_name       varchar(10),
    40  		d_street_1   varchar(20),
    41  		d_street_2   varchar(20),
    42  		d_city       varchar(20),
    43  		d_state      char(2),
    44  		d_zip        char(9),
    45  		d_tax        decimal(4,4),
    46  		d_ytd        decimal(12,2),
    47  		d_next_o_id  integer,
    48  		primary key (d_w_id, d_id)
    49  	)`
    50  	tpccDistrictSchemaInterleaveSuffix = `
    51  		interleave in parent warehouse (d_w_id)`
    52  
    53  	// CUSTOMER table.
    54  	tpccCustomerSchemaBase = `(
    55  		c_id           integer        not null,
    56  		c_d_id         integer        not null,
    57  		c_w_id         integer        not null,
    58  		c_first        varchar(16),
    59  		c_middle       char(2),
    60  		c_last         varchar(16),
    61  		c_street_1     varchar(20),
    62  		c_street_2     varchar(20),
    63  		c_city         varchar(20),
    64  		c_state        char(2),
    65  		c_zip          char(9),
    66  		c_phone        char(16),
    67  		c_since        timestamp,
    68  		c_credit       char(2),
    69  		c_credit_lim   decimal(12,2),
    70  		c_discount     decimal(4,4),
    71  		c_balance      decimal(12,2),
    72  		c_ytd_payment  decimal(12,2),
    73  		c_payment_cnt  integer,
    74  		c_delivery_cnt integer,
    75  		c_data         varchar(500),
    76  		primary key (c_w_id, c_d_id, c_id),
    77  		index customer_idx (c_w_id, c_d_id, c_last, c_first)
    78  	)`
    79  	tpccCustomerSchemaInterleaveSuffix = `
    80  		interleave in parent district (c_w_id, c_d_id)`
    81  
    82  	// HISTORY table.
    83  	tpccHistorySchemaBase = `(
    84  		rowid    uuid    not null default gen_random_uuid(),
    85  		h_c_id   integer not null,
    86  		h_c_d_id integer not null,
    87  		h_c_w_id integer not null,
    88  		h_d_id   integer not null,
    89  		h_w_id   integer not null,
    90  		h_date   timestamp,
    91  		h_amount decimal(6,2),
    92  		h_data   varchar(24),
    93  		primary key (h_w_id, rowid)`
    94  	tpccHistorySchemaFkSuffix = `
    95  		index history_customer_fk_idx (h_c_w_id, h_c_d_id, h_c_id),
    96  		index history_district_fk_idx (h_w_id, h_d_id)`
    97  
    98  	// ORDER table.
    99  	tpccOrderSchemaBase = `(
   100  		o_id         integer      not null,
   101  		o_d_id       integer      not null,
   102  		o_w_id       integer      not null,
   103  		o_c_id       integer,
   104  		o_entry_d    timestamp,
   105  		o_carrier_id integer,
   106  		o_ol_cnt     integer,
   107  		o_all_local  integer,
   108  		primary key  (o_w_id, o_d_id, o_id DESC),
   109  		unique index order_idx (o_w_id, o_d_id, o_c_id, o_id DESC) storing (o_entry_d, o_carrier_id)
   110  	)`
   111  	tpccOrderSchemaInterleaveSuffix = `
   112  		interleave in parent district (o_w_id, o_d_id)`
   113  
   114  	// NEW-ORDER table.
   115  	tpccNewOrderSchema = `(
   116  		no_o_id  integer   not null,
   117  		no_d_id  integer   not null,
   118  		no_w_id  integer   not null,
   119  		primary key (no_w_id, no_d_id, no_o_id)
   120  	)`
   121  	// This natural-seeming interleave makes performance worse, because this
   122  	// table has a ton of churn and produces a lot of MVCC tombstones, which
   123  	// then will gum up the works of scans over the parent table.
   124  	// tpccNewOrderSchemaInterleaveSuffix = `
   125  	// 	interleave in parent "order" (no_w_id, no_d_id, no_o_id)`
   126  
   127  	// ITEM table.
   128  	tpccItemSchema = `(
   129  		i_id     integer      not null,
   130  		i_im_id  integer,
   131  		i_name   varchar(24),
   132  		i_price  decimal(5,2),
   133  		i_data   varchar(50),
   134  		primary key (i_id)
   135  	)`
   136  
   137  	// STOCK table.
   138  	tpccStockSchemaBase = `(
   139  		s_i_id       integer       not null,
   140  		s_w_id       integer       not null,
   141  		s_quantity   integer,
   142  		s_dist_01    char(24),
   143  		s_dist_02    char(24),
   144  		s_dist_03    char(24),
   145  		s_dist_04    char(24),
   146  		s_dist_05    char(24),
   147  		s_dist_06    char(24),
   148  		s_dist_07    char(24),
   149  		s_dist_08    char(24),
   150  		s_dist_09    char(24),
   151  		s_dist_10    char(24),
   152  		s_ytd        integer,
   153  		s_order_cnt  integer,
   154  		s_remote_cnt integer,
   155  		s_data       varchar(50),
   156  		primary key (s_w_id, s_i_id)`
   157  	tpccStockSchemaFkSuffix = `
   158  		index stock_item_fk_idx (s_i_id)`
   159  	tpccStockSchemaInterleaveSuffix = `
   160  		interleave in parent warehouse (s_w_id)`
   161  
   162  	// ORDER-LINE table.
   163  	tpccOrderLineSchemaBase = `(
   164  		ol_o_id         integer   not null,
   165  		ol_d_id         integer   not null,
   166  		ol_w_id         integer   not null,
   167  		ol_number       integer   not null,
   168  		ol_i_id         integer   not null,
   169  		ol_supply_w_id  integer,
   170  		ol_delivery_d   timestamp,
   171  		ol_quantity     integer,
   172  		ol_amount       decimal(6,2),
   173  		ol_dist_info    char(24),
   174  		primary key (ol_w_id, ol_d_id, ol_o_id DESC, ol_number)`
   175  	tpccOrderLineSchemaFkSuffix = `
   176  		index order_line_stock_fk_idx (ol_supply_w_id, ol_i_id)`
   177  	tpccOrderLineSchemaInterleaveSuffix = `
   178  		interleave in parent "order" (ol_w_id, ol_d_id, ol_o_id)`
   179  )
   180  
   181  func maybeAddFkSuffix(fks bool, base, suffix string) string {
   182  	const endSchema = "\n\t)"
   183  	if !fks {
   184  		return base + endSchema
   185  	}
   186  	return base + "," + suffix + endSchema
   187  }
   188  
   189  func maybeAddInterleaveSuffix(interleave bool, base, suffix string) string {
   190  	if !interleave {
   191  		return base
   192  	}
   193  	return base + suffix
   194  }
   195  
   196  func scatterRanges(db *gosql.DB) error {
   197  	tables := []string{
   198  		`customer`,
   199  		`district`,
   200  		`history`,
   201  		`item`,
   202  		`new_order`,
   203  		`"order"`,
   204  		`order_line`,
   205  		`stock`,
   206  		`warehouse`,
   207  	}
   208  
   209  	var g errgroup.Group
   210  	for _, table := range tables {
   211  		g.Go(func() error {
   212  			sql := fmt.Sprintf(`ALTER TABLE %s SCATTER`, table)
   213  			if _, err := db.Exec(sql); err != nil {
   214  				return errors.Wrapf(err, "Couldn't exec %q", sql)
   215  			}
   216  			return nil
   217  		})
   218  	}
   219  	return g.Wait()
   220  }