from influxdb import InfluxDBClient client = InfluxDBClient('xx.xx.xx.xx', 8086, database='monitor') result = client.query("SELECT * FROM xx") for point in result.get_points(): pass
chunked (bool) – Enable to use chunked responses from InfluxDB. With chunked enabled, one ResultSet is returned per chunk containing all results within that chunk
chunk_size (int) – Size of each chunk to tell InfluxDB to use.
从以上描述中可以看出,使用这两个参数可以进行分块查询。
测试内存占用情况如下:
1 2 3 4 5 6 7
Line # Mem usage Increment Occurences Line Contents ============================================================ 14 51.5508 MiB 51.5508 MiB 1 @profile(precision=4) 15 def test2(): 16 54.3047 MiB 2.7539 MiB 1 result = client.query('SELECT * FROM sysio LIMIT 2000', chunked=True, chunk_size=100) 17 54.3047 MiB 0.0000 MiB 2001 for point in result.get_points(): 18 54.3047 MiB 0.0000 MiB 2000 pass
根据测试结果得出分块方式并不会减少内存占用,甚至会使用更多内存。
为什么分块了却没有减少内存的使用呢,这一点在influxdb的源码中可以看出。
1 2 3 4 5 6 7 8 9 10 11 12
def_read_chunked_response(response, raise_errors=True): result_set = {} for line in response.iter_lines(): ifisinstance(line, bytes): line = line.decode('utf-8') data = json.loads(line) for result in data.get('results', []): for _key in result: ifisinstance(result[_key], list): result_set.setdefault( _key, []).extend(result[_key]) return ResultSet(result_set, raise_errors=raise_errors)