How to optimize this SQL query?
In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
- Description of the steps you can take to speed up the query.
- The optimal indexes for this query, which you can copy and create in your database.
- An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
- Avoid Correlated Subqueries (query line: 34): A correlated subquery is a subquery that contains a reference (column: FileName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 8): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 11): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 14): A correlated subquery is a subquery that contains a reference (column: ServerName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 17): A correlated subquery is a subquery that contains a reference (column: DatabaseName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 21): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 24): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 27): A correlated subquery is a subquery that contains a reference (column: WorksheetName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 94): A correlated subquery is a subquery that contains a reference (column: FileName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 68): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 71): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 74): A correlated subquery is a subquery that contains a reference (column: ServerName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 77): A correlated subquery is a subquery that contains a reference (column: DatabaseName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 81): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 84): A correlated subquery is a subquery that contains a reference (column: ConnectionType) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Correlated Subqueries (query line: 87): A correlated subquery is a subquery that contains a reference (column: WorksheetName) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
- Avoid Subqueries (query line: 5): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
- Avoid Subqueries (query line: 54): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
The optimized query:
SELECT
MAX(DatasourceConnectionID) AS DatasourceConnectionID,
ConnectionDescription
FROM
(SELECT
meta.DatasourceConnection.DatasourceConnectionID,
CASE
WHEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) = 'SQL' THEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' TO [' + (SELECT
TOP 1 ServerName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + '].[' + (SELECT
TOP 1 DatabaseName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ']'
WHEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) = 'EXCEL' THEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' (last workshet ' + (SELECT
TOP 1 WorksheetName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ')'
ELSE (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' ' + isnull((SELECT
TOP 1 FileName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)),
'') END AS ConnectionDescription
FROM
meta.DatasetLoad
INNER JOIN
meta.DatasourceConnection
ON meta.DatasetLoad.DatasourceConnectionID = meta.DatasourceConnection.DatasourceConnectionID
INNER JOIN
meta.DataSource
ON meta.DatasourceConnection.DataSourceID = meta.DataSource.DataSourceID
INNER JOIN
meta.Connection
ON meta.DatasourceConnection.ConnectionID = meta.Connection.ConnectionID
WHERE
DatasetID = 1156
AND DatasetLoadStatusID = 5
AND NOT meta.DatasourceConnection.DatasourceConnectionID IN (
SELECT
DISTINCT DatasourceConnectionID
FROM
meta.DatasetLoadSchedule
WHERE
meta.DatasetLoadSchedule.DatasetID = 1156
)
) Data
GROUP BY
ConnectionDescription
UNION
ALL SELECT
meta.DatasourceConnection.DatasourceConnectionID,
CASE
WHEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) = 'SQL' THEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' TO [' + (SELECT
TOP 1 ServerName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + '].[' + (SELECT
TOP 1 DatabaseName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ']'
WHEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) = 'EXCEL' THEN (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' (last workshet ' + (SELECT
TOP 1 WorksheetName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ')'
ELSE (SELECT
TOP 1 ConnectionType
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)) + ' ' + isnull((SELECT
TOP 1 FileName
FROM
meta.GetConnectionFromXML(meta.Connection.ConnectionDetails)),
'') END AS ConnectionDescription
FROM
meta.DatasetLoadSchedule
INNER JOIN
meta.DatasourceConnection
ON meta.DatasetLoadSchedule.DatasourceConnectionID = meta.DatasourceConnection.DatasourceConnectionID
INNER JOIN
meta.DataSource
ON meta.DatasourceConnection.DataSourceID = meta.DataSource.DataSourceID
INNER JOIN
meta.Connection
ON meta.DatasourceConnection.ConnectionID = meta.Connection.ConnectionID
WHERE
DatasetID = 1156