【问题标题】:which is the most optimal way to filter out string column in Hive?在 Hive 中过滤掉字符串列的最佳方法是什么?
【发布时间】:2020-02-02 11:17:23
【问题描述】:

我正在过滤一个名为 mycolumn 的字符串列。我可以想到以下 3 种过滤方式,哪一种会获得更好的性能?

-- method #1
where
(   mycolumn = 'FixedStringA.FixedStringB.VariableStringA.FixedString' 
OR mycolumn = 'FixedStringA.FixedStringB.VariableStringB.FixedString' 
OR mycolumn = 'FixedStringA.FixedStringB.VariableStringC.FixedString' 
OR mycolumn = 'FixedStringA.FixedStringB.VariableStringD.FixedString' );

-- method #2
where mycolumn like '%//.FixedString';

-- method #3
where split(mycolumn,'//.')[3] = 'FixedString';

请知道 FixedStringA 和 FixedStringB 就像常量一样,它们的值将保持固定,这就是为什么称它们为固定字符串。而且 mycolumn 不是您信息的分区键。

【问题讨论】:

  • 如果您的过滤器一直是静态的,我建议使用方法 #1,这可以为您节省时间/性能。或者,如果您有需要更改过滤条件的情况,最好使用方法 #2 和方法 #3,它们是一种正则表达式类型。

标签: sql hive query-optimization hiveql where-clause


【解决方案1】:

在方法 3 中,它应该是 \\. 而不是 //. 看到这个答案:Dot in regexp

同样在方法2中应该是like '%.FixedString'

你也可以使用 rlike:

RLIKE '\\.FixedString$'

您不会注意到 Hive 的不同之处,因为它无论如何都是全扫描的并且是并行运行的。

我更喜欢RLIKE '\\.FixedString$',因为它短而强大,类似于正则表达式。

拆分和生成数组(方法 3)会对内存和垃圾收集器产生更大的压力,您将创建和释放更多的对象。

方法 1 也不错,但看起来不那么优雅。如果你有像ORC文件这样的内部索引,方法1会更好,你需要自己检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多