人脑要是有个内存,天都能翻了 mysql有各种各样的表类型,基于此的各种变种更是层出不穷,为了应付这种变化,先前曾经基于sysbench1.0做了一些分区表数据生成lua,分享一些基础的函数组合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
require("custom_luas/tdsqlv2/default/oltp_common") function array_range_value_int(table_size, partition_num, enable_maxvalue) local range = math.floor(table_size / partition_num) local min = 1 local max = range local pt_array = {} for i = 1, partition_num do if i == partition_num then if enable_maxvalue then max = "MAXVALUE" else max = table_size + 1 end pt_array[i] = max else pt_array[i] = max min = max + 1 max = min + range - 1 end end return pt_array end function array_range_value_date(partition_num, partition_func) local pt_array = {} if partition_func == nil or partition_func == "" then for i = 1, partition_num do if partition_func == nil or partition_func == "" or partition_func == "day" then pt_array[i] = os.date("%Y%m%d", os.time() + (i - 1) * 86400) end end end if partition_func == "day" then range = math.floor(31 / partition_num) for i = 1, partition_num do if i ~= partition_num then pt_array[i] = i * range else pt_array[i] = 32 end end end if partition_func == "month" then range = math.floor(12 / partition_num) for i = 1, partition_num do if i ~= partition_num then pt_array[i] = i * range else pt_array[i] = 13 end end end if partition_func == "year" then for i = 1, partition_num do pt_array[i] = os.date("%Y", os.time() + (i - 1) * 31536000) end end if partition_func == "tdsql_month" then year = tonumber(os.date("%Y", os.time())) month = tonumber(os.date("%m", os.time())) for i = 1, partition_num do pt_array[i] = string.format("%04d%02d", year, month) if month + 1 > 12 then year = year + 1 month = 1 else month = month + 1 end end end if partition_func == "tdsql_year" then year = tonumber(os.date("%Y", os.time())) for i = 1, partition_num do pt_array[i] = string.format("%04d", year) year = year + 1 end end return pt_array end function range_rand(pt_array, data_type, pt_func) if data_type == "int" then e = math.random(1, #pt_array) if e == 1 then return math.random(1, pt_array[e] - 1) else if pt_array[e] == "MAXVALUE" then return math.random(pt_array[e - 1], 9223372036854775807) end return math.random(pt_array[e - 1], pt_array[e] - 1) end end if data_type == "date" then if pt_func == nil or pt_func == "" then e = math.random(1, #pt_array) min = -90 if e ~= 1 then min = tonumber(pt_array[e-1]) - tonumber(pt_array[e]) end return getNewDate(pt_array[e], math.random(min, -1), "DAY") end if pt_func == "year" then e = math.random(1, #pt_array) min = -3 if e ~= 1 then min = tonumber(pt_array[e-1]) - tonumber(pt_array[e]) end return string.format("%04d%02d%02d",tonumber(pt_array[e]) + math.random(min, -1), math.random(01,12), math.random(01,28)) end if pt_func == "tdsql_month" then e = math.random(1, #pt_array) min = -3 if e ~= 1 then min = tonumber(pt_array[e-1]) - tonumber(pt_array[e]) end return getNewDate(string.format(pt_array[e].."01"), math.random(min, -1)*31, "DAY") end if pt_func == "tdsql_year" then e = math.random(1, #pt_array) min = -3 if e ~= 1 then min = tonumber(pt_array[e-1]) - tonumber(pt_array[e]) end return string.format("%04d%02d%02d", tonumber(pt_array[e]) + math.random(min, -1), math.random(01,12), math.random(01,28)) end end end function range_int(table_size, partition_num, enable_maxvalue) local sub_sql = "" local pt_array = array_range_value_int(table_size, partition_num, enable_maxvalue) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("PARTITION p%d VALUES LESS THAN (%s)", i, pt_array[i]) else sub_sql = string.format("%s, PARTITION p%d VALUES LESS THAN (%s)", sub_sql, i, pt_array[i]) end end return sub_sql end function distributed_range_int(table_size, partition_num, enable_maxvalue) local sub_sql = "" local pt_array = array_range_value_int(table_size, partition_num, enable_maxvalue) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("s%d VALUES LESS THAN (%s)", i, pt_array[i]) else sub_sql = string.format("%s, s%d VALUES LESS THAN (%s)", sub_sql, i, pt_array[i]) end end return sub_sql end function range_date(partition_num, partition_func) local sub_sql = "" local pt_array = array_range_value_date(partition_num, partition_func) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("PARTITION p%d VALUES LESS THAN (%s)", i, pt_array[i]) else sub_sql = string.format("%s, PARTITION p%d VALUES LESS THAN (%s)", sub_sql, i, pt_array[i]) end end return sub_sql end function distributed_range_date(partition_num, partition_func) local sub_sql = "" local pt_array = array_range_value_date(partition_num, partition_func) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("s%d VALUES LESS THAN (%s)", i, pt_array[i]) else sub_sql = string.format("%s, s%d VALUES LESS THAN (%s)", sub_sql, i, pt_array[i]) end end return sub_sql end function array_list_value_int(partition_num, partition_values_num) -- print(string.format("[debug]common.lua/array_list_value_int partition_num: %d, partition_values_num: %d",partition_num, partition_values_num)) local pt_array = {} for i = 1, partition_num do pt_array[i] = {} for j = 1, partition_values_num do while (true) do pt_array[i][j] = j + ((i - 1) * partition_values_num) if pt_array[i][j] ~= nill and string.len(pt_array[i][j]) ~= 0 then break end end end end return pt_array end function array_list_value_date(partition_num, partition_values_num, partition_func) print(string.format("[debug] partition_num: %d, partition_values_num: %d, partition_function: %s", partition_num, partition_values_num, partition_func)) local pt_array = {} --无日期函数 if partition_func == "" or partition_func == nil then for i = 1, partition_num do pt_array[i] = {} for j = 1, partition_values_num do pt_array[i][j] = os.date("%Y%m%d", os.time() + (j - 1) * 86400 + (i - 1) * partition_values_num * 86400) end end return pt_array end -- 只考虑mysql的day,tdsql的变种day模式暂时不考虑 if partition_func == "day" then local range = math.floor(31 / partition_num) local day_idx = 1 for i = 1, partition_num do local val_array = {} if i ~= partition_num then for j = 1, range do val_array[j] = day_idx day_idx = day_idx + 1 end else j = 1 while day_idx <= 31 do val_array[j] = day_idx day_idx = day_idx + 1 j = j + 1 end end pt_array[i] = val_array end return pt_array end if partition_func == "month" then local range = math.floor(12 / partition_num) local day_idx = 1 for i = 1, partition_num do local val_array = {} if i ~= partition_num then for j = 1, range do val_array[j] = day_idx day_idx = day_idx + 1 end else j = 1 while day_idx <= 12 do val_array[j] = day_idx day_idx = day_idx + 1 j = j + 1 end end pt_array[i] = val_array end return pt_array end if partition_func == "year" then for i = 1, partition_num do pt_array[i] = {} for j = 1, partition_values_num do while (true) do pt_array[i][j] = os.date("%Y", os.time() + (j - 1) * 31536000 + (i - 1) * partition_values_num * 31536000) if pt_array[i][j] ~= nill and string.len(pt_array[i][j]) ~= 0 then break end end end end return pt_array end if partition_func == "tdsql_month" then for i=1, partition_num do pt_array[i] = {} for j=1, partition_values_num do pt_array[i][j] = os.date("%Y%m", os.time() + (j - 1) * 86400 * 31 + (i - 1) * partition_values_num * 86400 * 31) end end return pt_array end if partition_func == "tdsql_year" then for i=1, partition_num do pt_array[i] = {} for j=1, partition_values_num do while (true) do pt_array[i][j] = os.date("%Y", os.time() + (j - 1) * 31536000 + (i - 1) * partition_values_num * 31536000) if pt_array[i][j] ~= nill and string.len(pt_array[i][j]) ~= 0 then break end end end end return pt_array end return pt_array end function list_int(partition_num, partition_values_num) local sub_sql = "" local pt_array = array_list_value_int(partition_num, partition_values_num) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("PARTITION p%d VALUES IN (%s)", i, table.concat(pt_array[i], ",")) else sub_sql = string.format("%s, PARTITION p%d VALUES IN (%s)", sub_sql, i, table.concat(pt_array[i], ",")) end end return sub_sql end function distributed_list_int(partition_num, partition_values_num) local sub_sql = "" local pt_array = array_list_value_int(partition_num, partition_values_num) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("s%d VALUES IN (%s)", i, table.concat(pt_array[i], ",")) else sub_sql = string.format("%s, s%d VALUES IN (%s)", sub_sql, i, table.concat(pt_array[i], ",")) end end return sub_sql end function group_partition_alias(partition_num, group_num) if group_num > partition_num then assert(false, "group_num must be equal or less than partition_num") end array_alis = {} alis_num = math.floor(partition_num/group_num) s_idx=1 for i = 1, group_num do array_alis[i] = {} j = 1 if i ~= group_num then while #array_alis[i] ~= alis_num do array_alis[i][j] = string.format("s%d", s_idx) s_idx = s_idx + 1 j = j + 1 end else while s_idx <= partition_num do array_alis[i][j] = string.format("s%d", s_idx) s_idx = s_idx + 1 j = j + 1 end end end return array_alis end function distributed_list_int_multipartition(partition_num, partition_values_num, group_num) local sub_sql = "" local pt_array = array_list_value_int(group_num, partition_values_num) local array_alis = group_partition_alias(partition_num, group_num) for i = 1, group_num do if string.len(sub_sql) == 0 then sub_sql = string.format("WHEN g%d VALUES IN (%s) THEN tdsql_subdistributed by hash(id) (%s)", i, table.concat(pt_array[i], ","), table.concat(array_alis[i], ",")) else sub_sql = string.format("%s, WHEN g%d VALUES IN (%s) THEN tdsql_subdistributed by hash(id) (%s)", sub_sql, i, table.concat(pt_array[i], ","), table.concat(array_alis[i], ",")) end end return sub_sql end function list_date(partition_num, partition_values_num, pt_func) local sub_sql = "" local pt_array = array_list_value_date(partition_num, partition_values_num, pt_func) --debug -- for i=1, partition_num do -- print(string.format("[debug]pt_array[%d]: %s",i, table.concat(pt_array[i], ","))) -- end for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("PARTITION p%d VALUES IN (%s)", i, table.concat(pt_array[i], ",")) else sub_sql = string.format("%s, PARTITION p%d VALUES IN (%s)", sub_sql, i, table.concat(pt_array[i], ",")) end -- sub_sql = sub_sql .. string.format("PARTITION p%d VALUES IN (%s)", i, table.concat(pt_array[i], ",")) -- if i ~= partition_num then -- sub_sql = sub_sql .. "," -- end end return sub_sql end function distributed_list_date(partition_num, partition_values_num, partition_func) local sub_sql = "" local pt_array = array_list_value_date(partition_num, partition_values_num, partition_func) for i = 1, partition_num do if string.len(sub_sql) == 0 then sub_sql = string.format("s%d VALUES IN (%s)", i, table.concat(pt_array[i], ",")) else sub_sql = string.format("%s, s%d VALUES IN (%s)", sub_sql, i, table.concat(pt_array[i], ",")) end end return sub_sql end function distributed_list_date_multipartition(partition_num, partition_values_num, group_num) local sub_sql = "" local pt_array = array_list_value_date(group_num, partition_values_num) local array_alis = group_partition_alias(partition_num, group_num) for i = 1, group_num do if string.len(sub_sql) == 0 then sub_sql = string.format("WHEN g%d VALUES IN (%s) THEN tdsql_subdistributed by hash(id) (%s)", i, table.concat(pt_array[i], ","), table.concat(array_alis[i], ",")) else sub_sql = string.format("%s, WHEN g%d VALUES IN (%s) THEN tdsql_subdistributed by hash(id) (%s)", sub_sql, i, table.concat(pt_array[i], ","), table.concat(array_alis[i], ",")) end end return sub_sql end function getNewDate(srcDateTime, interval, dateUnit) --从日期字符串中截取出年月日时分秒 local Y = string.sub(srcDateTime,1,4) local M = string.sub(srcDateTime,5,6) local D = string.sub(srcDateTime,7,8) local H = string.sub(srcDateTime,9,10) local MM = string.sub(srcDateTime,11,12) local SS = string.sub(srcDateTime,13,14) --把日期时间字符串转换成对应的日期时间 local dt1 = os.time{year=Y, month=M, day=D, hour=H,min=MM,sec=SS} --根据时间单位和偏移量得到具体的偏移数据 local ofset=0 if dateUnit =='DAY' then ofset = 60 *60 * 24 * interval elseif dateUnit == 'HOUR' then ofset = 60 *60 * interval elseif dateUnit == 'MINUTE' then ofset = 60 * interval elseif dateUnit == 'SECOND' then ofset = interval end --指定的时间+时间偏移量 local newTime = os.date("%Y%m%d", dt1 + tonumber(ofset)) return newTime end |
使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
-- require("oltp_common") require("custom_luas/tdsqlv2/default/common") function create_table(drv, con, table_num) local id_index_def, id_def local engine_def = "" local extra_table_options = "" local query if sysbench.opt.secondary then id_index_def = "KEY xid" else id_index_def = "PRIMARY KEY" end if drv:name() == "mysql" then if sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL AUTO_INCREMENT" else id_def = "INTEGER NOT NULL" end engine_def = "/*! ENGINE = " .. sysbench.opt.mysql_storage_engine .. " */" elseif drv:name() == "pgsql" then if not sysbench.opt.auto_inc then id_def = "INTEGER NOT NULL" elseif pgsql_variant == 'redshift' then id_def = "INTEGER IDENTITY(1,1)" else id_def = "SERIAL" end else error("Unsupported database driver:" .. drv:name()) end print(string.format("Creating table 'sbtest%d'...", table_num)) -- 分区sql local sub_partition = list_date(sysbench.opt.partition_num, sysbench.opt.partition_values_num) -- list分区值数组 local pt_values_array_array = array_list_value_date(sysbench.opt.partition_num, sysbench.opt.partition_values_num) local pt_values_array = {} for i = 1, sysbench.opt.partition_num do for j = 1, sysbench.opt.partition_values_num do table.insert(pt_values_array, pt_values_array_array[i][j]) end end query = string.format([[ CREATE TABLE sbtest%d( id %s, k INTEGER DEFAULT '0' NOT NULL, c CHAR(120) DEFAULT '' NOT NULL, pad CHAR(60) DEFAULT '' NOT NULL, pt DATE DEFAULT '2024-01-01' NOT NULL, %s (id) ) %s %s shardkey=id partition by list(day(pt))(%s)]], table_num, id_def, id_index_def, engine_def, sysbench.opt.create_table_options, sub_partition) con:query(query) if (sysbench.opt.table_size > 0) then print(string.format("Inserting %d records into 'sbtest%d'", sysbench.opt.table_size, table_num)) end if sysbench.opt.auto_inc then query = "INSERT INTO sbtest" .. table_num .. "(k, c, pad, pt) VALUES" else query = "INSERT INTO sbtest" .. table_num .. "(id, k, c, pad, pt) VALUES" end con:bulk_insert_init(query) local c_val local pad_val for i = 1, sysbench.opt.table_size do c_val = get_c_value() pad_val = get_pad_value() if (sysbench.opt.auto_inc) then query = string.format("(%d, '%s', '%s', '%s')", sysbench.rand.default(1, sysbench.opt.table_size), c_val, pad_val, pt_values_array[math.random(1, #pt_values_array)]) else query = string.format("(%d, %d, '%s', '%s', '%s')", i, sysbench.rand.default(1, sysbench.opt.table_size), c_val, pad_val, pt_values_array[math.random(1, #pt_values_array)]) end con:bulk_insert_next(query) end con:bulk_insert_done() if sysbench.opt.create_secondary then print(string.format("Creating a secondary index on 'sbtest%d'...", table_num)) con:query(string.format("CREATE INDEX k_%d ON sbtest%d(k)", table_num, table_num)) end end |