BenchmarkSQL 性能测试

测试方法

ShardingSphere Proxy 支持通过 BenchmarkSQL 5.0 进行 TPC-C 测试。 除本文说明的内容外,BenchmarkSQL 操作步骤按照原文档 HOW-TO-RUN.txt 即可。

测试工具微调

与单机数据库压测不同,分布式数据库解决方案难免在功能支持上有所取舍。使用 BenchmarkSQL 压测 ShardingSphere Proxy 建议进行如下调整。

移除外键与 extraHistID

修改 BenchmarkSQL 目录下 run/runDatabaseBuild.sh,文件第 17 行。

修改前:

  1. AFTER_LOAD="indexCreates foreignKeys extraHistID buildFinish"

修改后:

  1. AFTER_LOAD="indexCreates buildFinish"

压测相关参数建议

ShardingSphere 数据分片建议

对 BenchmarkSQL 的数据分片,可以考虑以 warehouse id 作为分片键。其中一个表 bmsql_item 没有 warehouse id,可以取 i_id 作为分片键。

BenchmarkSQL 中有如下 SQL 涉及多表:

  1. SELECT c_discount, c_last, c_credit, w_tax
  2. FROM bmsql_customer
  3. JOIN bmsql_warehouse ON (w_id = c_w_id)
  4. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?
  1. SELECT o_id, o_entry_d, o_carrier_id
  2. FROM bmsql_oorder
  3. WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ?
  4. AND o_id = (
  5. SELECT max(o_id)
  6. FROM bmsql_oorder
  7. WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ?
  8. )

如果以 warehouse id 作为分片键,以上 SQL 涉及的表可以配置为 bindingTable:

  1. rules:
  2. - !SHARDING
  3. bindingTables:
  4. - bmsql_warehouse, bmsql_customer
  5. - bmsql_stock, bmsql_district, bmsql_order_line

以 warehouse id 为分片键的数据分片配置可以参考本文附录。

PostgreSQL JDBC URL 参数建议

对 BenchmarkSQL 所使用的配置文件中的 JDBC URL 进行调整,即参数名 conn 的值。 增加参数 defaultRowFetchSize=1 可能减少 Delivery 业务耗时。

props.pg 文件节选,建议修改的位置为第 3 行 conn 的参数值:

  1. db=postgres
  2. driver=org.postgresql.Driver
  3. conn=jdbc:postgresql://localhost:5432/postgres?defaultRowFetchSize=1
  4. user=benchmarksql
  5. password=PWbmsql
  6. warehouses=1
  7. loadWorkers=4
  8. terminals=1

ShardingSphere Proxy server.yaml 参数建议

proxy-backend-query-fetch-size 参数值默认值为 -1,修改为 1000 可能减少 Delivery 业务耗时。

server.yaml 文件节选:

  1. props:
  2. proxy-backend-query-fetch-size: 1000

其他参数如 max-connections-size-per-query 等可以在压测过程中适当增大,比如取 Actual tables 最大的数量。 假如有个表分 4 库 x 4 表,共 16 个表,参数值可以尝试取 16。 实际效果与取决于数据分片方式,如果分片配置能够让所有 SQL 都路由到单点,该参数可能对性能没有影响。

附录

BenchmarkSQL 数据分片参考配置

Pool size 请根据实际压测情况适当调整。

  1. schemaName: bmsql_sharding
  2. dataSources:
  3. ds_0:
  4. url: jdbc:postgresql://db0.ip:5432/bmsql
  5. username: postgres
  6. password: postgres
  7. connectionTimeoutMilliseconds: 3000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 1000
  11. minPoolSize: 1000
  12. ds_1:
  13. url: jdbc:postgresql://db1.ip:5432/bmsql
  14. username: postgres
  15. password: postgres
  16. connectionTimeoutMilliseconds: 3000
  17. idleTimeoutMilliseconds: 60000
  18. maxLifetimeMilliseconds: 1800000
  19. maxPoolSize: 1000
  20. minPoolSize: 1000
  21. ds_2:
  22. url: jdbc:postgresql://db2.ip:5432/bmsql
  23. username: postgres
  24. password: postgres
  25. connectionTimeoutMilliseconds: 3000
  26. idleTimeoutMilliseconds: 60000
  27. maxLifetimeMilliseconds: 1800000
  28. maxPoolSize: 1000
  29. minPoolSize: 1000
  30. ds_3:
  31. url: jdbc:postgresql://db3.ip:5432/bmsql
  32. username: postgres
  33. password: postgres
  34. connectionTimeoutMilliseconds: 3000
  35. idleTimeoutMilliseconds: 60000
  36. maxLifetimeMilliseconds: 1800000
  37. maxPoolSize: 1000
  38. minPoolSize: 1000
  39. rules:
  40. - !SHARDING
  41. bindingTables:
  42. - bmsql_warehouse, bmsql_customer
  43. - bmsql_stock, bmsql_district, bmsql_order_line
  44. defaultDatabaseStrategy:
  45. none:
  46. defaultTableStrategy:
  47. none:
  48. keyGenerators:
  49. snowflake:
  50. props:
  51. worker-id: 123
  52. type: SNOWFLAKE
  53. tables:
  54. bmsql_config:
  55. actualDataNodes: ds_0.bmsql_config
  56. bmsql_warehouse:
  57. actualDataNodes: ds_${0..3}.bmsql_warehouse
  58. databaseStrategy:
  59. standard:
  60. shardingColumn: w_id
  61. shardingAlgorithmName: bmsql_warehouse_database_inline
  62. bmsql_district:
  63. actualDataNodes: ds_${0..3}.bmsql_district
  64. databaseStrategy:
  65. standard:
  66. shardingColumn: d_w_id
  67. shardingAlgorithmName: bmsql_district_database_inline
  68. bmsql_customer:
  69. actualDataNodes: ds_${0..3}.bmsql_customer
  70. databaseStrategy:
  71. standard:
  72. shardingColumn: c_w_id
  73. shardingAlgorithmName: bmsql_customer_database_inline
  74. bmsql_item:
  75. actualDataNodes: ds_${0..3}.bmsql_item
  76. databaseStrategy:
  77. standard:
  78. shardingColumn: i_id
  79. shardingAlgorithmName: bmsql_item_database_inline
  80. bmsql_history:
  81. actualDataNodes: ds_${0..3}.bmsql_history
  82. databaseStrategy:
  83. standard:
  84. shardingColumn: h_w_id
  85. shardingAlgorithmName: bmsql_history_database_inline
  86. bmsql_oorder:
  87. actualDataNodes: ds_${0..3}.bmsql_oorder_${0..3}
  88. databaseStrategy:
  89. standard:
  90. shardingColumn: o_w_id
  91. shardingAlgorithmName: bmsql_oorder_database_inline
  92. tableStrategy:
  93. standard:
  94. shardingColumn: o_c_id
  95. shardingAlgorithmName: bmsql_oorder_table_inline
  96. bmsql_stock:
  97. actualDataNodes: ds_${0..3}.bmsql_stock
  98. databaseStrategy:
  99. standard:
  100. shardingColumn: s_w_id
  101. shardingAlgorithmName: bmsql_stock_database_inline
  102. bmsql_new_order:
  103. actualDataNodes: ds_${0..3}.bmsql_new_order
  104. databaseStrategy:
  105. standard:
  106. shardingColumn: no_w_id
  107. shardingAlgorithmName: bmsql_new_order_database_inline
  108. bmsql_order_line:
  109. actualDataNodes: ds_${0..3}.bmsql_order_line
  110. databaseStrategy:
  111. standard:
  112. shardingColumn: ol_w_id
  113. shardingAlgorithmName: bmsql_order_line_database_inline
  114. shardingAlgorithms:
  115. bmsql_warehouse_database_inline:
  116. type: INLINE
  117. props:
  118. algorithm-expression: ds_${w_id & 3}
  119. bmsql_district_database_inline:
  120. type: INLINE
  121. props:
  122. algorithm-expression: ds_${d_w_id & 3}
  123. bmsql_customer_database_inline:
  124. type: INLINE
  125. props:
  126. algorithm-expression: ds_${c_w_id & 3}
  127. bmsql_item_database_inline:
  128. type: INLINE
  129. props:
  130. algorithm-expression: ds_${i_id & 3}
  131. bmsql_history_database_inline:
  132. type: INLINE
  133. props:
  134. algorithm-expression: ds_${h_w_id & 3}
  135. bmsql_oorder_database_inline:
  136. type: INLINE
  137. props:
  138. algorithm-expression: ds_${o_w_id & 3}
  139. bmsql_oorder_table_inline:
  140. type: INLINE
  141. props:
  142. algorithm-expression: bmsql_oorder_${o_c_id & 3}
  143. bmsql_stock_database_inline:
  144. type: INLINE
  145. props:
  146. algorithm-expression: ds_${s_w_id & 3}
  147. bmsql_new_order_database_inline:
  148. type: INLINE
  149. props:
  150. algorithm-expression: ds_${no_w_id & 3}
  151. bmsql_order_line_database_inline:
  152. type: INLINE
  153. props:
  154. algorithm-expression: ds_${ol_w_id & 3}

BenchmarkSQL 5.0 PostgreSQL 语句列表

Create tables

  1. create table bmsql_config (
  2. cfg_name varchar(30) primary key,
  3. cfg_value varchar(50)
  4. );
  5. create table bmsql_warehouse (
  6. w_id integer not null,
  7. w_ytd decimal(12,2),
  8. w_tax decimal(4,4),
  9. w_name varchar(10),
  10. w_street_1 varchar(20),
  11. w_street_2 varchar(20),
  12. w_city varchar(20),
  13. w_state char(2),
  14. w_zip char(9)
  15. );
  16. create table bmsql_district (
  17. d_w_id integer not null,
  18. d_id integer not null,
  19. d_ytd decimal(12,2),
  20. d_tax decimal(4,4),
  21. d_next_o_id integer,
  22. d_name varchar(10),
  23. d_street_1 varchar(20),
  24. d_street_2 varchar(20),
  25. d_city varchar(20),
  26. d_state char(2),
  27. d_zip char(9)
  28. );
  29. create table bmsql_customer (
  30. c_w_id integer not null,
  31. c_d_id integer not null,
  32. c_id integer not null,
  33. c_discount decimal(4,4),
  34. c_credit char(2),
  35. c_last varchar(16),
  36. c_first varchar(16),
  37. c_credit_lim decimal(12,2),
  38. c_balance decimal(12,2),
  39. c_ytd_payment decimal(12,2),
  40. c_payment_cnt integer,
  41. c_delivery_cnt integer,
  42. c_street_1 varchar(20),
  43. c_street_2 varchar(20),
  44. c_city varchar(20),
  45. c_state char(2),
  46. c_zip char(9),
  47. c_phone char(16),
  48. c_since timestamp,
  49. c_middle char(2),
  50. c_data varchar(500)
  51. );
  52. create sequence bmsql_hist_id_seq;
  53. create table bmsql_history (
  54. hist_id integer,
  55. h_c_id integer,
  56. h_c_d_id integer,
  57. h_c_w_id integer,
  58. h_d_id integer,
  59. h_w_id integer,
  60. h_date timestamp,
  61. h_amount decimal(6,2),
  62. h_data varchar(24)
  63. );
  64. create table bmsql_new_order (
  65. no_w_id integer not null,
  66. no_d_id integer not null,
  67. no_o_id integer not null
  68. );
  69. create table bmsql_oorder (
  70. o_w_id integer not null,
  71. o_d_id integer not null,
  72. o_id integer not null,
  73. o_c_id integer,
  74. o_carrier_id integer,
  75. o_ol_cnt integer,
  76. o_all_local integer,
  77. o_entry_d timestamp
  78. );
  79. create table bmsql_order_line (
  80. ol_w_id integer not null,
  81. ol_d_id integer not null,
  82. ol_o_id integer not null,
  83. ol_number integer not null,
  84. ol_i_id integer not null,
  85. ol_delivery_d timestamp,
  86. ol_amount decimal(6,2),
  87. ol_supply_w_id integer,
  88. ol_quantity integer,
  89. ol_dist_info char(24)
  90. );
  91. create table bmsql_item (
  92. i_id integer not null,
  93. i_name varchar(24),
  94. i_price decimal(5,2),
  95. i_data varchar(50),
  96. i_im_id integer
  97. );
  98. create table bmsql_stock (
  99. s_w_id integer not null,
  100. s_i_id integer not null,
  101. s_quantity integer,
  102. s_ytd integer,
  103. s_order_cnt integer,
  104. s_remote_cnt integer,
  105. s_data varchar(50),
  106. s_dist_01 char(24),
  107. s_dist_02 char(24),
  108. s_dist_03 char(24),
  109. s_dist_04 char(24),
  110. s_dist_05 char(24),
  111. s_dist_06 char(24),
  112. s_dist_07 char(24),
  113. s_dist_08 char(24),
  114. s_dist_09 char(24),
  115. s_dist_10 char(24)
  116. );

Create indexes

  1. alter table bmsql_warehouse add constraint bmsql_warehouse_pkey
  2. primary key (w_id);
  3. alter table bmsql_district add constraint bmsql_district_pkey
  4. primary key (d_w_id, d_id);
  5. alter table bmsql_customer add constraint bmsql_customer_pkey
  6. primary key (c_w_id, c_d_id, c_id);
  7. create index bmsql_customer_idx1
  8. on bmsql_customer (c_w_id, c_d_id, c_last, c_first);
  9. alter table bmsql_oorder add constraint bmsql_oorder_pkey
  10. primary key (o_w_id, o_d_id, o_id);
  11. create unique index bmsql_oorder_idx1
  12. on bmsql_oorder (o_w_id, o_d_id, o_carrier_id, o_id);
  13. alter table bmsql_new_order add constraint bmsql_new_order_pkey
  14. primary key (no_w_id, no_d_id, no_o_id);
  15. alter table bmsql_order_line add constraint bmsql_order_line_pkey
  16. primary key (ol_w_id, ol_d_id, ol_o_id, ol_number);
  17. alter table bmsql_stock add constraint bmsql_stock_pkey
  18. primary key (s_w_id, s_i_id);
  19. alter table bmsql_item add constraint bmsql_item_pkey
  20. primary key (i_id);

New Order 业务

stmtNewOrderSelectWhseCust

  1. UPDATE bmsql_district
  2. SET d_next_o_id = d_next_o_id + 1
  3. WHERE d_w_id = ? AND d_id = ?

stmtNewOrderSelectDist

  1. SELECT d_tax, d_next_o_id
  2. FROM bmsql_district
  3. WHERE d_w_id = ? AND d_id = ?
  4. FOR UPDATE

stmtNewOrderUpdateDist

  1. UPDATE bmsql_district
  2. SET d_next_o_id = d_next_o_id + 1
  3. WHERE d_w_id = ? AND d_id = ?

stmtNewOrderInsertOrder

  1. INSERT INTO bmsql_oorder (
  2. o_id, o_d_id, o_w_id, o_c_id, o_entry_d,
  3. o_ol_cnt, o_all_local)
  4. VALUES (?, ?, ?, ?, ?, ?, ?)

stmtNewOrderInsertNewOrder

  1. INSERT INTO bmsql_new_order (
  2. no_o_id, no_d_id, no_w_id)
  3. VALUES (?, ?, ?)

stmtNewOrderSelectStock

  1. SELECT s_quantity, s_data,
  2. s_dist_01, s_dist_02, s_dist_03, s_dist_04,
  3. s_dist_05, s_dist_06, s_dist_07, s_dist_08,
  4. s_dist_09, s_dist_10
  5. FROM bmsql_stock
  6. WHERE s_w_id = ? AND s_i_id = ?
  7. FOR UPDATE

stmtNewOrderSelectItem

  1. SELECT i_price, i_name, i_data
  2. FROM bmsql_item
  3. WHERE i_id = ?

stmtNewOrderUpdateStock

  1. UPDATE bmsql_stock
  2. SET s_quantity = ?, s_ytd = s_ytd + ?,
  3. s_order_cnt = s_order_cnt + 1,
  4. s_remote_cnt = s_remote_cnt + ?
  5. WHERE s_w_id = ? AND s_i_id = ?

stmtNewOrderInsertOrderLine

  1. INSERT INTO bmsql_order_line (
  2. ol_o_id, ol_d_id, ol_w_id, ol_number,
  3. ol_i_id, ol_supply_w_id, ol_quantity,
  4. ol_amount, ol_dist_info)
  5. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

Payment 业务

stmtPaymentSelectWarehouse

  1. SELECT w_name, w_street_1, w_street_2, w_city,
  2. w_state, w_zip
  3. FROM bmsql_warehouse
  4. WHERE w_id = ?

stmtPaymentSelectDistrict

  1. SELECT d_name, d_street_1, d_street_2, d_city,
  2. d_state, d_zip
  3. FROM bmsql_district
  4. WHERE d_w_id = ? AND d_id = ?

stmtPaymentSelectCustomerListByLast

  1. SELECT c_id
  2. FROM bmsql_customer
  3. WHERE c_w_id = ? AND c_d_id = ? AND c_last = ?
  4. ORDER BY c_first

stmtPaymentSelectCustomer

  1. SELECT c_first, c_middle, c_last, c_street_1, c_street_2,
  2. c_city, c_state, c_zip, c_phone, c_since, c_credit,
  3. c_credit_lim, c_discount, c_balance
  4. FROM bmsql_customer
  5. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?
  6. FOR UPDATE

stmtPaymentSelectCustomerData

  1. SELECT c_data
  2. FROM bmsql_customer
  3. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?

stmtPaymentUpdateWarehouse

  1. UPDATE bmsql_warehouse
  2. SET w_ytd = w_ytd + ?
  3. WHERE w_id = ?

stmtPaymentUpdateDistrict

  1. UPDATE bmsql_district
  2. SET d_ytd = d_ytd + ?
  3. WHERE d_w_id = ? AND d_id = ?

stmtPaymentUpdateCustomer

  1. UPDATE bmsql_customer
  2. SET c_balance = c_balance - ?,
  3. c_ytd_payment = c_ytd_payment + ?,
  4. c_payment_cnt = c_payment_cnt + 1
  5. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?

stmtPaymentUpdateCustomerWithData

  1. UPDATE bmsql_customer
  2. SET c_balance = c_balance - ?,
  3. c_ytd_payment = c_ytd_payment + ?,
  4. c_payment_cnt = c_payment_cnt + 1,
  5. c_data = ?
  6. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?

stmtPaymentInsertHistory

  1. INSERT INTO bmsql_history (
  2. h_c_id, h_c_d_id, h_c_w_id, h_d_id, h_w_id,
  3. h_date, h_amount, h_data)
  4. VALUES (?, ?, ?, ?, ?, ?, ?, ?)

Order Status 业务

stmtOrderStatusSelectCustomerListByLast

  1. SELECT c_id
  2. FROM bmsql_customer
  3. WHERE c_w_id = ? AND c_d_id = ? AND c_last = ?
  4. ORDER BY c_first

stmtOrderStatusSelectCustomer

  1. SELECT c_first, c_middle, c_last, c_balance
  2. FROM bmsql_customer
  3. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?

stmtOrderStatusSelectLastOrder

  1. SELECT o_id, o_entry_d, o_carrier_id
  2. FROM bmsql_oorder
  3. WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ?
  4. AND o_id = (
  5. SELECT max(o_id)
  6. FROM bmsql_oorder
  7. WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ?
  8. )

stmtOrderStatusSelectOrderLine

  1. SELECT ol_i_id, ol_supply_w_id, ol_quantity,
  2. ol_amount, ol_delivery_d
  3. FROM bmsql_order_line
  4. WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ?
  5. ORDER BY ol_w_id, ol_d_id, ol_o_id, ol_number

Stock level 业务

stmtStockLevelSelectLow

  1. SELECT count(*) AS low_stock FROM (
  2. SELECT s_w_id, s_i_id, s_quantity
  3. FROM bmsql_stock
  4. WHERE s_w_id = ? AND s_quantity < ? AND s_i_id IN (
  5. SELECT ol_i_id
  6. FROM bmsql_district
  7. JOIN bmsql_order_line ON ol_w_id = d_w_id
  8. AND ol_d_id = d_id
  9. AND ol_o_id >= d_next_o_id - 20
  10. AND ol_o_id < d_next_o_id
  11. WHERE d_w_id = ? AND d_id = ?
  12. )
  13. ) AS L

Delivery BG 业务

stmtDeliveryBGSelectOldestNewOrder

  1. SELECT no_o_id
  2. FROM bmsql_new_order
  3. WHERE no_w_id = ? AND no_d_id = ?
  4. ORDER BY no_o_id ASC

stmtDeliveryBGDeleteOldestNewOrder

  1. DELETE FROM bmsql_new_order
  2. WHERE no_w_id = ? AND no_d_id = ? AND no_o_id = ?

stmtDeliveryBGSelectOrder

  1. SELECT o_c_id
  2. FROM bmsql_oorder
  3. WHERE o_w_id = ? AND o_d_id = ? AND o_id = ?

stmtDeliveryBGUpdateOrder

  1. UPDATE bmsql_oorder
  2. SET o_carrier_id = ?
  3. WHERE o_w_id = ? AND o_d_id = ? AND o_id = ?

stmtDeliveryBGSelectSumOLAmount

  1. SELECT sum(ol_amount) AS sum_ol_amount
  2. FROM bmsql_order_line
  3. WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ?

stmtDeliveryBGUpdateOrderLine

  1. UPDATE bmsql_order_line
  2. SET ol_delivery_d = ?
  3. WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ?

stmtDeliveryBGUpdateCustomer

  1. UPDATE bmsql_customer
  2. SET c_balance = c_balance + ?,
  3. c_delivery_cnt = c_delivery_cnt + 1
  4. WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?