# V-Order and Sorting

I have written about V-order before. A quick summary from [official MS Docs](https://learn.microsoft.com/en-us/fabric/data-engineering/delta-optimization-and-v-order?tabs=sparksql):

*V-Order is a write time optimization for the parquet file format that enables fast reads under Microsoft Fabric compute engines like Power BI, SQL, Spark, and others. It improves data access times and provides cost efficiency and performance by applying sorting, row group distribution, dictionary encoding, and compression on parquet files.*

To check if a table is V-order you can read [my blog](https://fabric.guru/checking-if-delta-table-in-fabric-is-v-order-optimized).

By default, all engines in Microsoft Fabric create V-order optimized tables. If they currently do not, they will be by GA. However, currently, there is one specific instance when tables will not be V-order'd - if you sort the dataframe in spark.

I have an existing table called `orders` . I can confirm if it's V-order'd by using my script from the above blog:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687289874208/0da77704-123e-401c-bb66-41a90072a042.png align="center")

## Sort & Save Table

If I sort the spark dataframe and save the table, the table will not be V-order'd. User will not get any error or warning. In the example below, I sorted the dataframe by `year` column before saving and this resulted in a table without V-order.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687290170387/058e2ff0-6221-44ae-b386-7a1feadcf64f.png align="center")

## Solution

To force V-order for a sorted dataframe, you have to use `option("parquet.vorder.enabled", "force_true")` while saving the table, as shown below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687290359513/0fbff161-b8ed-4a9c-a6ea-15cf236e311a.png align="center")

You can also set spark configuration setting `spark.conf.set('spark.sql.parquet.vorder.ignorePlan',  'true')` to apply V-order in the Spark session.

> As the product is in public preview, note that this behavior may change in the future. Always check if the table is V-order enabled. As far as I know, this is only applicable to spark. Refer to the official documentation [here](https://learn.microsoft.com/en-us/fabric/data-engineering/delta-optimization-and-v-order?tabs=sparksql) in the future for details.

Thank you to Tamas Polner at Microsoft for this information.
