Reputation: 6736
I am currently coding a gambling system for a client, effectivly, the way the system works is as follows:
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
ticketNumbers
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
Upvotes: 2
Views: 2398
Reputation: 545
I can't think of a best solution here at the moment without trying stuff - do you actually have to save the numbers in the first place in a database? If its plain numbers from 000000 to 999999 then you can just generate them on the fly, that could be more efficient.
In order to generally overcome the timeout issue you could always increase the default php timeout or disable completely for this specific task:
//set php script timeout, 0 to disable
set_time_limit(0);
// your time consuming code
//don't forget to reset to 30 seconds.
set_time_limit(30);
Upvotes: 1
Reputation: 2349
I'm not really into gambling, so not really sure in which way these tickets have anything to do with gambling.
That said, if you really need to create such an amount of data, there are a few options. You could for example move the ticket-creating away from the user. What I mean with that is whenever the user decides to 'add the tickets to the prize' you delegate this task to another process/thread and let the user move on. Then when the creation of the tickets is done, the process notifies the user somehow.
Upvotes: 1