S
S
Sergey Eremin2020-12-19 05:03:03
MySQL
Sergey Eremin, 2020-12-19 05:03:03

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)

But there are half a billion records in table3 and iterate over them all, make SUM and COUNT -- not fast... In fact, even 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 ...

And now I don’t understand how to optimize this. In reality, there is no need to "add" and "count" anything... You just need to find at least one table3.bExported = FALSE and that's enough. And table3.bExported -- has an index and it's quick to find it...

But how to write such a query?...

PS It is absolutely ideal if such a request can be written to Django-ORM ... But, in my opinion, this is already from the realm of fantasy ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2020-12-19
@idShura

remove
HAVING SUM(table3.bExported) < COUNT(table3.id)
and add
WHERE table3.bExported = FALSE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question