I got a hash map in java like this
{(1,'2018-06-29 10:19:33'),(4,'2018-06-29 10:19:34'),(10,'2018-06-29 10:19:38'),....}
The length of map could go as high as 3000
and a mysql table
id name updated
1, firstProduct, 2018-06-29 09:19:33
2, secondproduct, 2014-06-29 10:19:33
4, fourthproduct, 2016-06-29 09:19:33
10, tenthproduct, 2018-06-29 06:13:32
.......
the key in the map is a id field in the table and the value is the updated timestamp field.
I would like to get all the products in the table whose timestamp is greater that value in the map.
like
select * from products where id = 1 and updated > '2018-06-29 10:19:33'
select * from products where id = 4 and updated > '2018-06-29 10:19:34'
...
But there could be as many as 3000 entries in the map.
I am thinking of passing the map values to mysql stored procedure. Inside the procedure a while loop will execute select statement for each map entry into a result set and finally return the result set back to java program.
Would like to know if this is possible and i feel there is a better solution for this but cant figure out.
I would dynamically generate a statement in Java.
Either:
SELECT p.id, p.name, p.update
FROM products p
WHERE 1=0
OR ( p.id = ? AND updated > ? )
OR ( p.id = ? AND updated > ? )
OR ( p.id = ? AND updated > ? )
(MySQL likely won't make effective use of an index for that query.)
Or, we could do :
SELECT p.id, p.name, p.update FROM products p WHERE p.id = ? AND updated > ?
UNION ALL
SELECT p.id, p.name, p.update FROM products p WHERE p.id = ? AND updated > ?
UNION ALL
SELECT p.id, p.name, p.update FROM products p WHERE p.id = ? AND updated > ?
With an appropriate index defined, each SELECT could make effective use of that. But with this approach, we are probably going to hit an upper limit on the number of table references in a single query. So this approach may need to be broken up into chunks.
Personally, I wouldn't hide the complexity in a database procedure.
It's not clear what benefit we would gain by implementing a database procedure.