#!/usr/bin/env sysbench
require("oltp_common")
local function get_table_num()
return sysbench.rand.uniform(1, sysbench.opt.tables)
end
local function get_id()
return sysbench.rand.default(1, sysbench.opt.table_size)
end
local function get_large_templete()
local large_temp = ""
for i=1,1024 do
large_temp = large_temp .. "#########-#########-#########-#########-#########-"
end
return large_temp
end
local large_template = get_large_templete()
local function get_large_value()
return sysbench.rand.string(large_template)
end
function large_create_table(drv, con, table_num)
if drv:name() == "mysql"
then
print(string.format("Creating mysql table 'sbtest%d'...", table_num))
else
error("Unsupported database driver:" .. drv:name())
end
query = string.format([[
CREATE TABLE sbtest%d(
id int not null auto_increment,
k INTEGER DEFAULT '0' NOT NULL,
c longtext,
pad longtext,
primary key (id)
) ]],table_num)
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
query = "INSERT INTO sbtest" .. table_num .. "(k, c, pad) VALUES"
con:bulk_insert_init(query)
for i = 1, sysbench.opt.table_size do
local large_value = get_large_value()
query = string.format("(%d, '%s', '%s')",
sysbench.rand.default(1, sysbench.opt.table_size),
large_value, large_value)
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
function execute_delete_inserts_large()
local tnum = get_table_num()
for i = 1, sysbench.opt.delete_inserts do
local id = get_id()
local k = get_id()
param[tnum].deletes[1]:set(id)
param[tnum].inserts[1]:set(id)
param[tnum].inserts[2]:set(k)
param[tnum].inserts[3]:set_rand_str(large_template)
param[tnum].inserts[4]:set_rand_str(large_template)
stmt[tnum].deletes:execute()
stmt[tnum].inserts:execute()
end
end
function execute_non_index_updates_large()
local tnum = get_table_num()
for i = 1, sysbench.opt.non_index_updates do
param[tnum].non_index_updates[1]:set_rand_str(large_template)
param[tnum].non_index_updates[2]:set(get_id())
stmt[tnum].non_index_updates:execute()
end
end
local t = sysbench.sql.type
local stmt_defs = {
point_selects = {
"SELECT c FROM sbtest%u WHERE id=?",
t.INT},
simple_ranges = {
"SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ?",
t.INT, t.INT},
sum_ranges = {
"SELECT SUM(k) FROM sbtest%u WHERE id BETWEEN ? AND ?",
t.INT, t.INT},
order_ranges = {
"SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c",
t.INT, t.INT},
distinct_ranges = {
"SELECT DISTINCT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c",
t.INT, t.INT},
index_updates = {
"UPDATE sbtest%u SET k=k+1 WHERE id=?",
t.INT},
non_index_updates = {
"UPDATE sbtest%u SET c=? WHERE id=?",
{t.CHAR, 51200}, t.INT},
deletes = {
"DELETE FROM sbtest%u WHERE id=?",
t.INT},
inserts = {
"INSERT INTO sbtest%u (id, k, c, pad) VALUES (?, ?, ?, ?)",
t.INT, t.INT, {t.CHAR, 51200}, {t.CHAR, 51200}},
}
function prepare_for_each_large_table(key)
for t = 1, sysbench.opt.tables do
stmt[t][key] = con:prepare(string.format(stmt_defs[key][1], t))
local nparam = #stmt_defs[key] - 1
if nparam > 0 then
param[t][key] = {}
end
for p = 1, nparam do
local btype = stmt_defs[key][p+1]
local len
if type(btype) == "table" then
len = btype[2]
btype = btype[1]
end
if btype == sysbench.sql.type.VARCHAR or
btype == sysbench.sql.type.CHAR then
param[t][key][p] = stmt[t][key]:bind_create(btype, len)
else
param[t][key][p] = stmt[t][key]:bind_create(btype)
end
end
if nparam > 0 then
stmt[t][key]:bind_param(unpack(param[t][key]))
end
end
end
create_table = large_create_table
prepare_for_each_table = prepare_for_each_large_table
execute_delete_inserts = execute_delete_inserts_large
execute_non_index_updates = execute_non_index_updates_large
function prepare_statements()
if not sysbench.opt.skip_trx then
prepare_begin()
prepare_commit()
end
prepare_index_updates()
prepare_non_index_updates()
prepare_delete_inserts()
end
function event()
if not sysbench.opt.skip_trx then
begin()
end
execute_index_updates()
execute_non_index_updates()
execute_delete_inserts()
if not sysbench.opt.skip_trx then
commit()
end
end