From 27952883b617ffbedd81a6bca3bbfa0975b5dcf7 Mon Sep 17 00:00:00 2001 From: monkeyless <52392953+monkeyless@users.noreply.github.com> Date: Fri, 9 Aug 2019 05:38:48 +0200 Subject: [PATCH 1/4] Isert cities in chunks to increase performance --- app/Console/Commands/ImportCities.php | 51 +++++++++++++-------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/app/Console/Commands/ImportCities.php b/app/Console/Commands/ImportCities.php index d61b20813..20c179c41 100644 --- a/app/Console/Commands/ImportCities.php +++ b/app/Console/Commands/ImportCities.php @@ -1,5 +1,4 @@ error('Missing storage/app/cities.json file!'); return; } - - if(Place::count() > 10) { - $this->error('Cities already imported, aborting operation...'); - return; - } + // if (Place::count() > 10) { + // $this->error('Cities already imported, aborting operation...'); + // return; + // } $this->info('Importing city data into database ...'); - $cities = file_get_contents($path); $cities = json_decode($cities); - $count = count($cities); - $this->info("Found {$count} cities to insert ..."); - $bar = $this->output->createProgressBar($count); + $cityCount = count($cities); + $this->info("Found {$cityCount} cities to insert ..."); + $bar = $this->output->createProgressBar($cityCount); $bar->start(); - + $buffer = []; + $count = 0; foreach ($cities as $city) { $country = $city->country == 'XK' ? 'Kosovo' : (new \League\ISO3166\ISO3166)->alpha2($city->country)['name']; - DB::transaction(function () use ($city, $country) { - $place = new Place(); - $place->name = $city->name; - $place->slug = Str::slug($city->name); - $place->country = $country; - $place->lat = $city->lat; - $place->long = $city->lng; - $place->save(); - }); - $bar->advance(); + $buffer[] = ["name" => $city->name, "slug" => Str::slug($city->name), "country" => $country, "lat" => $city->lat, "long" => $city->lng]; + $count++; + if ($count % $this->argument('chunk') == 0) { + $this->insertBuffer($buffer, $count); + $bar->advance(count($buffer)); + $buffer = []; + } } + $this->insertBuffer($buffer, $count); $bar->finish(); + $this->info('Successfully imported ' . $count . ' entries.'); return; } + + private function insertBuffer($buffer, $count) + { + DB::table('places')->insert($buffer); + } } From 983172bc0aaa335049880a1edf5f6177585b09c4 Mon Sep 17 00:00:00 2001 From: monkeyless <52392953+monkeyless@users.noreply.github.com> Date: Fri, 9 Aug 2019 05:39:27 +0200 Subject: [PATCH 2/4] Increase peak memory --- contrib/docker/php.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/docker/php.ini b/contrib/docker/php.ini index 7fc73c567..34afdce98 100644 --- a/contrib/docker/php.ini +++ b/contrib/docker/php.ini @@ -1,5 +1,5 @@ file_uploads = On -memory_limit = 64M +memory_limit = 128M upload_max_filesize = 64M post_max_size = 64M max_execution_time = 600 From 6d771819f471d28fec98395e462b67ba9ec61505 Mon Sep 17 00:00:00 2001 From: monkeyless <52392953+monkeyless@users.noreply.github.com> Date: Fri, 9 Aug 2019 06:01:59 +0200 Subject: [PATCH 3/4] Add checks back --- app/Console/Commands/ImportCities.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/ImportCities.php b/app/Console/Commands/ImportCities.php index 20c179c41..ea72faa65 100644 --- a/app/Console/Commands/ImportCities.php +++ b/app/Console/Commands/ImportCities.php @@ -41,10 +41,10 @@ class ImportCities extends Command $this->error('Missing storage/app/cities.json file!'); return; } - // if (Place::count() > 10) { - // $this->error('Cities already imported, aborting operation...'); - // return; - // } + if (Place::count() > 10) { + $this->error('Cities already imported, aborting operation...'); + return; + } $this->info('Importing city data into database ...'); $cities = file_get_contents($path); $cities = json_decode($cities); From 32a6100fed87df1f3e4a93bf86feca67aacf1393 Mon Sep 17 00:00:00 2001 From: monkeyless <52392953+monkeyless@users.noreply.github.com> Date: Fri, 9 Aug 2019 06:02:44 +0200 Subject: [PATCH 4/4] Add last progressbar advance --- app/Console/Commands/ImportCities.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Console/Commands/ImportCities.php b/app/Console/Commands/ImportCities.php index ea72faa65..429128698 100644 --- a/app/Console/Commands/ImportCities.php +++ b/app/Console/Commands/ImportCities.php @@ -65,6 +65,7 @@ class ImportCities extends Command } } $this->insertBuffer($buffer, $count); + $bar->advance(count($buffer)); $bar->finish(); $this->info('Successfully imported ' . $count . ' entries.'); return;