"Efficiently storing huge amounts of data in MySQL in just 5

Interested in my tech stack: express + typeorm + mysql

Seeking a solution for the following task: I have a csv file with over 100000 rows, where each row contains data such as:

reviewer, review, email, rating, employee, employee_position, employee_unique_id, company, company_description
I aim to efficiently store all this data in 5 distinct tables

@Entity('user')
export class UserEntity {
  @PrimaryGeneratedColumn('uuid')
  id: number

  @Column()
  name: string

  @Column()
  email: string

  @OneToMany(() => ReviewEntity, review => review.fromUser)
  reviews: ReviewEntity[]
}
@Entity('review')
export class ReviewEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column('longtext')
  text: string

  @Column()
  rating: number

  @ManyToOne(() => UserEntity, user => user.reviews)
  fromUser: UserEntity

  @ManyToOne(() => EmployeeEntity, employee => employee.reviews)
  forEmployee: EmployeeEntity
}
@Entity('employee')
export class EmployeeEntity {
  @PrimaryColumn()
  id: string

  @Column()
  name: string

  @ManyToOne(() => EmployeePositionEntity, position => position.employees)
  position: EmployeePositionEntity

  @ManyToOne(() => CompanyEntity, company => company.employees)
  company: CompanyEntity

  @OneToMany(() => ReviewEntity, review => review.forEmployee)
  reviews: ReviewEntity[]
}
@Entity('employee_position')
export class EmployeePositionEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  name: string

  @OneToMany(() => EmployeeEntity, employee => employee.position)
  employees: EmployeeEntity[]
}
@Entity('company')
export class CompanyEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  name: string

  @Column('longtext')
  description: string

  @OneToMany(() => EmployeeEntity, employee => employee.company)
  employees: EmployeeEntity[]
}

I attempted to parse the data into 5 arrays and save them concurrently in batches of 100 items each, but the process was too time-consuming

Answer №1

Efficient Data Loading Strategy

  1. LOAD DATA INFILE -- Import all data into a temporary table (raw) with all columns.
  2. CREATE TABLE table1 SELECT ... FROM raw
    ;
  3. Repeat the above step for each additional table.
  4. DROP TABLE raw

Instead of attempting to use the incoming data directly, treat it as a staging area for creating the final tables.

If normalization of the data is required, handling autoincrement ids efficiently can be challenging. This may require a method like this (conducting the process per id): Normalization .

This approach could involve multiple full table scans. However, performing each task in a single query will be significantly faster than row-by-row processing.

Furthermore, it is advisable to write the queries directly in MySQL rather than relying on a less-efficient SQL-generating framework.

Data Replacement? If the objective is to replace all data (and the data is read-only), follow these 3 steps:

  1. CREATE TABLEs with temporary names

  2. Import the data into these tables (as mentioned earlier)

  3. Swap in the new data swiftly (in under 5 seconds).

     RENAME TABLE main1 TO old1,
                  new1 TO main1,
                  main2 TO old2,
                  new2 TO main2
                  ...;
     DROP TABLE old1, old2, ...;
    

This process minimizes downtime to less than a second.

(The strategy discussed is tailored for MySQL; other listed technologies may require different approaches.)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieving dynamic population of a second dropdown menu based on selection from the first dropdown menu using PHP

In my current project, I am faced with the task of populating three different menus. The first menu is generated using a MySQL query in PHP and displays TV shows like "Modern Family" or "Dexter". What I want to achieve is that once a TV show is selected fr ...

Screening new elements to be included in the array

When adding new items to an array in my App, I encountered a problem with filtering the newly added items. If I use .filter by state, it modifies the original array and I am unable to filter from the beginning (original array). I attempted to filter using ...

What is the best way to continuously compare two date variables every minute using Javascript?

In my script, I have two date variables - one representing the current time and the other two minutes later. My goal is to compare both values every minute and trigger a function when the current time is greater than or equal to the latter time. Unfortun ...

Is there a way to dynamically alter the background style of a div by clicking on it multiple times using Javascript?

I am attempting to create a calendar where each day changes its background color between blue and green when clicked, using JavaScript and CSS. It should function similar to a toggle feature. The default color is blue, and I have successfully made the days ...

Customizing text inputs in three.js

Why are the HTML textboxes disabled for input when the "render" button is working properly? I have tried changing the render.domElement to different elements on the page such as container, info, parent, and scene, but I cannot figure out the reason for t ...

The functionality of a generated button has been compromised

My goal is to create a webshop that doesn't rely on MySQL or any database, but instead reads items from JSON and stores them in LocalStorage. However, I've encountered an issue where the functionality of buttons gets lost after using AJAX to gene ...

The absence of AudioPlayer may be responsible for the compilation failure on Vercel

Here is the latest code snippet import { useState, useEffect, useRef } from "react"; import { FaPlay, FaPause, FaForward, FaBackward } from "react-icons/fa"; export default function Player() { const [isPlaying, setIsPlaying] = useState(false); const [ ...

Assign the value from JavaScript to the element with the "ng required" attribute, without resetting frm.$error.required to false

My situation involves using a button with ng-style that relies on frm.mybtn.$error.required as follows: ng-style="frm.mybtn.$error.required?{'border-color':'#a94442'}:''" I am updating the value of the button from JavaScript ...

Encountering the error message 'db.get is not a function' in Node.js while working with MongoDB

Developing a voting application that involves creating two models: users and polls. The database will have two collections - one for users and one for polls. User.js 'use strict'; var mongoose = require('mongoose'); var Schema = mong ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...

Displaying Product Attribute and Category Names in Woocommerce Title

After reading the answer provided in this thread (Woocommerce: How to show Product Attribute name on title when in a category page and "filtering" products via '?pa_attribute=' on address bar), I am interested in displaying both the categ ...

What is the method for entering text into a span element and submitting it by hitting the "ENTER" key using Selenium in Python 3.7?

Environment: Using Python 3.7 and Selenium 3.141 Issue : I need to automate commenting on posts using Selenium in a web page. The challenge is that the comment form does not have a traditional "Submit" button; instead, it's a SPAN element where yo ...

I was confused about the distinction between the https.get() and https.request() functions in the Node.js npm package for https

// # Exciting Nodejs Programs! const https = require('https'); https.get('https://www.google.com/', (res) => { console.log('statusCode:', res.statusCode); console.log('headers:', res.headers); res.on ...

The efficiency of React Context API's setters is remarkably sluggish

I have a goal to implement a functionality where the background gradient of a page changes depending on whether the child's sublinks are expanded or collapsed. To achieve this, I am using the useContext hook. However, I've noticed that although e ...

Using RxJs in an Angular 2 application to enable row selection in a table by detecting mouse movements

Check out this example of an Angular 2 application with row selection in a table: https://plnkr.co/edit/HdQnWqbg9HloWb4eYGHz. The row selection functionality is implemented using mouse event handlers (mousedown, mousemove, mouseup). Below is the template ...

WebPack Error: When calling __webpack_modules__[moduleId], a TypeError occurs, indicating that it is not a function during development. In production, an Invalid hook call error

Encountering a WebPack error when utilizing my custom library hosted as a package and streamed with NPM Link. Interestingly, the production version functions flawlessly. Below are my scripts: "scripts": { "dev": "rm -rf build ...

When using TypeScript, default imports can only be done with the 'esModuleInterop' flag enabled

Below is my current code snippet in index.ts: import { User } from "./datatypes" import express from 'express'; console.log("hello world") Displayed is the content of my package.json: { "name": "simple_app& ...

Transforming text colors dynamically using Vue.js

Here is an Angular code snippet: <div [style.color]="'#' + prod.id.substring(0,6)"> <small>{{ prod.id }}</small> </div> Now I want to create a similar code using vue.js. ...

Utilizing ID's within a jade template embedded within a P HTML element

Using the jade template, I successfully formed this paragraph. My next step is to add an ID or a class around the word stackoverflow. How can I achieve this in jade? I am aware that in regular HTML we would use something like <div class="className"> ...

"Error: Vue prop is not defined when passed to computed functions during initial call

I am encountering an issue with my Vue component. Here is the code for reference: Vue.component('result', { props: ['stuff'], data: () => ({}), template: "<img :src='tag' class='result'></img>", ...