From 9ddf2da518c66092f7c050f3118e00ab63761c81 Mon Sep 17 00:00:00 2001 From: c-bata Date: Fri, 6 May 2022 19:02:48 +0900 Subject: [PATCH] Fix N+1 queries problem --- optuna_dashboard/_app.py | 6 +++--- optuna_dashboard/_serializer.py | 5 ----- optuna_dashboard/ts/apiClient.ts | 7 ------- optuna_dashboard/ts/components/StudyList.tsx | 13 +------------ optuna_dashboard/ts/types/index.d.ts | 1 - 5 files changed, 4 insertions(+), 28 deletions(-) diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index 6b140839..960c16fb 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -136,7 +136,7 @@ def json_api_view(view: BottleView) -> BottleView: def get_study_summary(storage: BaseStorage, study_id: int) -> Optional[StudySummary]: if version.parse(optuna_ver) >= version.Version("3.0.0b0.dev"): - summaries = storage.get_all_study_summaries(include_best_trial=True) # type: ignore + summaries = storage.get_all_study_summaries(include_best_trial=False) # type: ignore else: summaries = storage.get_all_study_summaries() # type: ignore for summary in summaries: @@ -158,7 +158,7 @@ def get_trials( and datetime.now() - last_fetched_at < timedelta(seconds=ttl_seconds) ): return trials - trials = storage.get_all_trials(study_id) + trials = storage.get_all_trials(study_id, deepcopy=False) with trials_cache_lock: trials_last_fetched_at[study_id] = datetime.now() trials_cache[study_id] = trials @@ -212,7 +212,7 @@ def create_app(storage: BaseStorage, debug: bool = False) -> Bottle: @json_api_view def list_study_summaries() -> BottleViewReturn: if version.parse(optuna_ver) >= version.Version("3.0.0b0.dev"): - summaries = storage.get_all_study_summaries(include_best_trial=True) # type: ignore + summaries = storage.get_all_study_summaries(include_best_trial=False) # type: ignore else: summaries = storage.get_all_study_summaries() # type: ignore serialized = [serialize_study_summary(summary) for summary in summaries] diff --git a/optuna_dashboard/_serializer.py b/optuna_dashboard/_serializer.py index d33ec75f..b2dda478 100644 --- a/optuna_dashboard/_serializer.py +++ b/optuna_dashboard/_serializer.py @@ -79,11 +79,6 @@ def serialize_study_summary(summary: StudySummary) -> Dict[str, Any]: if summary.datetime_start is not None: serialized["datetime_start"] = (summary.datetime_start.isoformat(),) - if summary.best_trial: - serialized["best_trial"] = serialize_frozen_trial( - summary._study_id, summary.best_trial - ) - return serialized diff --git a/optuna_dashboard/ts/apiClient.ts b/optuna_dashboard/ts/apiClient.ts index 55495b77..c6fa35e2 100644 --- a/optuna_dashboard/ts/apiClient.ts +++ b/optuna_dashboard/ts/apiClient.ts @@ -70,9 +70,6 @@ export const getStudyDetailAPI = ( name: res.data.name, datetime_start: new Date(res.data.datetime_start), directions: res.data.directions, - best_trial: res.data.best_trial - ? convertTrialResponse(res.data.best_trial) - : undefined, trials: trials, union_search_space: res.data.union_search_space, intersection_search_space: res.data.intersection_search_space, @@ -111,14 +108,10 @@ export const getStudySummariesAPI = (): Promise => { .get(`/api/studies`, {}) .then((res) => { return res.data.study_summaries.map((study): StudySummary => { - const best_trial = study.best_trial - ? convertTrialResponse(study.best_trial) - : undefined return { study_id: study.study_id, study_name: study.study_name, directions: study.directions, - best_trial: best_trial, user_attrs: study.user_attrs, system_attrs: study.system_attrs, datetime_start: study.datetime_start diff --git a/optuna_dashboard/ts/components/StudyList.tsx b/optuna_dashboard/ts/components/StudyList.tsx index 3d61ff8d..6fc2b0a6 100644 --- a/optuna_dashboard/ts/components/StudyList.tsx +++ b/optuna_dashboard/ts/components/StudyList.tsx @@ -123,17 +123,6 @@ export const StudyList: FC<{ sortable: false, toCellValue: (i) => studies[i].directions.join(), }, - { - field: "best_trial", - label: "Best value", - sortable: false, - toCellValue: (i) => { - if (studies[i].directions.length !== 1) { - return "-" // Multi-objective study does not hold best_trial attribute. - } - return studies[i].best_trial?.values?.[0] || null - }, - }, { field: "study_name", label: "", @@ -362,7 +351,7 @@ export const StudyList: FC<{ rows={studies} keyField={"study_id"} collapseBody={collapseBody} - initialRowsPerPage={-1} + initialRowsPerPage={10} rowsPerPageOption={[5, 10, { label: "All", value: -1 }]} defaultFilter={studyFilter} /> diff --git a/optuna_dashboard/ts/types/index.d.ts b/optuna_dashboard/ts/types/index.d.ts index d62102ce..7af52573 100644 --- a/optuna_dashboard/ts/types/index.d.ts +++ b/optuna_dashboard/ts/types/index.d.ts @@ -68,7 +68,6 @@ declare interface StudySummary { study_id: number study_name: string directions: StudyDirection[] - best_trial?: Trial user_attrs: Attribute[] system_attrs: Attribute[] datetime_start?: Date