完成作业7第二题的代码

This commit is contained in:
typingbugs 2024-05-29 08:22:09 +08:00
parent 8dd5a17062
commit 875a5dcd61
5 changed files with 100463 additions and 0 deletions

View File

@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "30712dff-7471-4a6f-a464-aa4305036b9a",
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"import random\n",
"from tqdm import tqdm\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"N = 20000"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "480e12b1-02a4-4e89-9a13-1b147b075372",
"metadata": {},
"outputs": [],
"source": [
"db = pymysql.connect(\n",
" host='127.0.0.1', user='kejingfan', \n",
" password='KJF2811879', database='TESTDB'\n",
")\n",
"cursor = db.cursor()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "91fb704d-4104-4cf1-86cc-5ef7a2f06601",
"metadata": {},
"outputs": [],
"source": [
"sql_statements = [\n",
" \"SET profiling = 1;\",\n",
" \n",
" \"DROP TABLE IF EXISTS passengers_no_index;\",\n",
" \"DROP TABLE IF EXISTS passengers_with_index;\",\n",
" \"\"\"\n",
" CREATE TABLE passengers_no_index (\n",
" ID BIGINT PRIMARY KEY,\n",
" `Name` VARCHAR (255) NOT NULL,\n",
" Phone_number BIGINT NOT NULL,\n",
" `Password` VARCHAR (255) NOT NULL,\n",
" CHECK (ID REGEXP '^\\\\\\\\d{18}$'),\n",
" CHECK (Phone_number REGEXP '^\\\\\\\\d{11}$')\n",
" );\n",
" \"\"\",\n",
" \"\"\"\n",
" CREATE TABLE passengers_with_index (\n",
" ID BIGINT PRIMARY KEY,\n",
" `Name` VARCHAR (255) NOT NULL,\n",
" Phone_number BIGINT NOT NULL,\n",
" `Password` VARCHAR (255) NOT NULL,\n",
" CHECK (ID REGEXP '^\\\\\\\\d{18}$'),\n",
" CHECK (Phone_number REGEXP '^\\\\\\\\d{11}$')\n",
" );\n",
" \"\"\",\n",
" \"CREATE INDEX idx_phone_number ON passengers_with_index (Phone_number);\",\n",
"]\n",
"\n",
"for sql in sql_statements:\n",
" cursor.execute(sql)\n",
"db.commit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "100a5382",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"操作数据表 passengers_no_index \n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 71%|████████████████████████████████████████████████████████████████████████████████ | 14166/20000 [02:56<01:19, 73.12it/s]"
]
}
],
"source": [
"id_list = random.sample(range(100000000000000000, 1000000000000000000), N)\n",
"phone_number_list = random.sample(range(10000000000, 20000000000), N)\n",
"\n",
"insert_times = {\n",
" 'passengers_no_index': [],\n",
" 'passengers_with_index': []\n",
"}\n",
"query_times = {\n",
" 'passengers_no_index': [],\n",
" 'passengers_with_index': []\n",
"}\n",
"\n",
"for table_name in ['passengers_no_index', 'passengers_with_index']:\n",
" print(f\"操作数据表 {table_name} \")\n",
" insert_sql = f'''\n",
" INSERT INTO {table_name} (ID, `Name`, Phone_number, `Password`)\n",
" VALUES (%s, %s, %s, %s);\n",
" '''\n",
" query_sql = f'''\n",
" SELECT * FROM {table_name}\n",
" WHERE Phone_number = %s;\n",
" '''\n",
" \n",
" for i in tqdm(range(N)):\n",
" cursor.execute(insert_sql, (id_list[i], 'kejingfan', phone_number_list[i], 'password'))\n",
" db.commit()\n",
" cursor.execute(\"SHOW PROFILES;\")\n",
" profile = cursor.fetchall()[-2]\n",
" if \"INSERT INTO\" in profile[2]:\n",
" insert_times[table_name].append(profile[1])\n",
"\n",
" cursor.execute(query_sql, (phone_number_list[random.randint(0, i)],))\n",
" cursor.execute(\"SHOW PROFILES;\")\n",
" profile = cursor.fetchall()[-1]\n",
" if \"SELECT * FROM\" in profile[2]:\n",
" query_times[table_name].append(profile[1])\n",
"\n",
"cursor.close()\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3783d8dd",
"metadata": {},
"outputs": [],
"source": [
"def get_average_per_n(data, n):\n",
" return [np.mean(data[i:i + n]) for i in range(0, len(data), n)]\n",
"\n",
"avg_insert_times_no_index = get_average_per_n(insert_times['passengers_no_index'], 100)\n",
"avg_insert_times_with_index = get_average_per_n(insert_times['passengers_with_index'], 100)\n",
"avg_query_times_no_index = get_average_per_n(query_times['passengers_no_index'], 100)\n",
"avg_query_times_with_index = get_average_per_n(query_times['passengers_with_index'], 100)\n",
"\n",
"plt.figure(figsize=(14, 7))\n",
"\n",
"plt.subplot(2, 1, 1)\n",
"plt.plot(range(len(avg_insert_times_no_index)), avg_insert_times_no_index, label='No Index Insert Time')\n",
"plt.plot(range(len(avg_insert_times_with_index)), avg_insert_times_with_index, label='With Index Insert Time')\n",
"plt.xlabel('Number of Insertions (in hundreds)')\n",
"plt.ylabel('Time (s)')\n",
"plt.title('Average Insert Time vs Number of Insertions')\n",
"plt.legend()\n",
"\n",
"plt.subplot(2, 1, 2)\n",
"plt.plot(range(len(avg_query_times_no_index)), avg_query_times_no_index, label='No Index Query Time')\n",
"plt.plot(range(len(avg_query_times_with_index)), avg_query_times_with_index, label='With Index Query Time')\n",
"plt.xlabel('Number of Queries (in hundreds)')\n",
"plt.ylabel('Time (s)')\n",
"plt.title('Average Query Time vs Number of Queries')\n",
"plt.legend()\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,24 @@
DROP TABLE IF EXISTS passengers_no_index;
DROP TABLE IF EXISTS passengers_with_index;
CREATE TABLE passengers_no_index (
ID BIGINT PRIMARY KEY,
`Name` VARCHAR(255) NOT NULL,
Phone_number BIGINT UNIQUE NOT NULL,
`Password` VARCHAR(255) NOT NULL,
CHECK (ID REGEXP '^\d{18}$'),
CHECK (Phone_number REGEXP '^\d{11}$')
);
CREATE TABLE passengers_with_index (
ID BIGINT PRIMARY KEY,
`Name` VARCHAR(255) NOT NULL,
Phone_number BIGINT UNIQUE NOT NULL,
`Password` VARCHAR(255) NOT NULL,
CHECK (ID REGEXP '^\d{18}$'),
CHECK (Phone_number REGEXP '^\d{11}$')
);
CREATE INDEX idx_phone_number ON passengers_with_index (Phone_number);
SET profiling = 1;

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
blinker==1.8.1
cffi==1.16.0
click==8.1.7
configparser==7.0.0
cryptography==42.0.6
Flask==3.0.3
itsdangerous==2.2.0
Jinja2==3.1.3
MarkupSafe==2.1.5
pycparser==2.22
PyMySQL==1.1.0
Werkzeug==3.0.2
jupyterlab
notebook