Exclude entries matching the subquery

There is probably an obvious answer to this question, but I have one minute of time ever with her.

Consider the query:

SELECT *
FROM reports AS r
JOIN reportvalues ​​AS rv ON rv.report_id = r.report_id
JOIN metrics AS m ON m.metric_id = rv.metric_id
WHERE r.report_id NOT IN (
    SELECT DISTINCT report_id
    FROM exclude_report
)

In this query exclude_report, this is a view constructed in a similar fashion.

Now it happens that the query takes EXTREMELY for a long time to execute, apparently because the subquery is executed for each row in the parent query. However, I do not find any possible ways to do this in any other way.

Great SQL Wizards, please advise. I really need a way to do all this in SQL, which I will use in SSRS.

+3
source share
4 answers

The differences are probably killing you, you don't need to distinguish between the subquery when used in

This is better?

SELECT *
FROM reports AS r
JOIN reportvalues AS rv ON rv.report_id = r.report_id
JOIN metrics AS m ON m.metric_id = rv.metric_id
WHERE NOT EXISTS (SELECT 1 
        FROM exclude_report e 
        WHERE e.report_id = r.report_id)
+4
source
SELECT *
FROM reports AS r
JOIN reportvalues AS rv ON rv.report_id = r.report_id
JOIN metrics AS m ON m.metric_id = rv.metric_id
LEFT JOIN exclude_report er ON r.report_id = er.report_id
WHERE er.report_id IS NULL
0
source

, . .

0

, :

  • report_id report
  • report_id reportvalues
  • metric_id reportvalues
  • metric_id metrics
  • report_id exclude_report
0

Source: https://habr.com/ru/post/1747914/


All Articles