[Solved] Why does Microsoft SQL Server 2012 query take minutes over JDBC 4.0 but second(s) in Management Studio?

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. 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.
Optimal indexes for this query:
CREATE INDEX objects_idx_identifier ON OBJECTS (IDENTIFIER);
CREATE INDEX properties_idx_object_target ON PROPERTIES (OBJECT,TARGET);
CREATE INDEX relations_idx_child ON RELATIONS (CHILD);
The optimized query:
SELECT
        PARENT.IDENTIFIER AS PARENT_IDENTIFIER,
        PARENT.CLASS AS PARENT_CLASS,
        CHILD.TYPE AS CHILD_TYPE,
        CHILD.IDENTIFIER AS CHILD_IDENTIFIER,
        PROPERTY.IDENTIFIER AS PROPERTY_IDENTIFIER,
        PROPERTY.DESCRIPTION AS PROPERTY_DESCRIPTION,
        PROPERTY.TYPE AS PROPERTY_TYPE,
        PROPERTY.PP AS PROPERTY_PP,
        PROPERTY.STATUS AS PROPERTY_STATUS,
        PROPERTY.TARGET AS PROPERTY_TARGET 
    FROM
        OBJECTS AS CHILD 
    LEFT OUTER JOIN
        RELATIONS 
            ON RELATIONS.CHILD = CHILD.IDENTIFIER 
    LEFT OUTER JOIN
        OBJECTS AS PARENT 
            ON RELATIONS.PARENT = PARENT.IDENTIFIER 
    INNER JOIN
        PROPERTIES AS PROPERTY 
            ON PROPERTY.OBJECT = CHILD.IDENTIFIER 
    WHERE
        PROPERTY.TARGET IS NOT NULL 
    ORDER BY
        CASE 
            WHEN PARENT.IDENTIFIER IS NULL THEN 1 
            ELSE 0 END,
PARENT.IDENTIFIER,
CHILD.IDENTIFIER,
PROPERTY.TARGET,
PROPERTY.IDENTIFIER

Related Articles



* original question posted on StackOverflow here.