我正在执行一个 influxSQL 查询,该查询在 1 分钟内返回结果。询问 :
select SUM(call_duration) as total_duration,Count(Distinct(recipient_id)) as total_recipients from xyz where target_id = '1';
单独的查询,如
select Count(Distinct(recipient_id)) as total_recipients from xyz where target_id = '1';
select SUM(call_duration) as total_duration from xyz where target_id = '1';
也需要超过 1 分钟才能返回。
而查询
select MAX(call_duration), MIN(call_duration) from xyz where target_id = '1';
返回结果非常快,在几秒钟内,如 3-4 秒。
表(测量值)xyz 非常大。有超过 1000 万条记录符合这个 where 条件。call_duration 和 recipient_id 是 Fields 而 target_id 是 Tag
虽然 MIN 和 MAX 函数返回结果非常快,但我怀疑为什么 SUM 需要更多时间?
InfluxDB 版本:1.7.4 机器配置 - AWS EC2 - t2.medium(4 GB RAM)
配置文件:
[meta]
dir = "/var/lib/influxdb/meta"
[data]
dir = "/var/lib/influxdb/data"
wal-dir = "/var/lib/influxdb/wal"
series-id-set-cache-size = 100
[coordinator]
[retention]
[shard-precreation]
[monitor]
[http]
enabled = true
auth-enabled = true
log-enabled = true
access-log-path = "/var/log/influxdb/http_access.log"
write-tracing = false
max-body-size = 0
max-concurrent-write-limit = 0
[logging]
[subscriber]
[[graphite]]
[[collectd]]
[[opentsdb]]
[[udp]]
[continuous_queries]
[tls]
我认为结果应该在 5-6 秒内返回
EXPLAIN select MIN(call_duration) from xyz where target_id='1';
QUERY PLAN
----------
EXPRESSION: min(call_duration::float)
NUMBER OF SHARDS: 21
NUMBER OF SERIES: 85134
CACHED VALUES: 0
NUMBER OF FILES: 24642
NUMBER OF BLOCKS: 31608
SIZE OF BLOCKS: 65520278
EXPLAIN select MAX(call_duration) from xyz where target_id='1';
QUERY PLAN
----------
EXPRESSION: max(call_duration::float)
NUMBER OF SHARDS: 21
NUMBER OF SERIES: 85134
CACHED VALUES: 0
NUMBER OF FILES: 24642
NUMBER OF BLOCKS: 31608
SIZE OF BLOCKS: 65520278
EXPLAIN select SUM(call_duration) as total_call_duration from xyz where target_id='1';
QUERY PLAN
----------
EXPRESSION: sum(call_duration::float)
NUMBER OF SHARDS: 21
NUMBER OF SERIES: 85134
CACHED VALUES: 0
NUMBER OF FILES: 24642
NUMBER OF BLOCKS: 31608
SIZE OF BLOCKS: 65520278
EXPLAIN select Count(Distinct(recipient_id)) as total_recipients from xyz where target_id='1';
QUERY PLAN
----------
EXPRESSION: recipient_id::integer
NUMBER OF SHARDS: 21
NUMBER OF SERIES: 85134
CACHED VALUES: 0
NUMBER OF FILES: 24642
NUMBER OF BLOCKS: 31608
SIZE OF BLOCKS: 82448255