perf: separate out blob and string tag queries

This commit is contained in:
Greg Heartsfield 2023-02-01 07:13:29 -06:00
parent 2d3a35fe30
commit 1820e9c689

View File

@ -800,21 +800,50 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
str_vals.push(Box::new(v.clone())); str_vals.push(Box::new(v.clone()));
} }
} }
// create clauses with "?" params for each tag value being searched // do not mix value and value_hex; this is a temporary special case.
let str_clause = format!("value IN ({})", repeat_vars(str_vals.len())); if str_vals.len() == 0 {
let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len())); // create clauses with "?" params for each tag value being searched
// find evidence of the target tag name/value existing for this event. let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len()));
let tag_clause = format!( // find evidence of the target tag name/value existing for this event.
"e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND ({} OR {})))", let tag_clause = format!(
str_clause, blob_clause "e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND {}))",
); blob_clause
// add the tag name as the first parameter );
params.push(Box::new(key.to_string())); // add the tag name as the first parameter
// add all tag values that are plain strings as params params.push(Box::new(key.to_string()));
params.append(&mut str_vals); // add all tag values that are blobs as params
// add all tag values that are blobs as params params.append(&mut blob_vals);
params.append(&mut blob_vals); filter_components.push(tag_clause);
filter_components.push(tag_clause); } else if blob_vals.len() == 0 {
// create clauses with "?" params for each tag value being searched
let str_clause = format!("value IN ({})", repeat_vars(str_vals.len()));
// find evidence of the target tag name/value existing for this event.
let tag_clause = format!(
"e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND {}))",
str_clause
);
// add the tag name as the first parameter
params.push(Box::new(key.to_string()));
// add all tag values that are blobs as params
params.append(&mut str_vals);
filter_components.push(tag_clause);
} else {
// create clauses with "?" params for each tag value being searched
let str_clause = format!("value IN ({})", repeat_vars(str_vals.len()));
let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len()));
// find evidence of the target tag name/value existing for this event.
let tag_clause = format!(
"e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND ({} OR {})))",
str_clause, blob_clause
);
// add the tag name as the first parameter
params.push(Box::new(key.to_string()));
// add all tag values that are plain strings as params
params.append(&mut str_vals);
// add all tag values that are blobs as params
params.append(&mut blob_vals);
filter_components.push(tag_clause);
}
} }
} }
// Query for timestamp // Query for timestamp