I need to synchronize the data in my database table with the new data from the SQL query, where I need to delete the current records that are not in the new data, and insert new records that are not in the current data. I was able to do this in java using this pseudo code:
// 1) get all data in database and store it in list (currentList) // 2) get new data obtained through sql query and store it in list (newList) // 3) sync both list for(entry : currentList) { if(newList.contains(entry)) finalList.add(entry) } for(entry : newList) { if(!finalList.contains(entry)) finalList.add(entry) } // 4) delete all data from DB // 5) insert finalList data to DB
This works great, however, I think it will have a performance problem when working with a large dataset, because I delete everything and re-insert the whole list instead of just inserting new records and deleting records not found in the new data .
Can you suggest a better way to do this? Is it possible to create an SQL query that can provide data synchronization?
source share