-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_database.py
More file actions
110 lines (90 loc) · 3.78 KB
/
Copy pathoptimize_database.py
File metadata and controls
110 lines (90 loc) · 3.78 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""
Database optimization script for DXFMeta application.
This script adds necessary indexes to improve database performance.
"""
import os
import sys
import sqlite3
import argparse
def optimize_database(db_path):
"""Add performance optimizations to the database."""
if not os.path.exists(db_path):
print(f"Error: Database file not found at {db_path}")
return False
print(f"Optimizing database at {db_path}...")
try:
# Connect to the database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if modules table exists
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='modules'")
if not cursor.fetchone():
print("Error: modules table not found in database")
conn.close()
return False
# Get table structure to check column names
cursor.execute("PRAGMA table_info(modules)")
columns = [row[1] for row in cursor.fetchall()]
print(f"Available columns: {columns}")
# Add indexes on commonly queried fields
indexes_to_create = [
("idx_modules_module_id", "CREATE INDEX IF NOT EXISTS idx_modules_module_id ON modules(module_id)"),
("idx_modules_module_type", "CREATE INDEX IF NOT EXISTS idx_modules_module_type ON modules(module_type)"),
("idx_modules_relative_path", "CREATE INDEX IF NOT EXISTS idx_modules_relative_path ON modules(relative_path)")
]
# Create each index
for idx_name, idx_sql in indexes_to_create:
print(f"Creating index {idx_name}...")
cursor.execute(idx_sql)
# Enable WAL mode for better concurrent access
print("Enabling WAL mode...")
wal_mode = cursor.execute("PRAGMA journal_mode=WAL").fetchone()[0]
print(f"Journal mode set to: {wal_mode}")
# Increase cache size for better performance
print("Increasing cache size...")
cursor.execute("PRAGMA cache_size=-10000") # 10MB cache
# Set synchronous mode for better performance
print("Setting synchronous mode...")
cursor.execute("PRAGMA synchronous=NORMAL")
sync_mode = cursor.execute("PRAGMA synchronous").fetchone()[0]
print(f"Synchronous mode set to: {sync_mode}")
# Optimize the database
print("Running ANALYZE to update statistics...")
cursor.execute("ANALYZE")
# Vacuum the database to reclaim space and optimize structure
print("Running VACUUM to optimize database structure...")
cursor.execute("VACUUM")
# Commit changes
conn.commit()
conn.close()
print("Database optimization completed successfully")
return True
except sqlite3.Error as e:
print(f"SQLite error: {str(e)}")
return False
except Exception as e:
print(f"Error optimizing database: {str(e)}")
return False
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(description='Optimize DXFMeta database performance')
parser.add_argument('--db-path', help='Path to the database file')
args = parser.parse_args()
# Determine database path
db_path = args.db_path
if not db_path:
# If not specified, look in the current directory
if os.path.exists('dxf_metadata.db'):
db_path = 'dxf_metadata.db'
else:
print("Error: Database path not specified and not found in current directory")
parser.print_help()
return 1
# Run the optimization
if optimize_database(db_path):
return 0
else:
return 1
if __name__ == "__main__":
sys.exit(main())