Created
September 19, 2022 02:04
-
-
Save kmuthukk/452c85bbd6459c7b8c3cb465bde32a93 to your computer and use it in GitHub Desktop.
Script to creates lots of tables in a colocated database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependencies: | |
# On CentOS you can install psycopg2 thus: | |
# | |
# sudo yum install postgresql-libs | |
# sudo yum install python-psycopg2 | |
import psycopg2 | |
from multiprocessing.dummy import Pool as ThreadPool | |
import time | |
host = "change-to-ip" | |
password = "change-to-password" | |
def drop_and_create_db(db_name): | |
connect_string = "host={} dbname=yugabyte user=yugabyte password={} port=5433".format(host, password); | |
conn = psycopg2.connect(connect_string) | |
conn.set_session(autocommit=True) | |
cur = conn.cursor() | |
print("dropping database if exists {}".format(db_name)) | |
cur.execute("DROP DATABASE IF EXISTS {}".format(db_name)) | |
print("dropped database if exists {}".format(db_name)) | |
cur.execute("CREATE DATABASE {} COLOCATED=true".format(db_name)) | |
print("created database {} COLOCATED=true".format(db_name)) | |
def create_tables(dbname, num_tables): | |
connect_string = "host={} dbname=mydb user=yugabyte password={} port=5433".format(host, password); | |
conn = psycopg2.connect(connect_string) | |
conn.set_session(autocommit=True) | |
cur = conn.cursor() | |
for idx in range(num_tables): | |
t1 = time.time(); | |
cur.execute(("CREATE TABLE IF NOT EXISTS t_{}" | |
+ "(k int primary key, v1 text, v2 text)" | |
).format(idx)) | |
t2 = time.time(); | |
delta_ms = (t2 - t1) * 1000.0 | |
print("Created table t_{} in {} ms".format(idx, delta_ms)) | |
# Main | |
num_tables = 60000 | |
db_name = "mydb" | |
drop_and_create_db(db_name) | |
create_tables(db_name, num_tables) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment