How to Convert Data with Scaling for Database Optimization in Python
A beginner-friendly helper that normalizes and scales numeric fields in a list of dicts, reducing storage footprint for database efficiency.
Python code
31 linesimport json
from datetime import datetime
def convert_data(data: list[dict], scale_factor: int = 1) -> list[dict]:
"""Convert a list of dicts to a scaled, normalized format for database efficiency."""
converted = []
for row in data:
normalized = {}
for key, value in row.items():
# Convert numeric values by scale factor to reduce storage size
if isinstance(value, (int, float)) and not isinstance(value, bool):
normalized[f"{key}_scaled"] = round(value / scale_factor, 2)
# Convert string dates to ISO format for consistency
elif isinstance(value, str) and "T" in value:
try:
dt = datetime.fromisoformat(value)
normalized[f"{key}_iso"] = dt.strftime("%Y-%m-%d")
except ValueError:
normalized[key] = value
else:
normalized[key] = value
converted.append(normalized)
return converted
if __name__ == "__main__":
raw_data = [
{"id": 1, "price": 100, "date": "2024-01-15T10:30:00"},
{"id": 2, "price": 200, "date": "2024-02-01T08:45:00"},
]
result = convert_data(raw_data, scale_factor=10)
print(json.dumps(result, indent=2))
Output
[
{
"id": 1,
"price_scaled": 10.0,
"date_iso": "2024-01-15"
},
{
"id": 2,
"price_scaled": 20.0,
"date_iso": "2024-02-01"
}
]
How it works
The function iterates over each dictionary and normalizes values based on type. Numeric values (excluding booleans) are divided by the scale factor and rounded to two decimals, producing keys with a _scaled suffix. ISO datetimes are parsed and reformatted to date-only strings, stored under a _iso key. The result is a new list of dicts ready for bulk insertion, sharding, or columnar storage where smaller numbers and consistent date formats improve compression and query speed.
Common mistakes
- Accidentally scaling booleans because `bool` is a subclass of `int` in Python.
- Not handling `ValueError` when parsing non-ISO strings that contain a 'T'.
- Modifying the original list in place instead of returning a new one.
- Using integer division that drops decimal precision when scale_factor is not a float.
Variations
- Use list comprehensions for a more concise but less explicit transformation.
- Leverage pandas `apply` with a custom function for DataFrames instead of raw dicts.
Real-world use cases
- Preprocessing raw JSON events before bulk loading into a time-series database.
- Normalizing legacy data exports to a stable format for sharded database migrations.
- Compressing numeric metrics before inserting into a columnar store like ClickHouse.
Sponsored
More from Database scaling & optimization
- Approximate Count with HyperLogLog in Python medium
- B-Tree Insert and In-Order Traversal in Python hard
- Broadcast a Small Reference Table in Python easy
- Build a Full Text Search Index in Python medium
- Build a Partial Index Mock in Python for Database Filtering easy
- Composite index leftmost prefix in Python medium
Keep learning
Related tutorials and quizzes for this topic.