Skip to main content
Question

Evaluate a subquery to use it as a Filter

  • November 16, 2018
  • 4 replies
  • 16 views

chaima
Forum|alt.badge.img+2
  • Participating Frequently

Hi,

Is there any way to evaluate a subquery used in a WHERE IN clause to use it as a filter instead of doing a join.
I've seen a few posts on this here but no solution was found.

Any ideas?

Many thanks

Chaima

4 replies

marcothesane
Forum|alt.badge.img+1
  • Participating Frequently
  • November 16, 2018

If you mean:

SELECT
  MONTH(sales_dt) AS mth
, SUM(amount) AS sales
FROM f_sales
WHERE cust_id IN (
  SELECT cust_id
  FROM d_cust
  WHERE age BETWEEN 55 AND 60
)
GROUP BY 1
;

that's how it's done. But what is the question, really?


mflower
Forum|alt.badge.img
  • Participating Frequently
  • November 20, 2018
Re: SELECT MONTH(sales_dt) AS mth , SUM(amount) AS sales FROM f_sales WHERE cust_id IN ( SELECT cust_id FROM d_cust WHERE age BETWEEN 55 AND 60 ) GROUP BY 1 ;
Does this flatten into a join anyway?

@Chaima - do you want to force Vertica to perform a subquery instead of a flattened join?

Michael Flower

chaima
Forum|alt.badge.img+2
  • Author
  • Participating Frequently
  • November 20, 2018

Hi Marco, Mike,

I was actually wondering if there is a way to tell Vertica to evaluate the subquery beforehand and use a filter operator instead of a join.
so that if
SELECT cust_id
FROM d_cust
WHERE age BETWEEN 55 AND 60
returns 1,2,3

The optimizer will evaluate the subquery and will run this query:
SELECT
MONTH(sales_dt) AS mth
, SUM(amount) AS sales
FROM f_sales
WHERE cust_id IN (1,2,3)
GROUP BY 1
;
But knowing if there is a query hint to preform the subquery first would help too.
Many Thanks
Chaima


Vertica_Curtis
Forum|alt.badge.img+1
  • Participating Frequently
  • November 20, 2018

What is your goal here? Every query in Vertica is treated separately. So Vertica is running that query separately. If you're trying to change the explain plan to get a Merge join, or something like that, the IN() clause is going to screw that up, and a subquery is going to behave like an IN() clause.