Cron Job Alternative

I am doing a service with an automated function to add a record in the database. However, what I worry most is the performance of doing the task, considering the the server capabilities.

What I plan to do is just a simple Cron Job. Accessing simply by

myhost/service/whatever?key=whateverkey

and within that function is this simple query

function whatever() {
    foreach(getThoseData() as $data) {
       AddDataQuery("Whatever data");
       LoggingFunc("whatever logs");
   }
}

What I worry is how it execute the function, considering it has getThoseData() returns 1million rows. In that case, it loops at 1million times.

Is there any way to ease the execution?