In line with @fredbe's answer, I would totally agree with using the Salesforce’s Duplicate Detection feature as it automatically detects duplicate records, so you don’t have to manually query the database to check for duplicates yourself.
You can follow these steps for your reference:
Create Matching Rules: Go to Setup → Duplicate Management → Matching Rules, and define fields to match duplicates
Create Duplicate Rules: Setup → Duplicate Management → Duplicate Rules, and set how to handle duplicates (Allow or Block) and enable alerts if needed. -->
Insert a batch of records to test; Salesforce will process valid records and flag or block duplicates based on your settings.
Once you have this set up, you don’t have to worry about duplicate records messing things up during bulk data insertion. It takes the load off, eliminates the need for complex triggers, and just makes life a little easier by handling duplicates in the background. The best part is that the process doesn’t stop for valid records, so you’re always moving forward.