Optimize MySQL insertion - insert multiple records with condition (per record) although not new to SQL, up until now I used the basic stuff - basic crud, joining tables etc. I have a special case now where I'm expecting a lot more insertions that goes something like this (pseudocode):
foreach(id_of_student in students){
    {class} = students[id_of_student].class;
    {grade} = students[id_of_student].grade;
    INSERT INTO grades (id_of_student,class,grade) VALUES({id_of_student},{class},{grade})
    WHERE grade.notSetManually=1 ON DUPLICATE KEY UPDATE
}


Of course I would like to not run it as a loop and access the db 50 times for each grade sheet (my app checks up to 50 grade sheets per minute). Can this be done in one big query, in a more optimized way? I saw that there is LOOP in mysql, but is it just syntactic sugar when it comes to the burden on the DB?

Another thing - this ON DUPLICATE KEY UPDATE means that as long as my key is {id_of_student},{class}, this will update and not add new line, right?
IODKU works well with SELECT:
INSERT INTO t
    (unique_key, counter, time)
    ON DUPLICATE KEY UPDATE
        counter = VALUES(counter) + 1,
        time = NOW()
  SELECT unique_key, 1, NOW()
      FROM ...

Notes:

Need unique key (or set of columns)
See how a counter can be updated