Why 90% of the catalog "changed": finding real differences between two data runs
A client re-imports a supplier's vehicle fitment data into their online store: for each part, the list of vehicles it fits. A full import covers tens of thousands of parts and takes a long time. The obvious improvement is to import only the parts whose fitment is new or different since last time.
Before building that, I tested the idea against two real deliveries. The comparison worked. The answer it gave was that nearly everything had changed. Both of those turned out to be useful.
What "changed" has to mean
Each part has a table of fitment rows with five columns: year range, make, model, position and notes. Comparing two deliveries byte for byte is hopeless, because plenty differs that does not matter:
- Rows arrive in a different order.
- Whitespace and HTML escaping vary.
- Row IDs are random and regenerated on every run.
- The delivery is split across numbered files, and the split is different each time.
- One run quoted a field that the other left unquoted.
So the definition I settled on: a part's fitment is the set of its rows, after cleaning each cell. It changed if that set is different.
The method
- Gather every file in a run into one lookup keyed by part number. Never compare file to file.
- For each part, pull out the data rows and skip headers and boilerplate.
- Clean each cell: decode HTML entities, collapse runs of whitespace, trim.
- Sort the rows and compare.
In Python, the core is a few lines. Hashing the sorted rows gives one short value per part that can be stored and compared later:
import hashlib, html, re
def fitment_hash(rows):
cleaned = sorted(
tuple(re.sub(r"\s+", " ", html.unescape(cell)).strip() for cell in row)
for row in rows
)
return hashlib.sha256(repr(cleaned).encode()).hexdigest()
The result
The two deliveries were about 22 months apart.
| Parts | Count |
|---|---|
| In both runs | 46,352 |
| Only in the new run | 11,248 |
| Only in the old run | 16,054 |
| In both, with different fitment | 41,813 (90.2%) |
A filter that passes 90% of its input is not much of a filter. My first suspicion was my own code: surely formatting noise was leaking through.
Checking the checker
The test I used: take vehicles that appear for the same part in both runs, and compare their remaining columns. If the cleaning step were leaving noise behind, those would disagree often. Across thousands of shared rows they disagreed zero times.
So the differences were real. The new run had about 36% more fitment rows overall, for two reasons. Two more model years had been added. And the older run had under-captured parts with very long fitment lists: one part went from 346 rows to 8,839, and the larger number matched the source.
The baseline matters more than the diff
The comparison was correct and still not worth shipping as a one-off, because the baseline was too old and partly incomplete. Comparing against it would have meant re-importing almost the whole catalog anyway.
Change detection pays off between two recent, complete runs. That shifts the work from "write a comparison script" to "make every run leave a baseline behind":
- Store the hash next to each part, along with the run that last changed it.
- Stop wiping the previous data at the start of a run. The old rows and hash have to survive long enough to compare against.
- When a part is fetched, compute the new hash. Same as stored: skip it. Different: replace the rows, update the hash, stamp the run.
- Build the import file from the parts stamped in this run.
Parts that have disappeared from the supplier keep their old rows and are never stamped, so they stay out of the import without special handling.
Takeaways
- Decide what "changed" means in business terms before comparing anything.
- Normalize, sort, then compare. Most false differences are ordering and formatting.
- When the result looks wrong, test the comparison itself before distrusting or trusting it.
- Measure against real data before building. A short experiment showed which version of this feature was worth paying for.
Re-importing an entire catalog every time something small changes? Tell me about it.