[Solved] select most recent values in very large table

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 my_table_idx_client_obj_id_obj_id ON my_table (client,obj_type_id,obj_id);
CREATE INDEX my_table_idx_obj_id_mod_date ON my_table (obj_id,mod_date);
The optimized query:
SELECT
        MAX(trunc(my_table.mod_date,
        'dd')) AS last_modified_date,
        my_table.obj_id 
    FROM
        my_table 
    WHERE
        my_table.client = 'client_name' 
        AND my_table.obj_type_id = 12 
    GROUP BY
        my_table.obj_id

Related Articles



* original question posted on StackOverflow here.