I am no DBA, but I got curious when I noticed sluggish write performance on a mysql database, and I remembered somewhere that you should always use batch writes over sequential writes. So I decided to test it out, using a python script and a mysql server.
What will we be doing
I wrote a python script that writes 100,000 records to a database and keeps time of how long the writes took, 2 examples which I will compare:
- One script writing each record to the database
- One script writing all the records as batch
Sequential Writes
It took 48 seconds to write 100,000 records into a database using sequential writes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Running that shows us this:
1 2 3 4 5 |
|
Batch Writes
It took 3 seconds to write to write 100,000 records using batch writes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Running that shows us this:
1 2 3 4 5 |
|
Looking at the Scripts
The script used for sequential writes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
The script used for the batch writes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
Thanks
Thanks for reading, so this was kind of interesting to see to never do sequential writes but write them in bulk when you have large amount of writes.