# Krempl PostgreSQL Migration Dieses Verzeichnis enthält alle Dateien für die Migration der Krempl-Daten von MySQL zu PostgreSQL. ## 🎯 Ziel **Problem:** 61 Millionen Zeilen in MySQL → langsame Queries, 504 Timeouts **Lösung:** Aggregation zu 68k Zeilen mit PostgreSQL Arrays → 50-200ms Queries --- ## 📁 Dateien ``` core/postgres/ ├── schema.sql # PostgreSQL Schema (Tabellen, Indizes) ├── import.py # Python Import-Script ├── config.ini.example # Konfigurations-Vorlage ├── config.ini # Deine echte Config (NICHT ins Git!) ├── .gitignore # Verhindert, dass config.ini committed wird └── README.md # Diese Datei ``` --- ## 🚀 Setup auf dem Server ### 1. PostgreSQL installieren ```bash # Auf Debian/Ubuntu Server sudo apt update sudo apt install postgresql postgresql-contrib php-pgsql python3-psycopg2 # PostgreSQL starten sudo systemctl start postgresql sudo systemctl enable postgresql ``` ### 2. Datenbank erstellen **PLESK:** PostgreSQL User ist `root`, nicht `postgres`! ```bash # Plesk PostgreSQL Admin-Passwort holen: # Panel → Tools & Settings → Database Servers → PostgreSQL PLESK_PW='DEIN_PLESK_PG_PASSWORD' # Datenbank erstellen PGPASSWORD=$PLESK_PW psql -U root -h localhost -d postgres -c " CREATE DATABASE krempl_data WITH ENCODING 'UTF8'; " # User erstellen (WICHTIG: Passwort ohne Sonderzeichen!) PGPASSWORD=$PLESK_PW psql -U root -h localhost -d postgres -c " CREATE USER krempl_user WITH PASSWORD 'KremplSecure2025'; " # Rechte vergeben PGPASSWORD=$PLESK_PW psql -U root -h localhost -d postgres -c " GRANT ALL PRIVILEGES ON DATABASE krempl_data TO krempl_user; " PGPASSWORD=$PLESK_PW psql -U root -h localhost -d krempl_data -c " GRANT ALL ON SCHEMA public TO krempl_user; " # Schema laden cd /var/www/vhosts/newmail.intelectra.de/httpdocs/core/postgres PGPASSWORD=$PLESK_PW psql -U root -h localhost -d krempl_data -f schema.sql # Tabellen-Rechte setzen PGPASSWORD=$PLESK_PW psql -U root -h localhost -d krempl_data -c " GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO krempl_user; GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO krempl_user; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO krempl_user; " ``` ### 3. Config erstellen ```bash cd /pfad/zu/Intelectra-WebShop/core/postgres/ # Kopiere Beispiel-Config cp config.ini.example config.ini # Bearbeite config.ini nano config.ini # Trage ein: # - PostgreSQL Passwort # - CSV-Dateipfade ``` ### 4. Import ausführen ```bash # Python-Abhängigkeit installieren pip3 install psycopg2-binary # Prüfen: python3 -c "import psycopg2; print('psycopg2 OK')" # Import starten (dauert 10-20 Min) cd /var/www/vhosts/newmail.intelectra.de/httpdocs/core/postgres python3 import.py # Mit Live-Output: python3 import.py 2>&1 | tee /tmp/krempl_import.log # Fortschritt anschauen: tail -f /tmp/krempl_import.log # Mit anderem Config-File: python3 import.py --config /pfad/zu/config.ini ``` **Erwarteter Output:** ``` ====================================================================== KREMPL POSTGRESQL IMPORT Aggregiert 61 Millionen Zeilen → 68k Zeilen mit Arrays ====================================================================== ✅ Verbunden mit PostgreSQL: krempl_data ====================================================================== [1/4] IMPORTIERE GERÄTE ====================================================================== 📁 CSV-Datei: geraete_Export.csv (227.0 MB) 🗑️ Tabelle geleert → 100,000 Geräte importiert... → 200,000 Geräte importiert... ... ✅ 1,446,180 Geräte importiert (Fehler: 0) ====================================================================== [2/4] IMPORTIERE ERSATZTEILE ====================================================================== ... ====================================================================== [3/4] IMPORTIERE MAPPING (AGGREGIERT) ====================================================================== 📁 CSV-Datei: artikelgeraet_Export.csv (1.32 GB) 💾 Modus: In-Memory Aggregation (schnell, RAM-intensiv) ⏳ Lese und aggregiere 61 Millionen Zeilen... → 1,000,000 Zeilen gelesen... → 10,000,000 Zeilen gelesen... → 61,000,000 Zeilen gelesen... ✓ 61,234,567 Zeilen gelesen ✓ 68,667 unique Ersatzteile gefunden 💾 Schreibe aggregierte Daten nach PostgreSQL... → 500/68,667 Ersatzteile geschrieben... → 10,000/68,667 Ersatzteile geschrieben... ✅ 68,667 Ersatzteile mit Arrays gespeichert 📊 Max Geräte pro Teil: 129,819 📊 Ø Geräte pro Teil: 888 ... ====================================================================== ✅ IMPORT ERFOLGREICH in 0:12:34 ====================================================================== ``` --- ## 🧪 Testen ### PostgreSQL Queries testen ```bash # Als postgres User sudo -u postgres psql -d krempl_data # Test 1: Geräte zu Ersatzteil SELECT g.nr, g.marke, g.typ FROM ersatzteil_mapping em JOIN geraete g ON g.id = ANY(em.geraet_ids) WHERE em.ersatzteil_id = 7764466071 LIMIT 10; # Test 2: Ersatzteile zu Gerät SELECT ersatzteil_id FROM ersatzteil_mapping WHERE geraet_ids @> ARRAY[123456]::bigint[] LIMIT 10; # Test 3: Full-Text Suche SELECT id, nr, marke, typ FROM geraete WHERE to_tsvector('german', nr || ' ' || typ) @@ plainto_tsquery('german', 'AEG Kühlschrank') LIMIT 10; ``` ### PHP Connection testen ```bash # PHP Extension prüfen php -m | grep pdo_pgsql # Sollte "pdo_pgsql" ausgeben # Test-Script php -r "new PDO('pgsql:host=localhost;dbname=krempl_data', 'krempl_user', 'PASSWORT');" # Sollte keine Fehler ausgeben ``` --- ## 🔧 Troubleshooting ### ❌ "password authentication failed for user krempl_user" Sonderzeichen im Passwort können Probleme machen! ```bash # Passwort ohne Sonderzeichen setzen (nur A-Z, a-z, 0-9) PGPASSWORD='PLESK_PG_PASSWORD' psql -U root -h localhost -d postgres -c " ALTER USER krempl_user WITH PASSWORD 'KremplSecure2025'; " # Testen: PGPASSWORD='KremplSecure2025' psql -U krempl_user -h localhost -d krempl_data -c "SELECT 1;" ``` ### ❌ "permission denied for table ..." ```bash PGPASSWORD='PLESK_PG_PASSWORD' psql -U root -h localhost -d krempl_data -c " GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO krempl_user; GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO krempl_user; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO krempl_user; " ``` ### ❌ "ModuleNotFoundError: No module named 'psycopg2'" ```bash pip3 install psycopg2-binary python3 -c "import psycopg2; print('OK')" ``` ### Import läuft Out of Memory → In `config.ini` ändern: ```ini mapping_in_memory = false ``` (Langsamer, aber RAM-freundlich) ### CSV-Datei nicht gefunden → Prüfe Pfade in `config.ini`: ```bash ls -lh /var/www/vhosts/intelectra.de/httpdocs/upload/*.csv ``` ### Log-Datei kann nicht erstellt werden → In `config.ini` Log-Pfad leer lassen: ```ini [logging] log_file = ``` ### PHP kann nicht zu PostgreSQL connecten **Plesk:** PHP-PostgreSQL Extension ist meist schon da! ```bash php -m | grep pdo_pgsql # Sollte "pdo_pgsql" ausgeben ``` --- ## 📊 Erwartete Performance | Metrik | MySQL (alt) | PostgreSQL (neu) | |--------|-------------|------------------| | DB-Größe | 1.3 GB | ~100 MB | | Zeilen | 61 Mio | 68k | | Artikelseite | 5-15 Sek | 50-200 ms | | Gerätesuche | 10-30 Sek | 100-500 ms | --- ## 🔄 Re-Import (Update) Wenn neue Krempl-Daten kommen: ```bash # Einfach nochmal ausführen python3 import.py # Das Script ist idempotent (mehrfach ausführbar) # Bestehende Daten werden überschrieben ``` --- ## 📚 Nächste Schritte Nach erfolgreichem Import: 1. ✅ PHP Connection-Klasse implementieren (`core/postgres/connection.php`) 2. ✅ Artikelseite umbauen (nutzt PostgreSQL statt MySQL) 3. ✅ Gerätesuche umbauen 4. ✅ Auf Testshop testen 5. ✅ Performance messen 6. 🚀 Live deployen 7. 🗑️ MySQL Krempl-Tabellen löschen (1.3 GB frei!) --- **Viel Erfolg! 🚀**