Which of the two queries is theoretically better and why?
I would like to understand why query 2 takes longer although there is no relevant information in the Execution Plan.
I'm querying a Presto database. I am not the owner of the table. The table does not have any keys or indexes. The table has 7,5 million rows.
The query calculates the percentage distribution of ids per Loc for each country.
Query 1:
WITH a AS (
SELECT title
, country
, loc
, count(DISTINCT id) AS num_id
FROM table
GROUP BY 1,2,3)
SELECT title
, country
, loc
, num_id
, num_id * 100.0000/ (sum(num_id) over(PARTITION BY title, country)) AS percentage
FROM a
GROUP BY 1,2,3,4
Query 2:
SELECT title
, country
, loc
, count(DISTINCT id) AS num_id
, count(DISTINCT id) * 100.0000/ (sum(count(DISTINCT id)) over(PARTITION BY title, country)) AS percentage
FROM table
GROUP BY 1,2,3
Table definition
title varchar
dt date
div_code varchar
id varchar
id_type varchar
country varchar
loc varchar
vo varchar
ref_title varchar
mode varchar
sessions bigint
ttl_type bigint