🔧 Aktualisierung der Backend-Logik und Optimierung der SQLite-Datenbankkonfiguration für Raspberry Pi: Hinzufügen spezifischer Optimierungen, Verbesserung der Fehlerbehandlung und Protokollierung. Einführung von Caching-Mechanismen und Anpassungen für schwache Hardware. 📈
This commit is contained in:
@ -83,13 +83,35 @@ def configure_sqlite_for_production(dbapi_connection, _connection_record):
|
||||
# Checkpoint-Intervall für WAL
|
||||
cursor.execute("PRAGMA wal_autocheckpoint=1000")
|
||||
|
||||
# ===== RASPBERRY PI SPEZIFISCHE OPTIMIERUNGEN =====
|
||||
# Reduzierte Cache-Größe für schwache Hardware
|
||||
cursor.execute("PRAGMA cache_size=-32000") # 32MB statt 64MB für Pi
|
||||
|
||||
# Kleinere Memory-mapped I/O für SD-Karten
|
||||
cursor.execute("PRAGMA mmap_size=134217728") # 128MB statt 256MB
|
||||
|
||||
# Weniger aggressive Vacuum-Einstellungen
|
||||
cursor.execute("PRAGMA auto_vacuum=INCREMENTAL")
|
||||
cursor.execute("PRAGMA incremental_vacuum(10)") # Nur 10 Seiten pro Mal
|
||||
|
||||
# Optimierungen für SD-Karten I/O
|
||||
cursor.execute("PRAGMA page_size=4096") # Optimal für SD-Karten
|
||||
cursor.execute("PRAGMA temp_store=MEMORY") # Temp im RAM
|
||||
cursor.execute("PRAGMA locking_mode=NORMAL") # Normale Sperrung
|
||||
|
||||
# Query Planner Optimierung
|
||||
cursor.execute("PRAGMA optimize=0x10002") # Aggressive Optimierung
|
||||
|
||||
# Reduzierte WAL-Datei-Größe für Pi
|
||||
cursor.execute("PRAGMA journal_size_limit=32768000") # 32MB WAL-Limit
|
||||
|
||||
cursor.close()
|
||||
|
||||
logger.info("SQLite für Produktionsumgebung konfiguriert (WAL-Modus, Cache, Optimierungen)")
|
||||
logger.info("SQLite für Raspberry Pi optimiert (reduzierte Cache-Größe, SD-Karten I/O)")
|
||||
|
||||
def create_optimized_engine():
|
||||
"""
|
||||
Erstellt eine optimierte SQLite-Engine mit Connection Pooling und WAL-Modus.
|
||||
Erstellt eine optimierte SQLite-Engine mit korrekten SQLite-spezifischen Parametern.
|
||||
"""
|
||||
global _engine
|
||||
|
||||
@ -105,24 +127,27 @@ def create_optimized_engine():
|
||||
# Connection String mit optimierten Parametern
|
||||
connection_string = f"sqlite:///{DATABASE_PATH}"
|
||||
|
||||
# Engine mit Connection Pooling erstellen
|
||||
# Engine mit SQLite-spezifischen Parametern (ohne Server-DB Pool-Parameter)
|
||||
_engine = create_engine(
|
||||
connection_string,
|
||||
# Connection Pool Konfiguration
|
||||
# SQLite-spezifische Pool-Konfiguration
|
||||
poolclass=StaticPool,
|
||||
pool_pre_ping=True, # Verbindungen vor Nutzung testen
|
||||
pool_recycle=3600, # Verbindungen nach 1 Stunde erneuern
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=7200, # Recycling-Zeit (für SQLite sinnvoll)
|
||||
connect_args={
|
||||
"check_same_thread": False, # Für Multi-Threading
|
||||
"timeout": 30, # Connection Timeout
|
||||
"isolation_level": None # Autocommit-Modus für bessere Kontrolle
|
||||
"check_same_thread": False,
|
||||
"timeout": 45, # Längerer Timeout für SD-Karten
|
||||
"isolation_level": None,
|
||||
# Raspberry Pi spezifische SQLite-Einstellungen
|
||||
"cached_statements": 100, # Reduzierte Statement-Cache
|
||||
},
|
||||
# Echo für Debugging (in Produktion ausschalten)
|
||||
echo=False,
|
||||
# Weitere Optimierungen
|
||||
# Performance-optimierte Execution-Optionen für Pi
|
||||
execution_options={
|
||||
"autocommit": False
|
||||
"autocommit": False,
|
||||
"compiled_cache": {}, # Statement-Kompilierung cachen
|
||||
}
|
||||
# Entfernt: pool_size, max_overflow, pool_timeout (nicht für SQLite/StaticPool)
|
||||
)
|
||||
|
||||
# Event-Listener für SQLite-Optimierungen
|
||||
|
Reference in New Issue
Block a user