cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Get the Size of Filtered Rows in Databricks SQL

vidya_kothavale
New Contributor III

I have a query that filters rows from a table based on a timestamp range. The query is as follows:

SELECT COUNT(*) FROM table_name WHERE ts >= '2025-02-04 00:00:00' AND ts < '2025-02-05 00:00:00';

This query returns 10 rows. I need to calculate the total size (in bytes) of these 10 rows.

What is the best approach in Databricks SQL to get the exact size of these filtered rows?
is there a built-in function to get this information?

 

2 REPLIES 2

holly
Databricks Employee
Databricks Employee

You'd have to write the results to a table, then do 

DESCRIBE DETAIL table_name

and look for the column sizeInBytes

MadhuB
Contributor III

@vidya_kothavale try this code block. Keep in mind to handle the null values.

SELECT 
    SUM(OCTET_LENGTH(CAST(column1 AS STRING)) + OCTET_LENGTH(CAST(column2 AS STRING)) + OCTET_LENGTH(CAST(COALESCE(column3, '0') AS STRING))) as bytes,
    SUM(OCTET_LENGTH(CAST(column1 AS STRING)) + OCTET_LENGTH(CAST(column2 AS STRING)) + OCTET_LENGTH(CAST(COALESCE(column3, '0') AS STRING))) /POWER(1024,2) as mb,
    SUM(OCTET_LENGTH(CAST(column1 AS STRING)) + OCTET_LENGTH(CAST(column2 AS STRING)) + OCTET_LENGTH(CAST(COALESCE(column3, '0') AS STRING))) /POWER(1024,3) as gb 
FROM catalog.schema.table where ts >= '2025-02-04 00:00:00' AND ts < '2025-02-05 00:00:00';

Let me know for anything, else mark it as solution.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group