-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_db.py
More file actions
62 lines (49 loc) · 1.86 KB
/
optimize_db.py
File metadata and controls
62 lines (49 loc) · 1.86 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
import sqlite3
import argparse
import time
INDEXES = [
("idx_timestamp", "timestamp"),
("idx_user_id", "user_id"),
("idx_search_query", "search_query")
]
def add_indexes(db_file):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
for idx_name, col in INDEXES:
print(f"Adding index on '{col}'...")
try:
cursor.execute(f"CREATE INDEX IF NOT EXISTS {idx_name} ON search_logs({col})")
except Exception as e:
print(f"Failed to add index {idx_name}: {e}")
conn.commit()
conn.close()
print("Index optimization complete.")
def benchmark_query(db_file):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
print("\n⏱Benchmarking common queries:")
start = time.time()
cursor.execute("SELECT COUNT(*) FROM search_logs")
print("Total entries:", cursor.fetchone()[0])
print("Query 1 time:", round(time.time() - start, 4), "s")
start = time.time()
cursor.execute("SELECT COUNT(*) FROM search_logs WHERE user_id = 'test_user_1'")
print("Query 2 time:", round(time.time() - start, 4), "s")
start = time.time()
cursor.execute("SELECT COUNT(*) FROM search_logs WHERE search_query LIKE '%flask%'")
print("Query 3 time:", round(time.time() - start, 4), "s")
conn.close()
def main():
parser = argparse.ArgumentParser(description="Add indexes to logs.db for query performance optimization")
parser.add_argument("--db", default="logs.db", help="Path to the SQLite database")
parser.add_argument("--benchmark", action="store_true", help="Run benchmark queries before and after")
args = parser.parse_args()
if args.benchmark:
print("\nBEFORE INDEXES:")
benchmark_query(args.db)
add_indexes(args.db)
if args.benchmark:
print("\nAFTER INDEXES:")
benchmark_query(args.db)
if __name__ == "__main__":
main()