Start with this Moodle Doc... it will give you some ideas on how to do what you want to do....Millions of users? in Oz??????
Create a csv file with the appropriate fields and names. The first example on how to bulk create cohorts given is not that clear but it gets the idea over... Something like this to create the cohorts in categories
where add students, just get a list of students you want, and create another csv file to add them like this:
username,password,firstname,lastname,email,cohort1
tomjones,Pass1234*,Tom,Jones,tomjones@example.com,Yr 8 Students
marysmith,Pass1234*,Mary,Smith,marysmith@example.com,Yr 9 Students