How to to insert large amounts of data with PHP/MySQL?
===

I am currently coding a gambling system for a client, effectivly, the way the system works is as follows:

- Tickets are what we create
- Each ticket will get assigned a certain amount of numbers per ticket
- There are a total of 1,000,000 which are ordered randomly from 000,000 to 999,999
- These 1 million numbers get split into tickets the amount of numbers per ticket is defined by the admin.

Now the problem is at the moment we have an array of 1 million numbers sorted randomly, this is fine, but what would be the best way to insert this into a database?

We have two tables

- tickets | Fields (ID, ticketUID, prizeID, addedDate)

- ticketNumbers | Fields (ID, ticketID, number)

At the moment when the user decides to add the tickets to the prize we loop through and create the total tickets which is determined by the total amount of numbers per ticket, e.g. 1 number per ticket = 1 million tickets, etc...

Now we loop through check the total numbers per ticket add how ever many from our number array then remove it from the original array so there are no duplicate numbers.

Now where the problem stems from is that this takes quite a lot of memory in this way and it causes the page to time out....

Is there another way someone can suggest to help overcome this problem?

P.S. I cannot provide source code, but this is essentially what we are doing, and what i really need is theory of other methods of how i could handle the insertion of all this data.

Thanks in advance