I'm currently working on wordle-like project and wanted to populate database with words.
Found a list of ~15k of words using regex. I've made an array [word(1), word(2), ..., word(n)]
from which I looped through each of these words and added them into my database.
preg_match_all('/\w{5}/', $words, $matches);
foreach($matches[0] as $m) {
$word = new Words();
$word->setName($m);
$entityManagerInterface->persist($word);
$entityManagerInterface->flush();
}
But it took over 30 sec and symfony
stopped it because it thought it's infinite loop which is not.
I found the solution using set_time_limit(0);
but after like 2 mins it stopped and I got an error about using too much memory.
I thought about splitting $words
array into smaller and add them one by one.
So here's my question,
- Is there any better way to add those words faster than this?
When it failed to add those words into my database I tried to delete all records using:
$query = $entityManagerInterface->createQuery('DELETE FROM App\Entity\Words e')
->execute();
It works fine but when I add words after execution of the last line, primary key
doesn't start from "1" but it continues from last primary key.
Just curious,
- Is it possible to make it start again from
primary key
"1".