-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (71 loc) · 2.32 KB
/
main.py
File metadata and controls
81 lines (71 loc) · 2.32 KB
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
import mysql.connector
import uuid
from faker import Faker
from dotenv import load_dotenv
import random
from datetime import datetime, timedelta
import os
# Load environment variables
load_dotenv()
# Connection settings
HOST = os.getenv('host')
USER = os.getenv('user')
PASSWORD = os.getenv('password')
DATABASE = os.getenv('database')
# Connect to the MySQL database
connection = mysql.connector.connect(
host=HOST,
user=USER,
password=PASSWORD,
database=DATABASE
)
cursor = connection.cursor()
fake = Faker()
# Insert 1,000,000 rows into opt_clients
print("Inserting into opt_clients...")
client_insert_query = """
INSERT INTO opt_clients (id, name, surname, email, phone, address, status)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
clients_data = [
(str(uuid.uuid4()), fake.first_name(), fake.last_name(), fake.email(), fake.phone_number(), fake.address(), random.choice(['active', 'inactive']))
for _ in range(100000)
]
cursor.executemany(client_insert_query, clients_data)
connection.commit()
print("Inserted into opt_clients.")
# Insert 1,000 rows into opt_products
print("Inserting into opt_products...")
product_insert_query = """
INSERT INTO opt_products (product_name, product_category, description)
VALUES (%s, %s, %s)
"""
categories = ['Category1', 'Category2', 'Category3', 'Category4', 'Category5']
products_data = [
(fake.word(), random.choice(categories), fake.text())
for _ in range(1000)
]
cursor.executemany(product_insert_query, products_data)
connection.commit()
print("Inserted into opt_products.")
# Insert 10,000,000 rows into opt_orders
print("Inserting into opt_orders...")
order_insert_query = """
INSERT INTO opt_orders (order_date, client_id, product_id)
VALUES (%s, %s, %s)
"""
order_date_start = datetime.now() - timedelta(days=365 * 5)
orders_data = [
(order_date_start + timedelta(days=random.randint(0, 365 * 5)), random.choice(clients_data)[0], random.randint(1, 1000))
for _ in range(1000000)
]
# Use chunks to avoid memory issues
chunk_size = 10000
for i in range(0, len(orders_data), chunk_size):
cursor.executemany(order_insert_query, orders_data[i:i + chunk_size])
connection.commit()
print(f"Inserted {i + chunk_size} rows into opt_orders...")
print("Inserted into opt_orders.")
# Close the cursor and connection
cursor.close()
connection.close()