-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_sqlite.py
More file actions
54 lines (42 loc) · 1.6 KB
/
Copy pathsetup_sqlite.py
File metadata and controls
54 lines (42 loc) · 1.6 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
import pandas as pd
import sqlite3
import glob
import time
import os
# Config
DATA_DIR = "data"
SQLITE_DB = f"{DATA_DIR}/taxi.db"
PARQUET_GLOB = f"{DATA_DIR}/yellow_tripdata_2024-*.parquet"
def load_sqlite():
print("🛠️ Setting up SQLite for 'Fair Comparison'...")
# Check if DB exists and delete to ensure clean timing
if os.path.exists(SQLITE_DB):
os.remove(SQLITE_DB)
con = sqlite3.connect(SQLITE_DB)
files = sorted(glob.glob(PARQUET_GLOB))
total_start = time.time()
total_rows = 0
for f in files:
print(f" 📖 Reading {f}...")
# Read parquet into Pandas first (SQLite can't read parquet natively)
df = pd.read_parquet(f)
total_rows += len(df)
print(f" 💾 Writing {len(df):,} rows to SQLite...")
# Write to SQLite
df.to_sql("trips", con, if_exists='append', index=False)
# Free memory immediately
del df
# Indexing (Crucial for fair SQL comparison)
print(" 📇 Creating Indices (Fairness)...")
con.execute("CREATE INDEX idx_pu ON trips(PULocationID)")
con.execute("CREATE INDEX idx_do ON trips(DOLocationID)")
con.execute("CREATE INDEX idx_dist_fare ON trips(trip_distance, fare_amount)")
total_time = time.time() - total_start
print(f"\n✅ SQLite Setup Complete.")
print(f" Total Rows: {total_rows:,}")
print(f" Total Setup Time: {total_time:.2f}s")
# Save this number! You need it for your report.
with open("sqlite_timing.txt", "w") as f:
f.write(str(total_time))
if __name__ == "__main__":
load_sqlite()