Advertisement
AndikaWiratana

mysql_json_implementation

May 15th, 2024
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import mysql.connector
  2. from dotenv import load_dotenv
  3. import json
  4. from pymemcache.client import base
  5. import time
  6.  
  7. load_dotenv()
  8.  
  9. conn = None
  10. mc = base.Client(('localhost','11211'))
  11.  
  12. def main():
  13.     connect()
  14.     init()
  15.     scenario()
  16.  
  17. def connect():
  18.     """ Connect to MySQL database """
  19.     global conn
  20.     try:
  21.         print('Connecting to MySQL database...')
  22.         conn = mysql.connector.connect(host="34.30.23.118", user="root", password='|0zHafT),7"#<H&<', database='model_storage')
  23.  
  24.         if conn.is_connected():
  25.             print('Connection is established.')
  26.         else:
  27.             print('Connection is failed.')
  28.     except mysql.connector.Error as error:
  29.         print(error)
  30.  
  31. def init():
  32.     global conn
  33.     cur = conn.cursor()
  34.  
  35.     cur.execute("CREATE TABLE IF NOT EXISTS students(id INT PRIMARY KEY AUTO_INCREMENT, student_data JSON NOT NULL);")
  36.     cur.execute("CREATE TABLE IF NOT EXISTS lecture(id INT PRIMARY KEY AUTO_INCREMENT, lecture_data JSON NOT NULL);")
  37.     cur.close()
  38.  
  39. def write(data):
  40.     global conn
  41.     cur = conn.cursor()
  42.     cur.execute("INSERT INTO students VALUES (null, '%s')" % data)
  43.  
  44.     conn.commit()
  45.     cur.close()
  46.  
  47. def read_v1(all=False):
  48.     global conn
  49.     cur = conn.cursor()
  50.     sql = "SELECT * FROM students LIMIT 100" if not all else "SELECT * FROM students"
  51.     print(sql)
  52.     cur.execute(sql)
  53.     result = cur.fetchall()
  54.     return result
  55.  
  56. def read_v2():
  57.     if not mc.get("read"):
  58.         global conn
  59.         cur = conn.cursor()
  60.         cur.execute("SELECT * FROM students LIMIT 100")
  61.  
  62.         result = cur.fetchall()
  63.         mc.set("read", json.dumps(result))
  64.         return result
  65.  
  66.     return mc.get("read")
  67.  
  68. def read_id_v1(id=None):
  69.     global conn
  70.     cur = conn.cursor()
  71.     cur.execute("SELECT * FROM students WHERE `student_data`->>'$.nim' = '%s' LIMIT 1" % str(id))
  72.     result = cur.fetchall()
  73.     return result
  74.  
  75. def read_id_v2(id=None):
  76.     if not mc.get("read_%s" % str(id)):
  77.         global conn
  78.         cur = conn.cursor()
  79.         cur.execute("SELECT * FROM students WHERE `student_data`->>'$.nim' = '%s' LIMIT 1" % str(id))
  80.         result = cur.fetchall()
  81.         mc.set("read_%s" % str(id), json.dumps(result))
  82.         return result
  83.  
  84.     return mc.get("read_%s" % str(id))
  85.  
  86. def delete(id=None):
  87.     global conn
  88.     cur = conn.cursor()
  89.     if(id):
  90.         cur.execute("DELETE FROM students WHERE id=%s" % str(id))
  91.     else:
  92.         cur.execute("DELETE FROM students")
  93.     conn.commit()
  94.  
  95.     cur.close()
  96.  
  97. def reset():
  98.     global conn
  99.     cur = conn.cursor()
  100.     cur.execute("TRUNCATE TABLE students")
  101.     cur.execute("TRUNCATE TABLE lecture")
  102.     conn.commit()
  103.  
  104.     cur.close()
  105.  
  106. # Scenario:
  107. # i tried basic read, read with caching, write, and delete
  108. def scenario():
  109.     # reset()
  110.  
  111.     example_data = {
  112.         "name": "andika",
  113.         "nim": 2105551019,
  114.         "lecture": [
  115.             {"name": "alprog", "description": "belajar algoritma"},
  116.             {"name": "tbd", "description": "belajar basis data"},
  117.             {"name": "ml", "description": "belajar machine learning"}
  118.         ]
  119.     }
  120.  
  121.     start = time.time()
  122.     for i in range(5000):
  123.         write(json.dumps(example_data))
  124.     end = time.time()
  125.     print(f"Write 5000 row time taken: {end - start:.6f} seconds")
  126.  
  127.     results = read_v1(all=True)
  128.  
  129.     start = time.time()
  130.     for item in results:
  131.         read_id_v1(json.loads(item[1])['nim'])
  132.     end = time.time()
  133.     print(f"Read 5000 row time taken: {end - start:.6f} seconds")
  134.  
  135.     start = time.time()
  136.     for item in results:
  137.         read_id_v2(json.loads(item[1])['nim'])
  138.     end = time.time()
  139.     print(f"Read with caching 5000 row time taken: {end - start:.6f} seconds")
  140.  
  141.  
  142.     start = time.time()
  143.     for item in results:
  144.         delete(item[0])
  145.     end = time.time()
  146.     print(f"Delete 5000 row time taken: {end - start:.6f} seconds")
  147.  
  148.  
  149. if __name__ == '__main__':
  150.     main()
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement