Massive improvement of database creation time (63s -> 7s)

This commit is contained in:
2024-11-07 19:18:50 +01:00
parent 47e045dde3
commit d10f84750c
3 changed files with 56 additions and 26 deletions

View File

@@ -24,6 +24,11 @@ export class Order extends Model {
@Column @Column
paymentId: number paymentId: number
@Default(false)
@Column
shipped: boolean
// Relations // Relations

View File

@@ -25,6 +25,7 @@ import citiesLocations from "./../data/cities-locations.json"
import exercises from "./../data/exercises.json" import exercises from "./../data/exercises.json"
import bandsConcerts from "./../data/bands-concerts.json" import bandsConcerts from "./../data/bands-concerts.json"
import { Op } from 'sequelize' import { Op } from 'sequelize'
import moment from 'moment'
/** /**
@@ -76,10 +77,15 @@ export async function prepopulateExerciseDatabase() {
export async function prepopulateDatabase() { export async function prepopulateDatabase() {
deleteAllTables() deleteAllTables()
let start = moment()
////////// Locations and Seat Tables ////////// ////////// Locations and Seat Tables //////////
// Buffer seats, write them in one bulk action to the DB
let seats = []
for (let city of citiesLocations.cities) for (let city of citiesLocations.cities)
{ {
await City.create(city) await City.create(city)
@@ -94,9 +100,9 @@ export async function prepopulateDatabase() {
await Location.create(location) await Location.create(location)
.then(async locationDataset => { .then(async locationDataset => {
let capacity = 0 let capacity = 0
let seatGroups = []
for (let seatGroup of location.seatGroups) for (let seatGroup of location.seatGroups) {
{
seatGroup["locationId"] = locationDataset.id seatGroup["locationId"] = locationDataset.id
let surcharge = 0 let surcharge = 0
@@ -121,46 +127,58 @@ export async function prepopulateDatabase() {
seatGroup["surcharge"] = surcharge seatGroup["surcharge"] = surcharge
await SeatGroup.create(seatGroup) seatGroups.push(seatGroup)
.then(async seatGroupRes => { }
if (seatGroup.standingArea) {
await SeatGroup.bulkCreate(seatGroups)
.then(async seatGroupsRes => {
for (let seatGroup of seatGroupsRes) {
if (seatGroup.dataValues.standingArea) {
// In an area without seats, create one row with all "seats" // In an area without seats, create one row with all "seats"
await SeatRow.create({ await SeatRow.create({
row: 0, row: 0,
seatGroupId: seatGroupRes.id seatGroupId: seatGroup.dataValues.id
}) })
.then(async seatRowRes => { .then(async seatRowRes => {
for (let i = 0; i < seatGroup.capacity; i++) { for (let i = 0; i < seatGroup.dataValues.capacity; i++) {
await Seat.create({ seats.push({
seatNr: i + 1, seatNr: i + 1,
seatRowId: seatRowRes.id seatRowId: seatRowRes.id
}) })
capacity++ capacity++
} }
await Seat.bulkCreate(seats)
}) })
} }
else else
{ {
let seatRows = []
for (let row = 0; row < location.rows; row++) { for (let row = 0; row < location.rows; row++) {
await SeatRow.create({ seatRows.push({
row: row + 1, row: row + 1,
seatGroupId: seatGroupRes.id SeatGroupId: seatGroup.dataValues.id
}) })
.then(async seatRowRes => { }
await SeatRow.bulkCreate(seatRows)
.then(seatRowRes => {
for (let seatRow of seatRowRes) {
for (let col = 0; col < seatGroup.capacity / location.rows; col++) { for (let col = 0; col < seatGroup.capacity / location.rows; col++) {
await Seat.create({ seats.push({
seatNr: col, seatNr: col,
seatRowId: seatRowRes.id seatRowId: seatRow.id
}) })
capacity++ capacity++
} }
}
}) })
} }
} }
}) })
}
// Update capacity of location // Update capacity of location
await Location.update( await Location.update(
@@ -176,6 +194,8 @@ export async function prepopulateDatabase() {
}) })
} }
// Create seats to the database as bulk for better performance
await Seat.bulkCreate(seats)
////////// Account Tables ////////// ////////// Account Tables //////////
@@ -355,4 +375,6 @@ export async function prepopulateDatabase() {
} }
}) })
} }
console.log(moment().diff(start))
} }

View File

@@ -1,5 +1,8 @@
export class OrderModel { export class OrderModel {
id: number id: number
shippingProgress: number accountId: number
orderedAt: string orderedAt: string
addressId: number
paymentId: number
shipped: boolean
} }