I am currently struggling with a strange behavior with Crate using Python client. Let me explain with an example:
this is the query (may be is not the most optimized one):
SELECT
dr.id as id, dr.name as name, dr.description as description,
c.code as category_id, dr.unit as unit, c.cat_id as category_id_int,
c.name as category_name, f.code as filter_id, f.name as filter_name,
dr.isnumeric as is_numeric, array_agg(a.alarm_id) as alarms,
dr.gateway_config, dr.type
FROM project_demo.desc_real dr
LEFT JOIN project.categories c on dr.category_id = c.cat_id
LEFT JOIN project.var_filters f on dr.filter_id = f.filter_id
LEFT JOIN project.alarm_variable a ON a.var_id = dr.id AND a.var_type = 'real'
WHERE dr.id = ANY(?)
GROUP BY dr.id, dr.name, dr.description, c.code, dr.unit, c.cat_id, c.name, f.code, f.name, dr.isnumeric, dr.gateway_config, dr.type
ORDER BY dr.id ASC;
The code example used is:
var_ids = [231,232]
start=time.time()
cursor.execute(sql, (var_ids,))
elapsed = time.time() - start
print(elapsed)
Using this method the elapsed time is 119.877 seconds and CPU usage is 100% during the query.
If changing the WHERE clause to use directly an array in the query:
SELECT
dr.id as id, dr.name as name, dr.description as description,
c.code as category_id, dr.unit as unit, c.cat_id as category_id_int,
c.name as category_name, f.code as filter_id, f.name as filter_name,
dr.isnumeric as is_numeric, array_agg(a.alarm_id) as alarms,
dr.gateway_config, dr.type
FROM project_demo.desc_real dr
LEFT JOIN project.categories c on dr.category_id = c.cat_id
LEFT JOIN project.var_filters f on dr.filter_id = f.filter_id
LEFT JOIN project.alarm_variable a ON a.var_id = dr.id AND a.var_type = 'real'
WHERE dr.id = ANY([231,232])
GROUP BY dr.id, dr.name, dr.description, c.code, dr.unit, c.cat_id, c.name, f.code, f.name, dr.isnumeric, dr.gateway_config, dr.type
ORDER BY dr.id ASC;
start=time.time()
cursor.execute(sql)
elapsed = time.time() - start
print(elapsed)
In this case the elapsed time is 0.467seconds.
I have seen this behavior with other (complex) queries also. Any suggestion on this?
Best regards,