What is best way to batch mutate and avoid node duplication?

OK, the query should be outside or inside of mutate transaction? Do I have to query one by one or maybe I can batch query too? Should I process like this:

-> create Data object 
   -> get distinct [node orignal_id] 
         -> Query for [uid] by each node orignal id 
         -> ...(rest query to get uid if exists)
            -> set [Uid] to orignal [data object]
               -> create a Txn for mutate
                    -> Mutate(obj)
                      -> ...(mutate to insert rest obj)
                        -> Txn.Commit()