Database.Advanced.How would you design a high-availability database system?

Designing a high-availability (HA) database system means ensuring the database remains operational with minimal downtime, even in the face of hardware failures, software bugs, or network issues. Here’s how you can approach the design, step by step:

✅ 1. Set Requirements First

Before jumping to tech choices:

  • RTO (Recovery Time Objective): How quickly must it recover?
  • RPO (Recovery Point Objective): How much data loss is acceptable?
  • Read/Write patterns: OLTP vs OLAP, heavy reads vs writes?
  • Geographic needs: Multi-region or single region?
  • Budget & team expertise

🧱 2. Core Design Components

🔁 a) Replication

Maintain multiple copies of your data.

  • Primary-Replica (Master-Slave):
    • One primary node handles writes, replicas handle reads.
    • Easy to set up (e.g., PostgreSQL Streaming Replication, MySQL Replication).
  • Multi-Master (Active-Active):
    • All nodes accept writes.
    • Complex conflict resolution (e.g., Cassandra, Couchbase).

⚖️ b) Failover Mechanism

Automatically detect failure and switch to a backup.

  • Use orchestration tools (e.g., Patroni for PostgreSQL).
  • Use floating IP or DNS switching.
  • Integrate with a load balancer or service mesh (e.g., HAProxy, Envoy).

📡 c) Load Balancing

Distribute traffic to healthy nodes.

  • Read requests can go to replicas.
  • Use read-write splitting at application or proxy level.
  • Tools: HAProxy, PgBouncer, ProxySQL.

📦 d) Backups & Snapshots

Ensure point-in-time recovery.

  • Automate regular backups (full + incremental).
  • Store in different regions (e.g., S3, GCS).
  • Test restoration procedures.

🌍 3. Geo-Redundancy

For true fault tolerance:

  • Use multi-region replication.
  • Ensure latency-aware routing.
  • Use cloud-native solutions (e.g., AWS Aurora Global, Google Spanner).

🧠 4. Monitoring & Alerting

  • Use tools like Prometheus, Grafana, or cloud-native (CloudWatch, Stackdriver).
  • Alert on replication lag, node downtime, disk I/O, CPU, etc.
  • Auto-restart unhealthy nodes (via Kubernetes, systemd, or EC2 ASG).

☁️ 5. Cloud-Managed Alternatives

If you want HA without managing the complexity:

  • AWS RDS/Aurora, Google Cloud SQL/Spanner, Azure SQL offer:
    • Built-in failover
    • Replication
    • Backups
    • Monitoring

🔒 6. Security Considerations

  • Encrypt data at rest and in transit.
  • Use IAM roles, secrets managers.
  • Audit access and logs.

🛠️ Example Architecture: PostgreSQL HA Setup

               ┌────────────┐
               │ LoadBalancer│
               └────┬───────┘
                    │
          ┌─────────▼─────────┐
          │   PgBouncer (RW)  │
          └────┬───────┬──────┘
               │       │
      ┌────────▼──┐ ┌──▼────────┐
      │ Primary DB│ │ Read Replica│
      └────┬──────┘ └────┬──────┘
           │             │
      ┌────▼─────┐ ┌─────▼─────┐
      │ WAL Logs │ │ Monitoring│
      └──────────┘ └───────────┘

🧪 7. Test Everything

  • Simulate failure: kill nodes, disconnect network.
  • Check failover time and data consistency.
  • Practice disaster recovery drills.
Posted in Без рубрики | Comments Off on Database.Advanced.How would you design a high-availability database system?

Database.Middle.What is query execution plan (EXPLAIN)?

Understanding the query execution plan (via EXPLAIN) is key to becoming a query performance master. Let’s break it down step by step.


🧠 What Is a Query Execution Plan?

A query execution plan is a step-by-step blueprint of how the database will execute (or has executed) your SQL query.

You can ask the database to show you its plan using the EXPLAIN command.


🔍 Why Use EXPLAIN?

  • To understand how your query behaves
  • To find performance problems
  • To see if indexes are used
  • To check row estimates vs actual results

✅ Basic Example

EXPLAIN SELECT * FROM users WHERE email = 'stanley@example.com';

This will show:

  • Whether the query does a sequential scan (bad for big tables)
  • Or an index scan (faster!)
  • How many rows are expected
  • The cost of the operation (a unitless estimate)

🧠 Key Terms You’ll See in EXPLAIN

TermMeaning
Seq ScanFull table scan (slow for large tables)
Index ScanUses an index to locate matching rows
Index Only ScanUses only index — doesn’t touch table (very fast)
Nested LoopJoins one row at a time (fast for small sets)
Hash JoinJoins using a hash table (good for large joins)
CostEstimated cost to run the step (planner’s metric)
RowsEstimated number of rows at that step
WidthAverage size of each row in bytes

✅ More Powerful: EXPLAIN ANALYZE

EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'stanley@example.com';
  • Executes the query
  • Shows actual time taken
  • Shows actual row counts
  • Helps you see misestimates or performance issues

🧠 Example Output (Simplified)

Seq Scan on users  (cost=0.00..35.50 rows=1 width=48)
  Filter: (email = 'stanley@example.com')

🟥 This means:

  • The database is scanning the whole table to find the email — it’s not using an index, which is slow for big tables.

✅ Better:

Index Scan using idx_users_email on users
  Index Cond: (email = 'stanley@example.com')

This means:

  • The database used your index — much faster!

💡 When to Use EXPLAIN

SituationUse EXPLAIN?
Query is slow✅ Yes
You want to tune a query✅ Yes
Adding or testing an index✅ Yes
Optimizing joins✅ Yes
Posted in Без рубрики | Comments Off on Database.Middle.What is query execution plan (EXPLAIN)?

Database.Middle.How does a database optimizer work?

🧠 What Is a Database Optimizer?

A database optimizer is a component of a database engine that takes your SQL query and figures out the best plan to execute it.

It doesn’t just run the query directly — instead, it thinks:

“There are many ways to get this data. Which one is the cheapest and fastest?”

🔍 What the Optimizer Does (Step-by-Step)

1. Parse the SQL

The query is checked for syntax and converted into a tree of operations.

2. Generate Possible Plans

It considers different options:

  • Use an index or not?
  • Which table to read first in a join?
  • Which type of join? (nested loop, hash join, merge join)
  • Scan the whole table or use a filter?

3. Estimate Costs

For each plan, it estimates:

  • How many rows will be read?
  • How much CPU and memory will be used?
  • How long will it take?

This is based on statistics: table size, index cardinality, row estimates.

4. Pick the Best Plan

It picks the lowest-cost plan and runs it.

📦 Example: Simple Query

SELECT * FROM users WHERE email = 'stanley@example.com';

The optimizer might consider:

  • Full table scan ❌ (slow)
  • Index scan on email ✅ (fast)
  • Maybe an index-only scan ✅✅ (even better)

🔄 Example: Join Query

SELECT * FROM orders JOIN users ON orders.user_id = users.id;

The optimizer could choose:

  • Join orders → users or users → orders?
  • Use hash join, nested loop, or merge join?
  • Use an index on users.id or not?

It analyzes these options and picks the fastest plan.

📊 What Affects the Optimizer?

FactorDescription
StatisticsEstimates on row count, data distribution
IndexesWhether indexes exist and their usefulness
ConstraintsForeign keys, uniqueness, not null, etc.
Query shapeJoins, filters, GROUP BY, ORDER BY
ConfigurationMemory, parallelism, planner settings

⚙️ How to View the Plan

PostgreSQL:

EXPLAIN ANALYZE SELECT ...;

MySQL:

EXPLAIN SELECT ...;

It shows the actual plan: what tables, what indexes, join methods, and estimated vs. actual rows.


🧠 Summary

StepWhat the Optimizer Does
ParseUnderstand the query
Plan generationList all possible ways to run it
Cost estimationGuess how long each plan will take
Plan selectionChoose the fastest/cheapest one

🚀 Bonus Tip:

Bad query performance?
➡️ Use EXPLAIN, add missing indexes, update statistics, or refactor the query.

Posted in Без рубрики | Comments Off on Database.Middle.How does a database optimizer work?

Database.Middle.Explain write-ahead logging (WAL).

📘 What is Write-Ahead Logging (WAL)?

Write-Ahead Logging is a safety mechanism used by databases to protect your data from being lost or corrupted.

💡 The key idea:
Write changes to a log before applying them to the actual database.


🧠 Why Do We Need WAL?

Imagine this:

  • You’re updating a user’s balance in a database.
  • The system crashes halfway through.
  • How do we recover? How do we know what changed?

With WAL, the database remembers what it was going to do, so it can replay or undo those changes after a crash.


🔁 Step-by-Step Example (with WAL)

Let’s say you’re updating a row in a PostgreSQL database.

1. You run:

UPDATE users SET balance = balance - 100 WHERE id = 42;

2. The database does:

✅ Step 1: Write the change to the WAL file (on disk):

"User 42 balance will be decreased by 100"

✅ Step 2: Apply the change to the actual data file (in memory or disk).

✅ Step 3: Once both are successful, the transaction is committed.

💥 What if the system crashes?

  • If only WAL was written ✅ but the actual data wasn’t yet updated:
    • 🛠 The database uses WAL to replay the change.
  • If WAL wasn’t written at all ❌:
    • The transaction is discarded (it never “existed”).

✅ This ensures Atomicity and Durability (remember ACID?).

📦 Where Is WAL Used?

SystemWAL Usage Purpose
PostgreSQLAll transactions use WAL for recovery
MySQL (InnoDB)Uses redo logs (similar to WAL)
SQLiteWAL is a journal mode for durability
KafkaUses a log-based design — WAL at core

📊 Benefits of WAL

FeatureBenefit
Crash RecoveryCan replay changes after failure
PerformanceWrites are fast — just write to WAL first
DurabilityEnsures data isn’t lost after a commit
ReplicationWAL can be shipped to replicas

🔥 Real-Life Analogy

Imagine you’re writing an important letter ✉️.

  1. First, you scribble it in your notepad (WAL).
  2. Then, you type and send it (database change).
  3. If your computer crashes, you can still finish the letter from your notepad.

That’s WAL: always write the plan first, then apply.


✅ Summary

FeatureDescription
What it isA log of changes written before the data
Main benefitProtects data from loss or corruption
Used inPostgreSQL, SQLite, MySQL (InnoDB), Kafka
Key toAtomicity and Durability in ACID
Posted in Без рубрики | Comments Off on Database.Middle.Explain write-ahead logging (WAL).

Database.What is CAP theorem?

🧠 What is the CAP Theorem?

The CAP theorem is a rule that says a distributed system (like a NoSQL database spread over many servers) can only guarantee 2 out of 3 things at the same time:

C = Consistency
A = Availability
P = Partition Tolerance

Let’s break each one down in a fun, easy way:

🍰 Imagine: You and Your Friend Have a Cake Shop

You both work in two cities and share the same order notebook (database). You talk by phone or the internet to keep in sync.


1. ✅ Consistency

“Everyone sees the same data at the same time.”

If a customer orders chocolate cake in Moscow, your friend in Yekaterinburg sees that order immediately — no delay, no mistakes.

🟢 Good for banks and payments
❌ Hard when the network is slow or broken

2. ✅ Availability

“The system always responds — even if it returns old or different data.”

If your friend’s phone is dead, you still take cake orders in your city — the shop doesn’t close! But your friend might not know about new orders right away.

🟢 Good for fast services, search engines
❌ You might serve outdated data

3. ✅ Partition Tolerance

“Even if the connection between you and your friend breaks (network partition), the system keeps going.”

You keep working even if you can’t talk to each other — maybe your friend takes new orders, and so do you, but you’re now out of sync.

🟢 Needed in every distributed system (because networks can break)
✅ This is always assumed in CAP

📏 CAP Rule

You can only choose 2 out of 3:

CombinationWhat It MeansExample
CPConsistent and Partition-tolerantSystem may be slow or deny writes
APAvailable and Partition-tolerantYou might see old data
CAConsistent and Available — but ⚠️ no partitionsOnly works in non-distributed setup

✅ Real-World Analogy

🔌 You’re messaging a friend.
Do you want your message to arrive fast (Availability),
to arrive in order and never be lost (Consistency),
or to keep chatting even if Wi-Fi is bad (Partition Tolerance)?

In a real system, you can’t have all 3 at once.


🧠 Why It Matters

Distributed systems like:

  • MongoDB
  • Cassandra
  • DynamoDB
  • Etcd
  • Kafka

All must make trade-offs:

  • Some give up consistency for speed.
  • Others sacrifice availability during failures to stay accurate.

✅ Summary Table

PropertyDescriptionExample Requirement
Consistency (C)All users see same data at the same timeBanking
Availability (A)System always respondsShopping website
Partition (P)Works even if network failsCloud systems
Posted in Без рубрики | Comments Off on Database.What is CAP theorem?

Database.Middle.ACID

🧪 ACID — A Super Rule for Databases

Think of a database like a notebook where important things are written down: money transfers, orders, messages, etc. We want to make sure:

  • The writing is correct
  • It doesn’t get messed up
  • It’s still there after a power cut
  • And people writing at the same time don’t break things

To help with this, databases follow ACID rules.


✅ A — Atomicity (like “All or Nothing”)

🔸 Meaning: A task (called a “transaction”) either happens completely or doesn’t happen at all.

👦 Imagine:

You want to give 10 coins from your piggy bank to your friend:

  1. You take 10 coins from your piggy bank.
  2. You put 10 coins into your friend’s piggy bank.

What if the first part worked, but the second failed (you dropped the coins)? You both lost! ❌

With Atomicity, the database says:

“I’ll do both steps or none — no half-done things!”


✅ C — Consistency (like “Rules are Always Followed”)

🔸 Meaning: The data must always follow the rules.

👧 Example:

You say: “Everyone must have at least 0 coins, not -10!”

If someone tries to take more coins than they have, the database says NO to keep the rules true.

So Consistency means:

“Everything in the notebook must always make sense.”


✅ I — Isolation (like “No Interrupting”)

🔸 Meaning: If two people are writing at the same time, their work doesn’t bump into each other.

👦 Example:

Two people try to take coins from your piggy bank at the same time.

Without Isolation, both might see you have 50 coins and each take 30. You end up with -10 😱

With isolation, the database says:

“Wait your turn! I’ll finish one task before starting the next.”

Even if things happen at the same time, they feel separate.


✅ D — Durability (like “Writing with a Permanent Marker”)

🔸 Meaning: Once a change is saved, it’s forever — even if the computer crashes.

👧 Example:

You wrote down “I gave 10 coins to Alice” in your notebook.

If lightning strikes the house 💥, and the computer restarts — the note is still there.

Databases make sure of that by writing to safe places like disk or log files.


🧠 Bonus Words You Asked About


🔒 Integrity

“Things must stay correct and real.”

If you say: “Every person must have a name and age” — integrity makes sure that no one is saved without them.

It’s like a teacher checking your work to make sure it follows the rules.


👥 Scalability

“Can it grow if we get more users?”

  • If 5 people use your app — no problem.
  • If 5 million people use it — can your database still handle it?

Scalability means the system can grow without breaking:

  • Add more computers?
  • Handle more users?
  • Still fast?

✅ Summary: Like a Toy Box

RuleLike…What it means for DBs
AtomicA toy that works or doesn’t — no half jobsAll or nothing transactions
ConsistentToys must fit in the right placesAlways follow rules
IsolatedPlay one game at a timeNo interference between actions
DurableToy stays put even after a stormData stays safe, even after crash
Posted in Без рубрики | Comments Off on Database.Middle.ACID

Database.Middle.What is replication lag ?

Replication lag is the delay between when a change occurs on the primary database and when that same change is visible on a replica.


🔍 What Does That Mean?

Imagine this sequence:

  1. A user updates their profile in the primary database.
  2. The update is logged (e.g., in WAL or binary logs).
  3. The read replica eventually receives and applies the change.

If the replica takes, say, 5 seconds to apply the update, then:

  • For those 5 seconds, queries on the replica show stale data.
  • This delay is the replication lag.

🧠 Why Does Replication Lag Happen?

CauseDescription
Network latencyDelay in transmitting logs from primary to replica
IO bottlenecksReplica is slow to write/apply changes
Large transactionsBig or frequent writes take time to replicate
Resource contentionCPU, disk, or memory pressure on the replica
Asynchronous replicationBy design, the replica doesn’t block the primary, but may fall behind

📏 How to Measure It

  • PostgreSQL: Compare pg_current_wal_lsn() on the primary with pg_last_wal_replay_lsn() on the replica
  • MySQL: Check Seconds_Behind_Master in SHOW SLAVE STATUS
  • MongoDB: Use rs.printSlaveReplicationInfo()

⚠️ Why It Matters

RiskExplanation
Stale readsReplica shows outdated data — confusing to users or apps
Failover issuesIf you promote a lagging replica, it may miss recent changes
Data integrityApplications relying on up-to-date reads may break

✅ How to Reduce Replication Lag

  1. Optimize write performance on the primary
  2. Use faster disk and more memory on replicas
  3. Compress replication logs
  4. Monitor and alert when lag exceeds threshold
  5. Use synchronous replication if strong consistency is needed (but slower)

🔄 Summary

TermMeaning
Replication LagTime delay between primary update and replica sync
Caused ByNetwork, IO, large writes, async delay
ResultStale reads, delayed failovers
SolutionsOptimize infra, monitor lag, use sync replication
Posted in Без рубрики | Comments Off on Database.Middle.What is replication lag ?

Database.Middle.What is a read replica and why would you use it?

A read replica is a read-only copy of a database that stays in sync with the primary (writeable) database. It is updated asynchronously — meaning changes in the primary DB are eventually propagated to the replica.


🧠 What Is a Read Replica?

  • Primary DB handles all writes (INSERT, UPDATE, DELETE)
  • Read Replica handles read-only queries
  • Updates are streamed from the primary to the replica (e.g., via binary logs or WAL)
          [ Primary DB ]
               |
     async      |
               v
         [ Read Replica ]

✅ Why Use a Read Replica?

1. Read Scalability

  • Offload heavy SELECT queries to replicas
  • Improves performance by reducing load on the primary

2. High Availability

  • Use replicas as failover targets if the primary goes down

3. Disaster Recovery

  • Replicas act as live backups — safer than daily snapshots

4. Geographic Distribution

  • Deploy replicas in different regions to reduce latency for global users

5. Reporting and Analytics

  • Run expensive reports or BI dashboards on replicas without affecting production

❌ Limitations

LimitationDescription
Eventually consistentThere can be replication lag
Read-onlyYou cannot modify data on the replica
No automatic failoverMust promote manually (or use orchestration)
Increased complexityRequires query routing and monitoring

🛠️ Example Use Case (PostgreSQL)

  1. Enable replication on the primary DB.
  2. Set up a replica server using pg_basebackup.
  3. Configure replication via standby.signal and primary_conninfo.
  4. Application routes SELECT queries to the replica.

🧰 Tools That Support Read Replicas

DatabaseRead Replica Support
PostgreSQLYes (Streaming Replication)
MySQLYes (Binlog-based Replication)
MongoDBYes (Replica Sets)
Amazon RDSYes (easy setup via console)
SQL ServerYes (Always On Read Replicas)

✅ Summary

FeatureDescription
PurposeOffload reads, increase availability
Sync MethodAsynchronous replication
Write Capability❌ No (read-only)
Best ForScalability, analytics, backups, disaster recovery
Posted in Без рубрики | Comments Off on Database.Middle.What is a read replica and why would you use it?

Database.Middle.How would you track changes (auditing) in a table?

Tracking changes in a table — known as auditing — is essential for data integrity, compliance, and debugging. There are several techniques, depending on how deep and automated you want the auditing to be.

✅ Common Methods to Track Changes (Auditing)

1. Audit Table with Triggers

Create a separate audit log table that stores changes (INSERT, UPDATE, DELETE) via database triggers.

🔧 Example (PostgreSQL):

CREATE TABLE user_audit (
    id SERIAL PRIMARY KEY,
    user_id INT,
    action TEXT,
    old_data JSONB,
    new_data JSONB,
    changed_at TIMESTAMP DEFAULT now(),
    changed_by TEXT -- optionally, who made the change
);

123

CREATE OR REPLACE FUNCTION audit_user_changes() RETURNS TRIGGER AS $$
BEGIN
    IF TG_OP = 'UPDATE' THEN
        INSERT INTO user_audit(user_id, action, old_data, new_data)
        VALUES (
            NEW.id,
            'UPDATE',
            to_jsonb(OLD),
            to_jsonb(NEW)
        );
    ELSIF TG_OP = 'DELETE' THEN
        INSERT INTO user_audit(user_id, action, old_data, new_data)
        VALUES (
            OLD.id,
            'DELETE',
            to_jsonb(OLD),
            NULL
        );
    ELSIF TG_OP = 'INSERT' THEN
        INSERT INTO user_audit(user_id, action, old_data, new_data)
        VALUES (
            NEW.id,
            'INSERT',
            NULL,
            to_jsonb(NEW)
        );
    END IF;
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_audit_users
AFTER INSERT OR UPDATE OR DELETE ON users
FOR EACH ROW EXECUTE FUNCTION audit_user_changes();

2. Change Data Capture (CDC)

Use database engine-level tools or log-based replication to track changes.

  • PostgreSQL: Logical replication or wal2json
  • MySQL: Binary log + Debezium (Kafka)
  • Oracle: GoldenGate
  • SQL Server: CDC or Change Tracking features

✅ Best for real-time stream processing of changes.

3. Temporal Tables (System-Versioned)

Let the database automatically store row history.

  • SQL Server and MariaDB support system-versioned temporal tables.
  • Tracks valid_from, valid_to dates and maintains history automatically.
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    salary DECIMAL,
    valid_from DATETIME2 GENERATED ALWAYS AS ROW START,
    valid_to DATETIME2 GENERATED ALWAYS AS ROW END,
    PERIOD FOR SYSTEM_TIME (valid_from, valid_to)
)
WITH (SYSTEM_VERSIONING = ON);

4. Manual History Tables

Maintain a parallel users_history table that you insert into manually in your app whenever data changes.

✅ Simple, but requires discipline in application code.

5. ORM-Level Auditing

Frameworks like Hibernate or Django can auto-track changes:

  • Hibernate Envers (Java)
  • Django Simple History (Python)
  • Entity Framework Audit (C#)

✅ Good for full-stack applications with strong ORM usage.

📦 What to Include in Audit Logs

FieldPurpose
actionType of change (INSERT, UPDATE, etc.)
timestampWhen the change occurred
user_idWho made the change (if available)
old_dataPre-change values (for UPDATE/DELETE)
new_dataNew values (for INSERT/UPDATE)
table_nameIf auditing multiple tables

✅ Summary

MethodBest ForOverheadRealtime?
Triggers + Audit TableGeneral auditing, regulatory complianceLow-Medium❌
CDC (Change Data Capture)Stream processing, analyticsMedium✅
Temporal TablesBuilt-in history managementLow❌
ORM-based AuditingApps with heavy ORM useLow❌
Manual App-Level LoggingFull control, but error-proneMedium❌
Posted in Без рубрики | Comments Off on Database.Middle.How would you track changes (auditing) in a table?

Database.Middle.How does an index-only scan work?

An index-only scan is a performance optimization used by relational databases (like PostgreSQL, MySQL, Oracle) where a query is satisfied entirely by reading an index, without accessing the table (heap) at all.


✅ What Is an Index-Only Scan?

In a normal index scan, the database:

  1. Uses the index to find the row locations (row pointers).
  2. Then fetches the full row data from the table (called the heap or base table).

In an index-only scan, the database:

  • Retrieves all needed columns directly from the index.
  • Skips the table (heap) entirely — saving I/O and improving speed.

🧠 Requirements for Index-Only Scan

  1. All selected columns must exist in the index.
  2. The table must have a visibility map (PostgreSQL) or equivalent mechanism to know that rows are visible (not deleted or uncommitted).
  3. No need for SELECT * (unless all columns are in the index).

🔍 Example

Suppose we have this table and index:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT,
    email TEXT
);

CREATE INDEX idx_users_email ON users (email);

Query:

SELECT email FROM users WHERE email LIKE 'john%';

✅ The index idx_users_email includes the column email, which is both:

  • The filter condition
  • The output column

→ So this query can be served by an index-only scan.

📈 Why It’s Faster

OperationIndex ScanIndex-Only Scan
Read index✅✅
Access table rows✅ (extra disk I/O)❌
PerformanceMediumFaster (less I/O)

🔧 How to Enable or Encourage Index-Only Scan

  • Create covering indexes: indexes that include all needed columns
CREATE INDEX idx_orders_covering ON orders (customer_id, order_date, total_amount);
  • Avoid SELECT * — only query columns that exist in the index.
  • Keep the visibility map up-to-date (PostgreSQL):
    • Regular VACUUM helps maintain visibility maps.
  • Use EXPLAIN to confirm: sqlCopyEditEXPLAIN SELECT customer_id, order_date FROM orders WHERE order_date > '2024-01-01';

❌ Limitations

  • Won’t work if:
    • Not all columns are in the index
    • Visibility map says some rows may be outdated (PostgreSQL only)
  • Index-only scans may not always be used, even when possible — the query planner chooses the cheapest plan.

🔍 Summary

FeatureIndex ScanIndex-Only Scan
Reads from table (heap)?✅❌
Needs all columns in index❌ (some ok)✅
PerformanceModerateHigh
Use caseGeneral-purposeRead-heavy, optimized
Posted in Без рубрики | Comments Off on Database.Middle.How does an index-only scan work?