When you have eg. 3 queries in a row that you submit to a SQL database as a single string, separated by semicolons, and another user does that exact same thing at the same time, will our queries be seen as a “group” or “queue” of queries which will always execute in the same, logical order per user, or can the queue for these queries get “mixed up”, ending up with both our queries being fired one by one?
So basically I want to know which one of the following situations both MySQL and SQL Servers will behave like when this occurs:
-
execution path situation 1:
-
user 1 query 1, user 1 query 2, user 1 query 3, user 2 query 1, user 2 query 2, user 2 query 3
-
execution path situation 2:
- user 1 query 1, user 2 query 1, user 1 query 2, user 2 query 2, user 1 query 3, user 2 query 3
This depends on whether your database sees this as a number of separate queries or as a single transaction. If it sees it as a transaction, the order won’t matter. If you want to ensure it sees it as a single transaction, there are ways to signal to the database that it should create a transaction for your queries. The database (should) ensure four basic properties (ACID):
Isolation is the one relevant to your case. Your queries should not be able to sabotage another person’s, so you should not need to worry about other users submitting queries at the same time.
Also, I doubt that your database is running off a single processor. It’s likely that the linear paths you described wouldn’t happen simply because the database can handle multiple queries simultaneously.
For safety’s sake, I’d start your query with a
BEGIN TRANSACTIONand end it withCOMMIT TRANSACTION(or whatever syntax your database requires). If the database does see it as a number of separate queries, you could have problems if they do any writing to the database. Best to use a transaction.