简体   繁体   中英

Query Optimization to check if column is up to date in entire database in MySQL

I am trying to query mySQL to check if the value in a certain column in a table is updated. For example, I have a table with a column that keeps a max_age data. I want to check if the max_age is updated.

This is what I have done so far but I have tens of millions of data to go through. I want to know how many data is not updated from the entire database. Is there a way to optimize this query?

SELECT Count(a.column1) 
FROM   table1 a 
       INNER JOIN table2 b 
               ON a.column1 = b.column2 
       INNER JOIN table3 c 
               ON b.column2 = c.b.column3 
WHERE  a.max_age <> -1 
       AND a.max_age = ( 2013 - c.birth_year ) 


An example: This example the answer returned is 1 because [max_age = (2013 - birth_year)] in third row.

max_age   2013 - birth_year
34            31
27            25
44            44

The table table2 seem useless in your query, since a.column1 = b.column2 and b.column2 = b.column3 . This implies that a.column1 = c.column3 . Then:

SELECT COUNT(a.column1) FROM table1 a
INNER JOIN table2 b ON a.column1 = c.column3
WHERE a.max_age <> -1
AND a.max_age = (2013 - c.birth_year)

On the other hand, if you to optimize a little bit more you can force to use an index to to your query or even to create a View.

I hope this is useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM