Best Practices für die Auswahl von Primärschlüsselkombinationen aus mehreren Spalten

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Best Practices für die Auswahl von Primärschlüsselkombinationen aus mehreren Spalten

by Anonymous » 04 Mar 2025, 08:38

Ich arbeite in Azure Databricks mit einem großen PYSPARK -Datenframe mit 170 Spalten. Ich muss die bestmögliche Kombination von 2-3 Spalten als Primärschlüssel identifizieren und sicherstellen, dass die ausgewählte Kombination jede Zeile eindeutig identifizieren sollte. Einschränkungen. Diese Methode ist jedoch bei großen Datensätzen aufgrund mehrerer .distinct (). Count () operationen. < /P>
Hier ist mein Code: < /p>
from itertools import combinations

selected_columns = [
"Message_MessageBody_Header_",
"Message_FileSequenceNo",
"Message_MessageBody_ArticleInfo_BNo",
"EnvDate",
"EnvTime"
] # Replace with actual column names

total_count = df.count()
print(f"Total records in DataFrame: {total_count}")

missing_columns = [col for col in selected_columns if col not in df.columns]
if missing_columns:
print(f"Error: The following columns are missing in the DataFrame: {missing_columns}")
else:
print(f"Selected columns exist in the DataFrame: {selected_columns}")

found_primary_key = False
for r in range(2, len(selected_columns) + 1):
print(f"\nChecking {r}-column combinations...")

for combo in combinations(selected_columns, r):
print(f"\n🔍 Testing combination: {combo}")
unique_count = df.select(*combo).distinct().count()
print(f"Unique count for {combo}: {unique_count}")

df.select(*combo).distinct().show(5, truncate=False)

if unique_count == total_count:
print(f"\n✅ Found Primary Key: {combo}")
found_primary_key = True
break # Stop once a valid key is found

if found_primary_key:
break

if not found_primary_key:
print("\n❌ No unique primary key combination found in the selected columns.")
< /code>
Problem:
Dieser Ansatz ist rechnerisch teuer, da wiederholt .Distinct (). count () Operationen auf großen Daten. schneller?>

Top