在查询中,如果我在主键上使用LIKE '<value>%'
,则使用索引的效果很好:
Operator | Rows returned | Executions | Latency
-- | -- | -- | --
Serialize Result 32 1 1.80 ms
Sort 32 1 1.78 ms
Hash Aggregate 32 1 1.73 ms
Distributed union 32 1 1.61 ms
Hash Aggregate 32 1 1.56 ms
Distributed union 128 1 1.34 ms
Compute - - -
FilterScan 128 1 1.33 ms
Table Scan: <tablename> 128 1 1.30 ms
不过,使用LIKE '<value>_'
会执行全表扫描:
Operator | Rows returned | Executions | Latency
-- | -- | -- | --
Serialize Result | 32 | 1 | 76.27 s
Sort | 32 | 1 | 76.27 s
Hash Aggregate | 32 | 1 | 76.27 s
Distributed union | 32 | 1 | 76.27 s
Hash Aggregate | 32 | 2 | ~72.18 s
Distributed union | 128 | 2 | ~72.18 s
Compute | - | - | -
FilterScan | 128 | 2 | ~72.18 s
Table Scan: <tablename> (full scan: true) | 13802624 | 2 | ~69.97 s
查询如下:
SELECT
'aggregated-quadkey AS quadkey' AS quadkey, day,
SUM(a_value_1), SUM(a_value_2), AVG(a_value_3), SUM(a_value_4), SUM(a_value_5), AVG(a_value_6), AVG(a_value_6), AVG(a_value_7), SUM(a_value_8), SUM(a_value_9), AVG(a_value_10), SUM(a_value_11), SUM(a_value_12), AVG(a_value_13), AVG(a_value_14), AVG(a_value_15), SUM(a_value_16), SUM(a_value_17), AVG(a_value_18), SUM(a_value_19), SUM(a_value_20), AVG(a_value_21), AVG(a_value_22), AVG(a_value_23)
FROM <tablename>
WHERE quadkey LIKE '03201012212212322_'
GROUP BY quadkey, day ORDER BY day
答案 0 :(得分:4)
对于与LIKE模式(column LIKE 'xxx%'
)匹配的前缀,查询优化器在内部将条件转换为STARTS_WITH(column, 'xxx')
,然后使用索引。
所以原因可能是因为查询优化器不够聪明,无法 转换与LIKE模式匹配的精确长度前缀
column LIKE 'xxx_'
合并为一个条件:
(STARTS_WITH(column, 'xxx') AND CHAR_LENGTH(column)=4)
类似地,诸如
`column LIKE 'abc%def'`
未在组合条件下优化:
`(STARTS_WITH(column,'abc') AND ENDS_WITH(column,'def'))`.
您始终可以通过使用上述条件在SQL生成中优化查询来解决此问题。
(这假设LIKE模式是查询中的字符串值,而不是参数-使用参数的LIKE
无法优化,因为该模式在查询编译时未知。)
答案 1 :(得分:4)
感谢您举报!我已经在待办事项列表中添加了此重写。同时,您可以按照RedPandaCurios的建议使用STARTS_WITH和CHAR_LENGTH解决此问题。