Answer the question
In order to leave comments, you need to log in
How to optimize SQL query so that SUM is not counted to the end?
The database has a simple, cascading data organization: table3 --> table2 --> table1 . Those. table3 is forked to table2 , which in turn is forked to table1 . Table3 has a boolean field bExported , which signals whether this record has been exported to another database.
And now I want to get such records from table1 so that they have at least one record that has not yet been exported "in conjunction": table3.bExported = FALSE
.
It seemed to me reasonable to sum up all the "related" bExported, and if this sum is less than the total number of records of the same selection, then I will get the desired:
SELECT
table1.*
FROM table3
INNER JOIN table2
ON table3.tab2_id = table2.id
INNER JOIN table1
ON table2.tab1_id = table1.id
GROUP BY
table1.id,
table1.datas
HAVING SUM(table3.bExported) < COUNT(table3.id)
HAVING SUM(table3.bExported) > 0
-- it takes the same time, although it would seem to be guessed that as soon as SUM(table3.bExported) becomes equal one can no longer scan tables, because table3.bExported is logical and it cannot contain negative numbers ... table3.bExported = FALSE
and that's enough. And table3.bExported -- has an index and it's quick to find it... Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question