My SQL query becomes too slow when a subquery is added in WHERE clause even though the individual run times of the queries is less than 1 minute. The query has the following skeleton
SELECT COL1, COL2, COL3, COL4, COL5, COL6, sum(COL7) FROM TABLE1
WHERE Col1 = 'something' AND COl2 = date AND Col3 = (SELECT MAX(COLUMN1) FROM TABLE2)
GROUP BY COL1, COL2, COL3, COL4, COL5, COL6
This query is running on SYBASE IQ.
Data for table 1 is 60M+ rows and post application of filter conditions is just 60 rows that usually takes 50 sec to run if subquery is replaced with hardcoded value.
Data for table 2 is 200 rows and post application of filter condition is just one integer value that individually takes 1 sec to run.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `TABLE1` ADD INDEX `table1_idx_col1_col2_date_col3_col4_col5` (`Col1`,`COl2`,`date`,`COL3`,`COL4`,`COL5`);
ALTER TABLE `TABLE2` ADD INDEX `table2_idx_column1` (`COLUMN1`);
SELECT
TABLE1.COL1,
TABLE1.COL2,
TABLE1.COL3,
TABLE1.COL4,
TABLE1.COL5,
TABLE1.COL6,
sum(TABLE1.COL7)
FROM
TABLE1
WHERE
TABLE1.Col1 = 'something'
AND TABLE1.COl2 = TABLE1.date
AND TABLE1.Col3 = (
SELECT
MAX(TABLE2.COLUMN1)
FROM
TABLE2
)
GROUP BY
TABLE1.COL1,
TABLE1.COL2,
TABLE1.COL3,
TABLE1.COL4,
TABLE1.COL5,
TABLE1.COL6
ORDER BY
NULL