Skip to content

Commit 98df395

Browse files
authored
[ES|QL] Create dataviews without asking for fields wherever is not required (elastic#240197)
## Summary Fetching fields can be expensive. I have upgraded the esql adhoc dataview helper to have a skip fields flag. I have also looked into the codebase and I apply it wherever is not required. This will give a performance boost to the consumers especially for sources with many fields. Note: I have tested the applications and I don't see any regressions but please test carefully too!
1 parent 71ad14a commit 98df395

7 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/platform/packages/shared/kbn-esql-utils/src/utils/get_esql_adhoc_dataview.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function getESQLAdHocDataview(
3636
options?: {
3737
allowNoIndex?: boolean;
3838
createNewInstanceEvenIfCachedOneAvailable?: boolean;
39+
skipFetchFields?: boolean;
3940
}
4041
) {
4142
const timeField = getTimeFieldFromESQLQuery(query);
@@ -46,13 +47,18 @@ export async function getESQLAdHocDataview(
4647
// overwise it might return a cached data view with a different time field
4748
dataViewsService.clearInstanceCache(dataViewId);
4849
}
50+
const skipFetchFields = options?.skipFetchFields ?? false;
4951

50-
const dataView = await dataViewsService.create({
51-
title: indexPattern,
52-
type: ESQL_TYPE,
53-
id: dataViewId,
54-
allowNoIndex: options?.allowNoIndex,
55-
});
52+
const dataView = await dataViewsService.create(
53+
{
54+
title: indexPattern,
55+
type: ESQL_TYPE,
56+
id: dataViewId,
57+
allowNoIndex: options?.allowNoIndex,
58+
},
59+
// important to skip if you just need the dataview without the fields for performance reasons
60+
skipFetchFields
61+
);
5662

5763
dataView.timeFieldName = timeField;
5864

x-pack/platform/plugins/shared/lens/public/app_plugin/shared/edit_on_the_fly/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const getGridAttrs = async (
7070

7171
const dataView = dataViewSpec
7272
? await data.dataViews.create(dataViewSpec)
73-
: await getESQLAdHocDataview(query.esql, data.dataViews);
73+
: await getESQLAdHocDataview(query.esql, data.dataViews, { skipFetchFields: true });
7474

7575
const filter = getDSLFilter(data.query, uiSettings, dataView.timeFieldName);
7676

x-pack/platform/plugins/shared/lens/public/datasources/form_based/esql_layer/text_based_languages.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,9 @@ export function getTextBasedDatasource({
680680

681681
for (const { query } of Object.values(state.layers)) {
682682
if (query) {
683-
const esqlAdhocDataview = await getESQLAdHocDataview(query.esql, dataViewsService);
683+
const esqlAdhocDataview = await getESQLAdHocDataview(query.esql, dataViewsService, {
684+
skipFetchFields: true,
685+
});
684686
indexPatterns.push(esqlAdhocDataview);
685687
}
686688
}

x-pack/platform/plugins/shared/lens/public/datasources/form_based/esql_layer/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function getStateFromAggregateQuery(
9797
let columnsFromQuery: DatatableColumn[] = [];
9898
let timeFieldName;
9999
try {
100-
const dataView = await getESQLAdHocDataview(query.esql, dataViews);
100+
const dataView = await getESQLAdHocDataview(query.esql, dataViews, { skipFetchFields: true });
101101

102102
if (dataView && dataView.id) {
103103
dataViewId = dataView?.id;

x-pack/platform/plugins/shared/lens/public/lens_suggestions_api/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The api returns an array of suggestions.
4040

4141
```typescript
4242
const indexName = (await getIndexForESQLQuery({ dataViews })) ?? '*';
43-
const dataView = await getESQLAdHocDataview(`from ${indexName}`, dataViews);
43+
const dataView = await getESQLAdHocDataview(`from ${indexName}`, dataViews, { skipFetchFields: true });
4444
```
4545
Optional parameters:
4646
- `preferredChartType`: Use this if you want the suggestions api to prioritize a specific suggestion type.

x-pack/platform/plugins/shared/onechat/public/application/components/tools/esql/visualize_esql/use_lens_input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function useLensInput({
5656

5757
const lensHelpersAsync = useAsync(() => lens.stateHelperApi(), [lens]);
5858
const dataViewAsync = useAsync(
59-
() => getESQLAdHocDataview(esqlQuery, dataViews),
59+
() => getESQLAdHocDataview(esqlQuery, dataViews, { skipFetchFields: true }),
6060
[esqlQuery, dataViews]
6161
);
6262

x-pack/solutions/observability/plugins/observability_ai_assistant_app/public/functions/visualize_esql.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function VisualizeESQL({
112112
}, [lens]);
113113

114114
const dataViewAsync = useAsync(() => {
115-
return getESQLAdHocDataview(query, dataViews);
115+
return getESQLAdHocDataview(query, dataViews, { skipFetchFields: true });
116116
}, [query, dataViews]);
117117

118118
const chatFlyoutSecondSlotHandler = useContext(ObservabilityAIAssistantMultipaneFlyoutContext);

0 commit comments

Comments
 (0)