The standard advice for your second PySpark project is to stop trusting inferSchema, declare a schema, clean before you transform, and write Parquet instead of CSV. Thomas Reid walks through all of it in a practical piece on Towards Data Science, and I don’t disagree with any of it. When the question is PySpark for WooCommerce order data, though, the advice that’s missing comes before all of that: whether the data should leave MySQL at all. On most stores the honest answer is no, and bolting Spark onto a store that doesn’t need it just moves the slow part somewhere you can’t see it.
Where WooCommerce order data lives now
For years the real argument for pulling store data out of the database was storage. Orders were posts, every order field was a row in wp_postmeta, and any report beyond a simple count turned into joins that the database handled badly. That pain is what people usually mistake for a Spark problem. Fix the storage first. Since WooCommerce 8.2, released in October 2023, new installs get High-Performance Order Storage by default: order data moves into dedicated tables, _wc_orders, _wc_order_addresses, _wc_order_operational_data and _wc_orders_meta, with typed columns and indexes, and the old post tables stay as a synced backup during the transition. The WooCommerce developer docs cover the table set and the sync behavior in detail.
So there are three realistic shapes for order reporting, and they are not equal. Reporting straight in MySQL is the right default when HPOS is on, because a monthly revenue rollup by status is one query against indexed columns, on a database already sized for the traffic that created the data. A nightly export plus pandas is the next step when the analysis needs to live outside the store, or when someone wants a file to work with. The full order table of a typical store fits in memory, and when it fits, Spark’s JVM startup and shuffles are overhead that buys you nothing. PySpark earns its place only when the data is genuinely large or comes from several places at once: multi-year order history combined with product feeds, ad platform exports, session or log data. That’s where lazy execution and column pruning have real work to do.
My split is simple. With one store and one recurring question, it’s SQL every time. When it’s several stores or several data sources, and a weekly job whose output someone else consumes, I’d build the pipeline the source describes, with Parquet in the middle. Everything in between is export plus pandas, or just better SQL. And if the store still keeps orders in posts and postmeta, that’s the thing to fix first: I’ve written about when a custom table beats post meta and about a data strategy for bloated WordPress databases, and both matter more here than any Spark tutorial.
PySpark for WooCommerce data starts with a declared schema
When you do export, the source’s best habit is the one I’d copy on day one: declare the schema and set mode to FAILFAST. inferSchema samples the file and guesses. One blank row, or one total with a stray currency symbol in it, and your money column silently becomes strings; the failure surfaces three transformations later as nulls and nobody connects it back to the read. FAILFAST moves the error to where the cause is, at load time. The definitions already exist on the WordPress side: the HPOS table is the spec, so writing the schema is transcription rather than guesswork.
One gotcha before you write any filters. In the HPOS tables the status column stores processing, not wc-processing, because the wc- prefix is added when statuses are displayed in the admin. Export the raw column, filter for wc-processing, and you get an empty result with no error, the sort of quiet bug the source’s cleaning functions exist to catch. dropna on the columns that matter, fillna where a missing value has a clear meaning, cast where the export forced everything to strings: that’s the right size for this job.
The export itself is one WP-CLI command from the server, with the table prefix adjusted for the install. (I haven’t run this on multisite, where every site gets its own prefix, so treat it as single-site.)
wp db query 'SELECT id, status, currency, total_amount, tax_amount, date_created_gmt FROM wp_wc_orders' > orders.tsv
Then the read, with the schema stated up front:
from pyspark.sql import types as T
orders_schema = T.StructType([
T.StructField("id", T.IntegerType(), False),
T.StructField("status", T.StringType(), False),
T.StructField("currency", T.StringType(), True),
T.StructField("total_amount", T.DoubleType(), True),
T.StructField("tax_amount", T.DoubleType(), True),
T.StructField("date_created_gmt", T.TimestampType(), True),
])
df_raw = (
spark.read
.option("header", True)
.option("sep", "\t")
.option("nullValue", "NULL")
.option("mode", "FAILFAST")
.schema(orders_schema)
.csv("orders.tsv")
)
The nullValue token has to match what the file contains. The source maps “N/A” because that’s what their sample file contains, but a MySQL batch export writes NULL, so that’s the token to declare here. Get it wrong and every null in the optional columns arrives as the literal string “NULL”, which means your fillna and dropna calls never fire and the cleaning code only looks correct.
If your store’s reporting has outgrown what the database can finish overnight, this is the kind of pipeline I build for clients: a scheduled export, a schema that fails loudly, and Parquet files the next tool can open. If you just want a second opinion on whether you need Spark at all, send me the query that’s slow.
Write the cleaned result back out as Parquet and the next job skips all of the parsing. Parquet is columnar and compressed, the schema travels inside the file, and Spark only reads the columns a job names, which the Spark docs call a self-describing format. The source calls it the easiest performance upgrade available to a beginner. I’d add a client-work reason: parquet files open in pandas, DuckDB and most BI tools, so whoever consumes the report next isn’t locked into your Spark session.
Keep the naming habit too: df_raw, df_clean, df_final, with a row count checked between stages. That is the debugging method, and it answers the questions that come up, like whether rows disappeared during cleaning or whether the join produced more rows than either input. When something runs slowly, the Spark UI at localhost:4040 shows which jobs ran, so you’re not guessing about what Spark did.
Two caveats before you build any of it. First, check that HPOS is on, under WooCommerce, Settings, Advanced, Custom data stores. Stores that predate 8.2 and never switched may have empty or missing order tables, with the data still living in posts and postmeta, which changes the tables your export reads. Second, run Spark somewhere that isn’t the web server. Use a local session like the source’s, or a separate box, because a JVM on the same host as PHP and MySQL is how a reporting problem becomes an outage. And test the boring option first: run the report as one SQL query and time it. If it comes back in seconds, you never had a problem Spark solves.