F
F
fman22015-12-09 16:21:38
MySQL
fman2, 2015-12-09 16:21:38

How to speed up MySQL UPDATE by 100k rows?

Good afternoon!
There is a table with the goods on 100 records.
You need to select all the records from it, make an extra charge (PHP f-i does this), and then do an UPDATE of this record.
Such a request

$this->products = (new Query())
            ->select(['p.id', 'p.price'])
            ->from(['products p'])
            ->all();

Like:
$this->products = (new Query())
            ->select(['p.id', 'p.price'])
            ->from(['products p']);
        
        foreach($this->products->batch(1000) as $product) {
            //....
        }

It is executed extremely slowly, apparently due to entering data into the array. About the line-by-line update, I generally keep quiet.
But how to do it differently? If you cannot work with these MySQL tools. id is set to AUTO_INCREMENT PRIMARY KEY

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Burov, 2015-12-09
@BuriK666

Do update not by one record, but by batches using transactions. like 1000 jokes.
It will be noticeably faster.

O
Optimus, 2015-12-09
Pyan @marrk2

Get rid of ActiveRecord rewrite the query in low-level mysqli or PDO and it will run for you in a few seconds.
PS Or at least Yii DAO or Yii Query Builder

S
Stalker_RED, 2015-12-09
@Stalker_RED

Is it not an option to mark up directly in the database?
update product set price = price * 1.3

D
Dmitry Belyaev, 2015-12-09
@bingo347

Do not receive data from the database, but calculate your margin using the database itself.
If you show the code of the function that calculates the markup, then I will help you make a request for UPDATE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question