Search code examples
mysqlmariadbinnodb

Can I INSERT into table while UPDATING multiple different rows with MariaDB or MySQL?


I am creating a custom analytics system and currently in the database designing process. I'm planning to use MariaDB with the InnoDB engine to be able to handle big loads.

The data I'm expecting could be around 500k clicks/day. I will need to insert these rows into the database, which means that I'll have around 5.8 inserts/sec on average. However, at the same time, I want to record if someone visited a page associated with that click. (basically to record funnels)

So what I'm planning to do is to create additional columns and search for the ID of the specific row then update that column with the exact time of the visit.

My first question: is this generally a recommended approach to design the database like that? If not, how else is it worth to design the database?

My only concern is that while updating rows the Table will be locked, and can't do inserts, therefore slowing down the user experience.

My second question: is this something I should worry about, that the table gets locked while updating, and thus slowing down inserts? Does it hurt performance?


Solution

  • InnoDB doesn't lock the table for insert if you're performing the update. Your users won't experience any weird hanging.

    It's an MVCC compliant engine, designed to handle concurrent access to underlying tables.

    You can control the engine's behavior by choosing an appropriate isolation level, however the default (REPEATABLE READ) is excellent and does the job more than well.

    If a table is being modified by multiple users (not users that connect to your site but connections established towards MySQL via a scripting language or some other service) and there's many inserts/updates/deletes - MySQL can throw an error saying a deadlock occurred.

    A deadlock is a warning, not an error, that more than 1 thread tried to access an occupied resource (such as two threads tried to update the same row at the same time, but only 1 will be allowed to do so). It's an indication you should repeat the query.

    I'm suggesting that you take care of all possible scenarios in the language of your choice when it comes to handling MySQL that's under heavier I/O.

    ~6 inserts a second isn't a lot, make sure you're allowing MySQL to access sufficient system resources. For InnoDB, check the value of innodb_buffer_pool_size or google a bit to see what it is and how to use it to make your database run fast.

    Good luck!