簡述
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加入配置後,所有的近似值都將無法生效。也將會影響查詢性能。