I am selecting around 10 million rows which are invalid from table grid into ##grid_temp and deleting their related info from grid_info table. I am running this in a python script. It is taking more than 6 hours. How can I optimize here?
query = """ select grid_id from grid into ##grid_temp
where
....some logic.....
set rowcount 10000
while 1 = 1
begin delete from grid_info where grid_id in (select grid_id from ##grid_temp)
if @@rowcount = 0
break
end
set rowcount 0
"""
db_conn.execute_query(query);
select * ... into
if you only need thegrid_id
?