简述
Apache druid 中的 COUNT(DISTINCT expr) 默认是使用 近似值算法HyperLogLog.
原来介绍过druid本身是为了海量数据分析而设计,所以在大数据量的情况下,使用近似值算法,速度更快。但有的需求,数据量本身较小,对数据的精准性要求比较高。不希望使用近似值的方式。
取消近似值的方式
连接时设置 useApproximateCountDistinct
useApproximateCountDistinct 默认值是true,设置false后,将使用精准值计算。
String url = "jdbc:avatica:remote:url=http://" + "ip:8082"
+ "/druid/v2/sql/avatica/";
Properties connectionProperties = new Properties();
connectionProperties.setProperty("useApproximateCountDistinct","false")
Connection connection = DriverManager.getConnection(url, connectionProperties);
broker 配置
druid.sql.planner.useApproximateCountDistinct=false
总结
根据自己的需求场景设置,一般建议在连接时使用,更加灵活些。在broker加入配置后,所有的近似值都将无法生效。也将会影响查询性能。