[Solved] Optimize this SQL Query for XML inline function

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:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. 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.
  18. 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

Related Articles



* original question posted on StackOverflow here.