Someone should make “EasySoup” or something like that
It is worth remembering that subqueries may affect database performance, slowing it down, as the database stores the result of the inner query as a temporary table. Also, they are more difficult to optimize. The use of subqueries should be therefore limited in high-performance situations, like online systems. Subqueries are often used in data mining projects where performance is not critical.
Image copyright: CH. R. Severance
3. Concurrency and transactions
Databases are designed to accept SQL commands from a variety of sources simultaneously and process them atomically (e.g. with simultaneous commands regarding one row one needs to be completed before completing another one).
Single SQL statements modifying a database such as INSERT INTO, UPDATE, DELETE FROM are atomic.
To implement atomicity a database “locks” specific area (e.g. a row or column) before it starts a SQL command that might change that area.
All other access to that area must wait until the area is unlocked.
The efficiency of a locking mechanism (lock granularity/implementation of transactions) in a database is an important aspect of its overall performance.
Image copyright: Ch. R. Severance
ON CONFLICT clause
Sometimes due to UNIQUE / primary key constraints in a table, we cannot update a row with a value that was already inserted.
An UPDATE clause can be used to limit the update to the “rating” column
Sometimes we want to put more than one statement in one transaction. For exampe when a client selects a product (for example a flight ticket) they usually have some time to provide the necessary details and eventually confirm the payment.
In such cases, the database temporarily locks a specific area - in this case a flight ticket - making it unavailable for other queries/ clients. If the client does not complete the transaction within a given time - usually max few minutes - the database removes the lock and the area can be accessed by other clients. The duration of the lock is dependent upon the type of the transaction - in some cases, it may be just a few seconds.
SQL clauses used with multi-statement transactions include:
BEGIN: indicates the beginning of a multi statement transaction,
ROLLBACK: cancels the transaction, returning to the initial situation.
COMMIT: confirms the transaction / updates the database.