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.

Easy Python 3.9+ Aug 9, 2026 Database scaling & optimization 14 views 0 copies

Python code

31 lines
Python 3.9+
import 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

stdout
[
  {
    "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

  1. Use list comprehensions for a more concise but less explicit transformation.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.