Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix operational point search performance issues #10172

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions editoast/editoast_derive/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use syn::DeriveInput;
)]
struct SearchParams {
table: String,
#[darling(default)]
distinct_on: Option<String>,
migration: Option<Migration>,
#[darling(default)]
joins: String,
Expand Down Expand Up @@ -165,6 +167,9 @@ pub fn expand_search(input: &DeriveInput) -> Result<TokenStream> {

let struct_name = &input.ident;
let table = params.table;
let distinct_on = params
.distinct_on
.map_or_else(|| quote! { None }, |d| quote! { Some(#d.to_owned()) });
let joins = match params.joins {
ref joins if !joins.is_empty() => quote! { Some(#joins.to_owned()) },
_ => quote! { None },
Expand Down Expand Up @@ -286,6 +291,7 @@ pub fn expand_search(input: &DeriveInput) -> Result<TokenStream> {
fn search_config() -> editoast_search::SearchConfig {
editoast_search::SearchConfig {
table: #table.to_owned(),
distinct_on: #distinct_on,
joins: #joins,
criterias: Vec::from([#criterias]),
properties: Vec::from([#properties]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl editoast_search::SearchObject for Track {
fn search_config() -> editoast_search::SearchConfig {
editoast_search::SearchConfig {
table: "search_track".to_owned(),
distinct_on: None,
joins: None,
criterias: Vec::from([
editoast_search::Criteria {
Expand Down
6 changes: 5 additions & 1 deletion editoast/editoast_search/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ pub fn query_into_sql(
}
let where_expression = context.search_ast_to_sql(&ast)?;
let table = &search_config.table;
let select = search_config.distinct_on.as_ref().map_or_else(
|| "SELECT".to_owned(),
|columns| format!("SELECT DISTINCT ON {columns}"),
);
let joins = search_config.joins.as_ref().cloned().unwrap_or_default();
let result_columns = search_config.result_columns();
let mut bindings = Default::default();
let constraints = where_expression.to_sql(&mut bindings);
let sql_code = format!(
"WITH _RESULT AS (
SELECT {result_columns}
{select} {result_columns}
FROM {table}
{joins}
WHERE {constraints}
Expand Down
1 change: 1 addition & 0 deletions editoast/editoast_search/src/search_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Property {

pub struct SearchConfig {
pub table: String,
pub distinct_on: Option<String>,
pub criterias: Vec<Criteria>,
pub properties: Vec<Property>,
pub joins: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ pub(super) struct SearchResultItemTrack {
#[derive(Search, Serialize, ToSchema)]
#[search(
table = "search_operational_point",
distinct_on = "(infra_id, obj_id)",
migration(src_table = "infra_object_operational_point"),
joins = "
INNER JOIN infra_object_operational_point AS OP ON OP.id = search_operational_point.id
INNER JOIN (SELECT DISTINCT ON (infra_id, obj_id) * FROM infra_layer_operational_point)
AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id",
INNER JOIN infra_layer_operational_point AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id",
column(
name = "obj_id",
data_type = "varchar(255)",
Expand Down
Loading