I have the below query which i want to tune as the query takes more time to run. Please help me out.
where info_id not in (select info_id
where info_id is not null)
AND rownum <= 1000 ;
The innerquery is returning millions of rows and hence the problem.
How to optimize this SQL query?
The following recommendations will help you in your 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:
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.
Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `info_table` ADD INDEX `info_table_idx_rownum` (`rownum`);
ALTER TABLE `some_table` ADD INDEX `some_table_idx_info_id` (`info_id`);
The optimized query:
NOT EXISTS (
some_table.info_id IS NOT NULL
info_table.info_id = some_table.info_id
AND info_table.rownum <= 1000