vertica中的directed query

更多内容可参考:directed query

定向查询(directed query)

定向查询封装了优化器可用于创建查询计划的信息。定向查询可以实现以下目标:

  • 使用vertica升级前的查询计划。
  • 创建可提高优化程序性能的查询计划。
    有时,您可能希望影响优化器以在执行给定查询时做出更好的选择。例如,您可以选择其他投影,或强制使用不同的加入顺序。在这种情况下,您可以使用直接查询来创建查询计划,该查询计划优先于优化器可能另外创建的任何计划。
  • 将输入查询重定向到使用不同语义的查询,例如,将联接查询映射到SELECT查询flatten table的语句。

Vertica提供了两种创建定向查询的方法:

  • 优化器可以从给定的输入查询生成带注释的查询,并将二者配对为有向查询。对应CREATE DIRECTED QUERY OPTIMIZER语句。
  • 您可以编写自己的带注释的查询,并将其与输入查询配对。对应CREATE DIRECTED QUERY CUSTOM语句。
优化程序生成的定向查询

CREATE DIRECTED QUERY OPTIMIZER 将输入查询传递给优化器,该优化器根据自己的查询计划生成带注释的查询。然后,它将输入查询和带注释的查询配对,并将它们保存为定向查询。

示例:

=> CREATE DIRECTED QUERY OPTIMIZER 'findBostonCashiers_OPT' 
   SELECT employee_first_name, employee_last_name FROM public.employee_dimension 
   	WHERE employee_city='Boston' and job_title='Cashier';
CREATE DIRECTED QUERY

=> ACTIVATE DIRECTED QUERY findBostonCashiers_OPT;
ACTIVATE DIRECTED QUERY
自定义定向查询

CREATE DIRECTED QUERY CUSTOM 指定带注释的查询,并将其与以前通过Save query保存的查询配对。该SAVE QUERY声明必须出现在CREATE DIRECTED QUERY CUSTOM之前。SAVE QUERY临时保存输入查询以用于创建定向查询。您必须在同一用户会话中发布这两个语句。

示例:

=> SAVE QUERY SELECT employee_first_name, employee_last_name FROM employee_dimension 
   WHERE employee_city='Boston' AND job_title='Cashier';
SAVE QUERY

=> CREATE DIRECTED QUERY CUSTOM 'findBostonCashiers_CUSTOM' 
   SELECT employee_first_name, employee_last_name 
   FROM employee_dimension /*+Projs('public.emp_dimension_unseg')*/ 
   WHERE employee_city='Boston' AND job_title='Cashier';
CREATE DIRECTED QUERY	

=> ACTIVATE DIRECTED QUERY findBostonCashiers_CUSTOM;
ACTIVATE DIRECTED QUERY

激活此定向查询后,优化器将使用它为输入查询的所有后续调用生成查询计划。以下explain输出验证了优化器对此定向查询的使用及其指定的projection:

=> EXPLAIN SELECT employee_first_name, employee_last_name FROM employee_dimension 
   WHERE employee_city='Boston' AND job_title='Cashier';

QUERY PLAN
------------------------------
QUERY PLAN DESCRIPTION:
------------------------------ 
EXPLAIN SELECT employee_first_name, employee_last_name FROM employee_dimension where employee_city='Boston' AND job_title='Cashier';

 The following active directed query(query name: findBostonCashiers_CUSTOM) is being executed:
 SELECT employee_dimension.employee_first_name, employee_dimension.employee_last_name FROM public.employee_dimension/*+Projs('public.Emp_Dimension_Unseg')*/ WHERE ((employee_dimension.employee_city = 'Boston'::varchar(6)) AND (employee_dimension.job_title = 'Cashier'::varchar(7)))

 Access Path:
 +-STORAGE ACCESS for employee_dimension [Cost: 158, Rows: 10K (NO STATISTICS)] (PATH ID: 1)
 |  Projection: public.emp_dimension_unseg
 |  Materialize: employee_dimension.employee_first_name, employee_dimension.employee_last_name
 |  Filter: (employee_dimension.employee_city = 'Boston')
 |  Filter: (employee_dimension.job_title = 'Cashier')
 |  Execute on: Query Initiator
 
在定向查询中引入变量

假设有以下两条查询语句:

=> SELECT Employee_first_name, Employee_last_name FROM EMP_Dimension WHERE 
Employee_city='Boston' and Employee_position='Cashier';

=> SELECT Employee_first_name, Employee_last_name FROM EMP_Dimension WHERE 
Employee_city='Chicago' and Employee_position='Greeter';

在此,你可以创建一个定向查询,在该查询中,将使用带有IGNORECONST来设置Employee_city和Employee_position两个条件:

=> SAVE QUERY SELECT Employee_first_name, Employee_last_name FROM EMP_Dimension 
   WHERE Employee_city='somewhere'/*+IGNORECONST(1)*/
   AND Employee_position='somejob' /*+IGNORECONST(2)*/;
SAVE QUERY

=> CREATE DIRECTED QUERY CUSTOM 'findEmployees' 
   SELECT Employee_first_name, Employee_last_name 
   FROM EMP_Dimension /*+projs('public.Emp_Dimension_Unseg')*/ 
   WHERE Employee_city='somewhere'/*+IGNORECONST(1)*/
   AND Employee_position='somejob'/*+IGNORECONST(2)*/
CREATE DIRECTED QUERY

=> ACTIVATE DIRECTED QUERY findEmployees;
ACTIVATE DIRECTED QUERY

IGNORECONST需要一个整数参数。此参数与您希望优化程序忽略的输入查询和带注释的查询中的常量匹配。在前面的例子中,使用IGNORECONST配对两组常数:

  • IGNORECONST(1)配对的输入和带注释的查询设置Employee_city。
  • IGNORECONST(2)配对的输入和带注释的查询设置Employee_position。

当优化器映射输入查询到定向查询findEmployees时,它将提示列Employee_city和Employee_position列去忽略设置为IGNORECONST的参数值。因此,用户可以为这些列提供任何值。

示例:directed query的使用

转自:定向查询的新用途
该示例使用directed query来将用户的查询定向到查询创建的flatten table,这样可以在不修改用户查询语句的情况下,使用flatten table。

  • 使用SAVE QUERY保存输入查询。例如,以下语句保存连接表T1,T2和T3的查询:
SAVE QUERY SELECT T1.a, T2,b, T3.c 
   FROM T1 JOIN T2 ON T1.x = T2.x
   JOIN T3 ON T2.y = T3.y
   WHERE T3.description ILIKE '%red%';
  • 创建一个自定义定向查询,该查询将保存的输入查询映射到查询flatten table T123的查询:
CREATE DIRECTED QUERY CUSTOM 'FindReds'
SELECT a, b, c FROM T123 WHERE description ILIKE '%red%';
  • 激活定向查询:
ACTIVATE DIRECTED QUERY FindReds;

当定向查询处于活动状态时,查询优化器会将与原始输入格式匹配的所有查询映射到定向查询。不是执行原始查询,而是使用带注释的查询,该查询查询flatten table T123。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值