I am in need of resetting flag of a table 'A' from 'X' to 'Y' where the update_date of a row satisfies the conditions 1. update_date > 1 month, 2. flag = 'X' & 3. type = 1.
And the update_date is checked against another table 'B'. I hope the following query will explain what exactly I need. Also this query works fine for me. But the problem is it is taking too long time. Actually my tables A & B are much bigger almost contains billion rows and there are about 10 columns.
When I run my sub query for selecting A.id I got the result immediately.
SELECT a.id
FROM A a
JOIN B b
ON (a.id = b.id
AND a.name = b.name
AND a.type = 1
AND a.flag = 'X'
AND a.update_date > DATE_SUB(NOW(), INTERVAL 1 MONTH) tmp_table)
But only the update query even if I put limit also it's taking much time.
UPDATE A
SET flag='Y'
WHERE id IN (SELECT a.id
FROM A a
JOIN B b
ON (a.id = b.id
AND a.name = b.name
AND a.type = 1
AND a.flag = 'X'
AND a.update_date > DATE_SUB(NOW(), INTERVAL 1 MONTH) tmp_table))
LIMIT 100
I am looking for alternate solutions of my query which makes it fast. Hope I could write a stored procedure for it. But in SP I should loop through for each target_ids right?
I don't wish to write two separate queries in PHP, since there are many threads of my PHP scripts running on cron which returns same results (time latency).
Also to note, I do have enough indexing for columns.
Wish to update limits by limit. ie., update 1000+ records for every run.