Unlike updateOrCreate which returns the updated model, updateOrInsert just returns a boolean response. If you want to get the last inserted ID, you can do the following just after your query:
The only accurate way to get the id of the inserted or updated record is to requery it using the same conditions that you are using for the where portion of the query (first array).
@cronix reply seems to be the most sensible to me if you want to use the updateOrInsert method
Of course you could always use the updateOrCreate method instead, which returns the updated/created model. If you are using an auto-increment ID in your database table and you are not specifying the ID in the data array, then I don't think there's any reason to use updateOrInsert
Now, using this $row_id I will find the row on table 2. If exists update else create just as in above code. This is taking too much time. To insert around 200,000 it is taking around 2 days. Now, what is the best way to deal with this? This is currently residing inside the job.
Of course it will be slower. It's performing an extra query for everything. If there were 2000 queries before, now there's 4000. I didn't say it's the best way, only the way that will do what you want - get the id whether it's an insert OR update.
If your email and name fields are indexed, it shouldn't really take that long though. Those need to be indexed because that's what you're using in your where portion of the query. It takes longer as more data gets inserted because there aren't any indexes for it to use, so it has to scan the entire table every time to find that record to see if it exists. I'm betting they're not indexed.
@cronix Actually, I already have an index of (id, name, city, state, zip). The code in question is just pasted from laravel documentation itself. I need to check if there is already a row with the same name, city, state, zip.