For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
The optimization process and recommendations:
- Avoid Calling Functions With Indexed Columns (query line: 12): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 17): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 22): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 27): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 32): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 37): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `parameters` is indexed, the index won’t be used as it’s wrapped with the function `jsonb_extract_path`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
- Use Numeric Column Types For Numeric Values (query line: 12): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 17): Referencing a numeric value (e.g. 2) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 22): Referencing a numeric value (e.g. 5) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 27): Referencing a numeric value (e.g. 6) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 32): Referencing a numeric value (e.g. 7) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 37): Referencing a numeric value (e.g. 8) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
CREATE INDEX data_idx_measure_id_id ON "data" ("measure_id","id");
The optimized query:
SELECT
d.id,
d.measure_id,
CAST(d.attributes AS TEXT) AS attributes,
CAST(d.parameters AS TEXT) AS parameters,
d.value
FROM
data d
WHERE
d.measure_id = 19
AND (
jsonb_extract_path(d.parameters, '1')::bigint IN (
12
)
)
AND (
jsonb_extract_path(d.parameters, '2')::bigint IN (
2, 59
)
)
AND (
jsonb_extract_path(d.parameters, '5')::bigint IN (
79, 80, 129, 130, 113
)
)
AND (
jsonb_extract_path(d.parameters, '6')::bigint IN (
249
)
)
AND (
jsonb_extract_path(d.parameters, '7')::bigint IN (
248
)
)
AND (
jsonb_extract_path(d.parameters, '8')::bigint IN (
412
)
)
ORDER BY
d.id