595291_
595291_
Example of a Transaction
Example of a Transaction
sql
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 100
WHERE account_id = 'A';
UPDATE accounts
SET balance = balance + 100
WHERE account_id = 'B';
COMMIT;
If an error occurs after the withdrawal but before the deposit, the transaction
will roll back to maintain data integrity.
Isolation Levels
sql
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
-- Transaction code
COMMIT;
1. Execution Plan:
o SQL queries are broken down into steps and compiled into an
execution plan by the query optimizer.
o An execution plan shows the sequence of operations the
database will perform to retrieve the requested data.
o Viewing the execution plan helps identify inefficient operations
like full table scans and can be accessed using EXPLAIN or
EXPLAIN ANALYZE commands.
sql
Copy code
2. Indexes:
o Indexes are data structures that speed up data retrieval by
providing a fast access path to rows in a table.
o Creating indexes on frequently searched or sorted columns can
greatly improve query performance.
o Common types of indexes include B-tree indexes, hash
indexes, and bitmap indexes.
o Trade-off: While indexes speed up SELECT queries, they may
slow down INSERT, UPDATE, and DELETE operations.
sql
Copy code
sql
Copy code
FROM orders o
sql
Copy code
sql
Copy code
sql
9. Partitioning Tables:
o Partitioning involves splitting large tables into smaller, more
manageable pieces based on a specified column, such as date.
o Helps optimize queries by reducing the number of rows the
database needs to scan.
o Common types of partitioning include range partitioning, list
partitioning, and hash partitioning.
sql
10. Caching:
o Query caching can store the results of frequent queries,
allowing the database to return results faster without re-running
the query.
o Caching should be applied selectively to queries with high
frequency and low variability in results.
1. Analyze Execution Plan: Identify slow steps such as full table scans.
2. Refactor and Simplify Queries: Simplify subqueries, remove
redundant joins, etc.
3. Implement Indexes: Create and test indexes based on analysis.
4. Optimize for Concurrency: Choose appropriate isolation levels and
minimize locking.
1. Query Optimization:
o The first line of tuning involves optimizing queries, as
inefficient SQL is often the main culprit behind slow performance.
o Techniques include indexing, using efficient joins, selecting only
necessary columns, and avoiding complex expressions in WHERE
clauses.
o Execution Plans: Use tools like EXPLAIN or EXPLAIN ANALYZE to
review the execution plan of queries and identify inefficient
operations like full table scans.
o Refactor Queries: Simplify queries, remove redundant joins or
subqueries, and test alternative query structures for better
performance.
2. Index Optimization:
o Indexes are essential for quick data retrieval, but improper
indexing can lead to inefficiency.
o Identify Slow Queries: Check which queries are slow and add
indexes on frequently used columns in WHERE clauses, JOINs,
and ORDER BY clauses.
o Avoid Over-Indexing: Indexes speed up SELECT queries but
can slow down INSERT, UPDATE, and DELETE operations. Use
only the necessary indexes.
o Composite Indexes: When filtering on multiple columns, use
composite indexes to cover the combination of columns being
queried.
sql
Copy code