MySQL Forums
Forum List  »  Performance

specify conditions from outer query on a materialized subquery
Posted by: Pratik Gandhi
Date: July 12, 2016 10:55PM

i have got the below query which references couple of views 'goldedRunQueries' and 'currentGoldMarkings'. My issue seems to be from the view that is referred in the subquery - currentGoldMarkings. While execution, MySQL first materializes this subquery and then implements the where clauses of 'queryCode' and 'runId', which therefore results in execution time of more than hour as the view refers tables that has got millions of rows of data. My question is how do I enforce those two where conditions on the subquery before it materializes.

SELECT goldedRunQueries.queryCode, goldedRunQueries.runId
(SELECT measuredRunId, queryCode, COUNT(resultId) as c
FROM currentGoldMarkings
GROUP BY measuredRunId, queryCode) AS accuracy
ON accuracy.measuredRunId = goldedRunQueries.runId AND accuracy.queryCode = goldedRunQueries.queryCode
WHERE goldedRunQueries.queryCode IN ('CH001', 'CH002', 'CH003')
and goldedRunQueries.runid = 5000
ORDER BY goldedRunQueries.runId DESC, goldedRunQueries.queryCode;

Here are the two views. Both of these also get used in a standalone mode and so integrating any clauses into them is not possible.

CREATE VIEW currentGoldMarkings
SELECT result.resultId, result.runId AS measuredRunId, result.documentId, result.queryCode, result.queryValue AS measuredValue,
gold.queryValue AS goldValue,
CASE result.queryValue WHEN gold.queryValue THEN 1 ELSE 0 END AS correct
FROM results AS result INNER JOIN
gold ON gold.documentId = result.documentId AND gold.queryCode = result.queryCode
WHERE gold.isCurrent = 1

CREATE VIEW goldedRunQueries
SELECT runId, queryCode
FROM runQueries
(SELECT 1 AS Expr1
FROM runs
WHERE (runId = runQueries.runId) AND (isManual = 0)) AND EXISTS
(SELECT 1 AS Expr1
FROM results
WHERE (runId = runQueries.runId) AND (queryCode = runQueries.queryCode) AND EXISTS
(SELECT 1 AS Expr1
FROM gold
WHERE (documentId = results.documentId) AND (queryCode = results.queryCode)))

Note: The above query reflects only a part of my actual query. There are 3 other left outer joins which are similar in nature to the above subquery which makes the problem far more worse.

Options: ReplyQuote

Written By
specify conditions from outer query on a materialized subquery
July 12, 2016 10:55PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.