Use intermediate flush to populate the DB

The previous code was very long with MYSQL