Skip to content

Instantly share code, notes, and snippets.

@altmannmarcelo
Created August 27, 2023 18:48
Show Gist options
  • Save altmannmarcelo/a71b672a4c1b6c9b30ed2ed66b19295b to your computer and use it in GitHub Desktop.
Save altmannmarcelo/a71b672a4c1b6c9b30ed2ed66b19295b to your computer and use it in GitHub Desktop.
prepare.cc
#include <fstream>
int main() {
/*
# Setup
USE test;
CREATE TABLE `big` (
`ID` int DEFAULT NULL,
`b` blob
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `user` (
`ID` int DEFAULT NULL,
`name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO user VALUES (1, 'marce');
INSERT INTO user VALUES (2, 'marce');
# Readyset
mysql -P 3307 -e "USE test; SELECT COUNT(*) FROM user WHERE name = 'marce';"
mysql -P 3307 -e "USE test; CREATE CACHE FROM SELECT COUNT(*) FROM user WHERE name = 'marce';"
# Run
g++ prepare.cc -o prepare
./prepare
mysql test < my_trx.sql
# Wait until readyset apply all events and check results
# Readyset
mysql -P 3307 -e "USE test; SELECT COUNT(*) FROM user WHERE name = 'marce';"
# MySQL
mysql -e "USE test; SELECT COUNT(*) FROM user WHERE name = 'marce';"
*/
std::ofstream outfile;
outfile.open("my_trx.sql", std::ios_base::app);
std::string query = "INSERT INTO big VALUES (1,\"";
for(int j = 0; j < 1024; j++){
query +="a";
}
query += "\");";
outfile << "PREPARE stmt FROM '";
outfile << query;
outfile << "';\n";
outfile << "PREPARE stmt2 FROM '";
outfile << "INSERT INTO user VALUES (?, \"marce\")';\n";
outfile << "START TRANSACTION;\n";
for(int i=0; i<4*1024*1024; i++) {
if(i % 10240 == 0) {
outfile << "SET @a = ";
outfile << i;
outfile << ";\n";
outfile << "EXECUTE stmt2 USING @a;\n";
}
outfile << "EXECUTE stmt;\n";
}
outfile << "COMMIT;";
outfile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment