關於sparksql中的hint

2022-08-29 07:30:11 字數 1421 閱讀 4893

在spark中,結構化查詢可以通過指定查詢提示(hint)來進行優化。

查詢提示,即向查詢加入注釋,告訴查詢優化器提供如何優化邏輯計畫, 這在查詢優化器無法做出最佳決策時十分有用。

spark sql支援coalesce,repartition以及broadcast提示。 在分析查詢語句時,所有剩餘的未解析的提示將從查詢計畫中被移除。

spark sql 2.2增加了對提示框架(hint framework)的支援。

我們可以使用dataset.hint運算子或帶有提示的select sql語句指定查詢提示。

// dataset api

val q = spark.range(1).hint(name = "myhint", 100, true)

val plan = q.queryexecution.logical

scala> println(plan.numberedtreestring)

00 'unresolvedhint myhint, [100, true]

01 +- range (0, 1, step=1, splits=some(8))

// sql

val q = sql("select /*+ myhint (100, true) */ 1")

val plan = q.queryexecution.logical

scala> println(plan.numberedtreestring)

00 'unresolvedhint myhint, [100, true]

01 +- 'project [unresolvedalias(1, none)]

02 +- onerowrelation

select sql語句支援查詢提示作為sql查詢中的注釋,spark sql將其轉換為邏輯計畫中的unresolvedhint一元邏輯運算子。

coalesce 和 repartition 提示

spark sql 2.4增加了對coalesce和repartition提示的支援(使用sql注釋), 語法如下:

select /*+ coalesce(5) */ …​

select /*+ repartition(3) */ …​

broadcast提示

spark sql 2.2支援使用廣播標準函式或sql注釋的broadcast提示 ,語法如下:

select /*+ mapjoin(b) */ …​

select /*+ broadcastjoin(b) */ …​

select /*+ broadcast(b) */ …​

hint framework

Spark SQL中開窗函式詳解

row number 開窗函式 其實就是給每個分組的資料,按照其排序的順序,打上乙個分組內的行號,相當於grouptopn,在實際應用中非常廣泛。deptname name salary dept 1 michael 3000 dept 2 andy 5000 dept 1 alex 4500 de...

使用sparksql讀取mysql中的資料

val sc new sparkcontext conf val sqlcontext new sqlcontext sc var jdbcdf sqlcontext.read.format jdbc options map url jdbc mysql driver com.mysql.jdbc....

Hive與SparkSQL別名中的區別

先上 select vid,from unixtime gpsdate,yyyy mm dd hh mm ss as time from xx where pdt 2020 01 01 and vid 010019410390 order by gpsdate limit 10 一行簡單的 沒啥毛病...