PHP MySQL make insert and update large amount of users faster
Well, the key point would be to wrap all your inserts in a transaction to make them dramatically faster.

Other points of less significance but rather a matter of sanity are:


connect once
prepare once and also make sure you are using a non-emulated prepared statement
get rid of the useless SELECT


So it should be like 

$db = dbConnect();
$query = $db-&gt;prepare(
  "INSERT INTO USERS(id, name, `group`) VALUES(:id, :name, :group)
   ON DUPLICATE KEY UPDATE name=values(name), group=values(`group`)"
);

$userList = getFromAPI();
$db-&gt;beginTransaction();
foreach ($userList as $userId){
    // assuming you have a code to define these variables 
    $query-&gt;execute([
    "id"=&gt;$id,
    "name"=&gt;$name,
    "group"=&gt;$group
      ]);
}
$db-&gt;commit();