1

I have a big table in GBQ which is partitioned by date. and I want to use partition pruning to optimize my query. the problem is that filter condition is a value that is read from another table and I can't hardcode the value. I wonder if there is any way to use partition pruning in this case and also I can't use declare statement because scheduling engine that I am working with doesn't allow that. here is my code:

WITH CTE AS
(
  SELECT tmp.partitionStartDate
  FROM tmp_table tmp
)
SELECT *      
FROM table1  t
WHERE sbcc.partitionDate = (select partitionStartDate from cte)
sia
  • 537
  • 1
  • 6
  • 22

2 Answers2

2

Script below will work:

DECLARE partitionDate DEFAULT (
  SELECT tmp.partitionStartDate
  FROM tmp_table tmp
);

SELECT *      
FROM table1  t
WHERE sbcc.partitionDate = partitionDate;

Or

EXECUTE IMMEDIATE
"""
SELECT *      
FROM table1  t
WHERE sbcc.partitionDate = @partitionDate;
""" USING (
  SELECT tmp.partitionStartDate
  FROM tmp_table tmp
) AS partitionDate;
Yun Zhang
  • 5,185
  • 2
  • 10
  • 29
2

Try this:

execute immediate "SELECT * FROM table1 WHERE partitionDate = ?" using (SELECT partitionStartDate FROM tmp_table);
Sergey Geron
  • 9,098
  • 2
  • 22
  • 29