Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mvnobrega's avatar

Slow database change

I'm running the following code to make a fix in mysql database:

public function correcaop(Request $request)
    {
        
        $palavras = palavras_br::whereBetween('id', [1, 150000])->get();
        

        foreach($palavras as $p){
            
            $busca = nvi::where('palavra', 'RLIKE','[[:<:]]'.$p->palavras.'[[:>:]]')->count();

            $update = palavras_br::where('id', $p->id)->first()->update(['total_versiculos' => $busca]);
            
            
            
        }
        
    }

I'm running the following code to make a fix in mysql database:

the "palavras_br" table contains 320k lines and the nvi table 32k lines. I need to take each word of the table "palavras_br" and count in how many rows of the table "nvi" it exists.

Everything is working, except for the delay. In 3 hours, it only corrected 30k lines. As it is, it takes almost 1 full day to finish. It seems extremely slow to me as my VPS server has 4gb of ram and ssd storage.

Does this slowness make sense or is there something I can do to make this adjustment to the tables faster?

0 likes
5 replies
vincent15000's avatar

Is the palavra field indexed in the database ? If not, you could index this field, the query should be faster.

I also have a question ;) ... I never used [[:<:]]. What's that ?

1 like
mvnobrega's avatar

@vincent15000

I do not know if I understand.

But "palavras_br" the structure is this: https://prnt.sc/L1K4ZTTREq2h

And "nvi" is this: https://prnt.sc/mjNP-CZ0gi-r

The "palavra" field has an index key. But the name of the index is different, see: https://prnt.sc/5NHR1dnhjSgI

It's like "ver_texto" . Does this make any difference to the way I'm running the code?

About this regex [[:<:]] I don't know how to explain how it works. I found this solution on a forum and it worked the most for me. But I can't tell you the meaning

1 like
vincent15000's avatar

@mvnobrega I don't have sufficient knowledge about databases to help you further about the speed. Sorry ...

Please or to participate in this conversation.