R
R
Roman2016-01-29 18:48:13
PHP
Roman, 2016-01-29 18:48:13

How to make an event accumulator and reset it with one query to a table?

There is such a case, on the page in the controller action, several events occur, each of which is then written to the log table via INSERT SQL. Well, let's say a method, Log::add($type, $message) which executes INSERT SQL to insert one row into the table.
Problem: There are a lot of events, constantly logging, insert queries are performed, the table is constantly growing, which slows down the site.
My task is to optimize this process, the first thing that comes to mind is a multiple INSERT for inserting several records at once, for this you need to implement a row accumulator and then somehow dump everything at once into a table.
The question is how to correctly implement this and where in the PHP script to hang the reset of accumulated events? Moreover, in order not to lose a single one in the event of an abnormal completion of the script or if exit, some kind of accumulation mechanism is needed and then recording all events in the log table, which will reduce the number of INSERTs.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Kalashnikov, 2016-01-29
@zo0m

As an option, replace your Muscle with Monga

E
entermix, 2016-01-29
@entermix

You can write to a file, and then cron process it

D
dimabdc, 2016-01-29
@dimabdc

Use register_shutdown_function
php.net/manual/ru/function.register-shutdown-funct...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question