HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Truncating MySQL table and insert newly records on daily basis

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
insertdailytruncatingbasisrecordsmysqlandtablenewly

Problem

I have my database in which I have to store new data on daily basis fetching from API. The problem is that I have to truncate table everyday and then insert newly fetched records in same table. Records count would be around 10k. It takes 5-10 seconds to insert new records.

So my concern is that if I truncate the table and some users are requesting at a time so he will end up with error because I truncated the table. So can you suggest what should I do to tackle this problem? I am not a DBA so please suggest what should be the better way to do it.

Solution

Instead, do

CREATE TABLE new LIKE real;
load new data into `new` by whatever means
RENAME TABLE real TO old, new TO real;
DROP TABLE old;


Notes:

  • The second step is the only slow step.



  • The RENAME is atomic.



  • You can continue to read the table during the process.



  • Do not use any transactions or table locks.

Code Snippets

CREATE TABLE new LIKE real;
load new data into `new` by whatever means
RENAME TABLE real TO old, new TO real;
DROP TABLE old;

Context

StackExchange Database Administrators Q#262435, answer score: 4

Revisions (0)

No revisions yet.