简体   繁体   中英

Tuning Oracle Query for slow select

I'm working on an oracle query that is doing a select on a huge table, however the joins with other tables seem to be costing a lot in terms of time of processing.

I'm looking for tips on how to improve the working of this query.

I'm attaching a version of the query and the explain plan of it.

Query

 SELECT
    l.gl_date,
    l.REST_OF_TABLES
    (
        SELECT
            MAX(tt.task_id)
        FROM
            bbb.jeg_pa_tasks tt
        WHERE
            l.project_id = tt.project_id
            AND l.task_number = tt.task_number
    ) task_id
FROM
    aaa.jeg_labor_history   l,
    bbb.jeg_pa_projects_all     p
WHERE
    p.org_id = 2165
    AND l.project_id = p.project_id
    AND p.project_status_code = '1000'

解释计划

Something to mention: This query takes data from oracle to send it to a sql server database, so I need it to be this big, I can't narrow the scope of the query. the purpose is to set it to a sql server job with SSIS so it runs periodically

As I don't know exactly how many rows this query is returning or how many rows this table/view has.

I can provide you few simple tips which might be helpful for you for better query performance:

  1. Check Indexes. There should be indexes on all fields used in the WHERE and JOIN portions of the SQL statement.

  2. Limit the size of your working data set.

  3. Only select columns you need.

  4. Remove unnecessary tables.

  5. Remove calculated columns in JOIN and WHERE clauses.

  6. Use inner join, instead of outer join if possible.

You view contains lot of data so you can also break down and limit only the information you need from this view

One obvious suggestion is not to use sub query in select clause.

Instead, you can try to join the tables.

SELECT
    l.gl_date,
    l.REST_OF_TABLES
    t.task_id
FROM
    aaa.jeg_labor_history   l
    Join bbb.jeg_pa_projects_all p
    On (l.project_id = p.project_id)
    Left join (SELECT
        tt.project_id, 
        tt.task_number,
        MAX(tt.task_id) task_id
    FROM
        bbb.jeg_pa_tasks tt
    Group by tt.project_id, tt.task_number) t
    On (l.project_id = t.project_id
            AND l.task_number = t.task_number)
WHERE
    p.org_id = 2165
    AND p.project_status_code = '1000';

Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM