8
I have a process that I need to implement on my system that, at a given time, will check a relatively large number of data and, if not found, should save in the Mysql database. This should be recorded on the record.
I’m not finding a way to do that without overloading the server. I am developing in PHP and until now the only way I thought was to make a loop reading each "line", checking if it exists, if it does not exist, write, recover the ID and write to another table. If it already exists, I only recover the ID and saved in another table.
This way you would have to make 1 query + 1 write + 1 read (recover the newly saved ID) + 1 write for each record. If we think that it will be common for each operation to do this on average 3000 times, it becomes unviable. In addition it will be common to also have more than one user doing this same process at the same time.
What would be the most correct way to proceed in this case?
[Additional information]
It is a product handling system. Each product has a "serial". Then I need to check each serial in table "A" and if it doesn’t exist, I register, I take the ID and I throw in table "B". If the serial is already registered in table "A", I just throw its serial in table "B".
Have you considered the possibility of using Events, Procedures and Triggers so you avoid this redundancy, leaving the whole rule in the database and away from PHP!
– Williams
I believe there may be an optimization for your system. If it checks if there is a certain ID in a table, it is because that ID has to be in another table, otherwise it would not know what to check. So, why not register the new Ids that appear also in the table where it is necessary to check?!
– dm707
@Diegomachado, this is exactly the process: I check if the ID exists in table "A", if it exists take the ID (from table "A") and insert in table "B". If it does not exist, insert the record in table "A", retrieve the ID and insert it in table "B". These are product movement contents. Each product has a serial. So I need to check serial to serial. If the serial is already registered (table A), I take the ID and loop in table B. And so on, as already explained.
– Danilo Miguel
I believe that you do not understand what I said, what I am trying to do is try to skip this verification process, but if it is not possible ok, we will see the other way. I wanted to say the following: Why add the records in only one table and then have to check everything to see if it exists in the other one too? I do not know how the system was made but I believe that it would be easier if the records were already added in the two tables at once, because since the verification is done automatically then there would be no problem in registering there once, understood what I meant.
– dm707
@I don’t think I really understand you. The serial, which must be unique, will be written to only one table and its ID that must be moved. The system already exists today and works the way (I think) I understood its explanation: I write the serial in more than one table. I am redesigning the system to avoid this redundancy. I added a figure to the statement to illustrate what I need. In fact, with all that, I just want to know one way to avoid a line query in a loop that can have thousands of repetitions.
– Danilo Miguel
I still can’t get a practical idea of how to solve this problem. Can anyone give me a direction?
– Danilo Miguel
The term you seek will not be
Transações
?– Edilson
I think not, because I need to pass values from PHP to the database. A loop in the data to do the search. However, do you have any transaction suggestions that apply in this case?
– Danilo Miguel
As @Joker mentioned, the
Events
andTriggers
They serve exactly this, working with heavy and repetitive processes. In the case of 3000 queries for each record is really heavy, you should then find a way to process the data before and only then enter.– Edilson