sqltime-complexityprestotrinounnest

Time Cost of UNNEST operation in query?


This is for PrestoSQL

Assuming col1, col2, col3 are of same cardinality, and assuming the Table has N rows

 SELECT c1 from Table, UNNEST(col1) AS t(c1) 


 SELECT c1, c2 from Table, UNNEST(col1, col2) AS t(c1, c2) 


 SELECT c1, c2, c3 from Table, UNNEST(col1, col2, col3) AS t(c1, c2, c3) 

How would the time complexities for these three queries differ? Would the 3rd one be N^3 times more costly than first one, and 2nd one be N^2 times more costly than the first one in terms of time complexity?

How does time complexity be measured in terms of column cardinality, and the table size?


Solution

  • How would the time complexities for these three queries differ? Would the 3rd one be N^3 times more costly than first one, and 2nd one be N^2 times more costly than the first one in terms of time complexity?

    While without looking into the source code it is hard to claim anything, there is no reason for such operation to be power of N (number of rows in the table), unnest with multiple array columns will result in arrays matched on the index, so in terms of the time complexity it does not matter 1,2 or 3 array columns are used, it will be O(N*m) where m is average cardinality of the array per row (or if all rows has array columns with the same cardinality then O(N*m) where m is just a cardinality).

    But for sure the query will be more time consuming since you are returning more data, but not an exponential in terms of number of array columns used.