随机生成一千个数据插入large_db中
# 这是一个示例 Python 脚本。
# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。
import pandas as pd
from sqlalchemy import create_engine
from faker import Faker
# 初始化faker对象
faker = Faker()
data = []
# 生成虚拟数据
for _ in range(1000):
data.append([faker.name(), faker.address(), faker.email()])
df = pd.DataFrame(data, columns=['name', 'address', 'email'])
# 创建数据库引擎
engine = create_engine("mysql+pymysql://root:123456@localhost/large_db")
# 将DataFrame写入MySQL数据库中的'users'表
df.to_sql('users', con=engine, if_exists='append', index=False, chunksize=100)
# 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助
优化查询
分页查询:
USE large_db;
SET @start_time=Now0;
SELECT SOL_NO_CACHE *FROM users Limit 100 OFFSET 1000;
SET @end_time=Nowd;
SELECT TIMEDIFF(@end_time,@start_time)AS query_duration;
索引查询:
CREATE INDEX idx_ name ON user(name);
CREATE INDEX idx_email ON users(email);
SET @start_time=Now();
SEIELCT SOL_NO_CACHE*FROM users WHERE name=“John Doe”;
SET @end_time=Now();
SELECT TIMEDIFF(@end_time,@start_time)AS query_duration;
在pycharm中版本查询:
import pymysql
# 连接MySQL数据库
connection = pymysql.connect(
host='localhost',
user='your_username', # 替换为你的MySQL用户名
password='your_password', # 替换为你的MySQL密码
db='your_database', # 替换为你的数据库名称
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# 执行SQL查询
sql = "SELECT VERSION()"
cursor.execute(sql)
result = cursor.fetchone()
print("Database version:", result['VERSION()'])
finally:
connection.close()
单字段查询:
import pymysql
# 连接MySQL数据库
connection = pymysql.connect(
host='localhost',
user='your_username', # 替换为你的MySQL用户名
password='your_password', # 替换为你的MySQL密码
db='your_database', # 替换为你的数据库名称
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# 执行SQL查询
sql = "SELECT * FROM users WHERE name = %s"
cursor.execute(sql, ('John Doe',))
result = cursor.fetchall()
for row in result:
print(row)
finally:
connection.close()
插入数据:
import pymysql
# 连接MySQL数据库
connection = pymysql.connect(
host='localhost',
user='your_username', # 替换为你的MySQL用户名
password='your_password', # 替换为你的MySQL密码
db='your_database', # 替换为你的数据库名称
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# 执行SQL插入
sql = "INSERT INTO users (name, address, email) VALUES (%s, %s, %s)"
cursor.execute(sql, ('John Doe', '123 Main St', 'john.doe@example.com'))
# 提交事务
connection.commit()
finally:
connection.close()
数据更新:
import pymysql
# 连接MySQL数据库
connection = pymysql.connect(
host='localhost',
user='your_username', # 替换为你的MySQL用户名
password='your_password', # 替换为你的MySQL密码
db='your_database', # 替换为你的数据库名称
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# 执行SQL更新
sql = "UPDATE users SET address = %s WHERE name = %s"
cursor.execute(sql, ('456 Oak St', 'John Doe'))
# 提交事务
connection.commit()
finally:
connection.close()
删除数据:
import pymysql
# 连接MySQL数据库
connection = pymysql.connect(
host='localhost',
user='your_username', # 替换为你的MySQL用户名
password='your_password', # 替换为你的MySQL密码
db='your_database', # 替换为你的数据库名称
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# 执行SQL删除
sql = "DELETE FROM users WHERE name = %s"
cursor.execute(sql, ('John Doe',))
# 提交事务
connection.commit()
finally:
connection.close()
单字段查询: