Database.Middle.How does a B-tree index work?

A B-tree index (short for Balanced Tree) is the most common type of index used in relational databases (e.g., PostgreSQL, MySQL InnoDB, Oracle). It organizes data in a way that allows fast lookup, insertion, deletion, and range queries, all in logarithmic time.

Let’s break down how it works in simple terms:


🌳 1. Structure of a B-tree

A B-tree is a balanced multi-level tree with these key properties:

  • ✅ Sorted: Keys in each node are ordered.
  • ✅ Balanced: All leaf nodes are at the same level.
  • ✅ Multi-way: Each node can have many children, not just 2 (unlike binary trees).
  • ✅ Disk-optimized: Designed to reduce disk reads/writes by grouping many keys in each node.

📚 Example (Simplified)

Suppose you have a B-tree index on column email:

         [anna, dave, kate]
        /       |       |     \
    <anna   anna–dave dave–kate >kate

Each node has:

  • Keys (sorted)
  • Pointers to children (where to go based on key comparison)

🔍 2. How Lookup Works

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

  1. Start at root node
  2. Find the range where 'dave@example.com' fits
  3. Follow pointer to the correct child node
  4. Repeat until leaf node
  5. Get pointer to actual row in the table (if non-clustered)

This takes O(log n) time.


📈 3. How Insert Works

  1. Navigate to the correct leaf node
  2. Insert key in sorted order
  3. If node is full, split and promote middle key to parent
  4. Tree remains balanced

🔁 4. How Range Queries Work

For:

SELECT * FROM orders WHERE created_at BETWEEN '2024-01-01' AND '2024-01-31';
  1. B-tree quickly locates the starting key
  2. Then performs a sequential scan across leaf nodes (they’re linked like a doubly linked list)
  3. Stops when upper bound is reached

This is why B-tree indexes are great for BETWEEN, >, <, >=, <=, and ORDER BY`.


🔧 Summary of Operations

OperationTime Complexity
Lookup (point)O(log n)
Insert/DeleteO(log n)
Range scanO(log n + k) (where k = rows returned)

🧠 Why B-trees Work So Well

  • Nodes are designed to match disk block sizes, minimizing disk I/O
  • Balance ensures predictable performance even with millions of rows
  • Self-adjusting on insert/delete
Posted in Без рубрики | Comments Off on Database.Middle.How does a B-tree index work?

Database.Middle.Is composite indexes better ?

🔹 What Is a Composite Index?

A composite index is an index on multiple columns, like:

CREATE INDEX idx_user_email_status ON users(email, status);

This index helps queries that filter or sort using email, email AND status, but not just status alone.

🧠 When Composite Indexes Are Better

✅ 1. When Queries Use Multiple Columns Together

-- Good use case
SELECT * FROM users WHERE email = 'a@example.com' AND status = 'active';

The index idx_user_email_status helps here because both columns are used.

✅ 2. When You Have Filtering + Sorting

-- Example
SELECT * FROM orders
WHERE customer_id = 123
ORDER BY order_date DESC;

Use:

CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date);

This speeds up both the filter and the sort.

⚠️ Caveats and Gotchas

❌ 1. Left-to-right prefix rule

Composite indexes only work from left to right:

CREATE INDEX idx(a, b, c)
  • Helps: WHERE a = ?, WHERE a = ? AND b = ?
  • ❌ Doesn’t help: WHERE b = ? alone

❌ 2. Not always better than single-column indexes

  • If you only ever query by one column, a single-column index is faster and smaller.

❌ 3. More indexes = slower inserts/updates

Every index must be updated during INSERT, UPDATE, or DELETE.

✅ Summary

SituationComposite IndexBetter?
Query filters on multiple columns✅ Yes
Query uses leftmost columns in WHERE or ORDER BY✅ Yes
Query only uses non-leftmost column❌ No
Many writes and rarely use multi-column filters❌ No
Large table with sorting + filtering✅ Yes
Posted in Без рубрики | Comments Off on Database.Middle.Is composite indexes better ?

Database.Middle.How would you optimize a slow SQL query?

To optimize a slow SQL query, follow a structured approach. Here’s a step-by-step guide with techniques, tools, and tips:


🔍 1. Analyze the Query

✅ Use EXPLAIN (or EXPLAIN ANALYZE)

  • In PostgreSQL:
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'a@example.com';

In MySQL:

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

This reveals how the query is executed:

  • Full table scan?
  • Index used?
  • Join type?
  • Rows examined?

🔧 2. Index Optimization

✅ Add indexes on columns used in:

  • WHERE, JOIN, ORDER BY, GROUP BY
CREATE INDEX idx_users_email ON users(email);

❗ Avoid over-indexing: indexes slow down inserts/updates.

✅ Use composite indexes for multiple columns

CREATE INDEX idx_orders_user_date ON orders(user_id, order_date);

🛠️ 3. Query Rewrite

✅ Avoid SELECT *

Only select necessary columns:

SELECT id, email FROM users WHERE status = 'active';

✅ Use EXISTS instead of IN (for subqueries)

-- Bad
SELECT * FROM orders WHERE user_id IN (SELECT id FROM users WHERE active = true);

-- Better
SELECT * FROM orders WHERE EXISTS (
    SELECT 1 FROM users WHERE users.id = orders.user_id AND active = true
);

🤝 4. Optimize Joins

✅ Use appropriate join types:

  • INNER JOIN is faster than OUTER JOIN if you don’t need unmatched rows.

✅ Index foreign keys and join columns:

CREATE INDEX idx_orders_user_id ON orders(user_id);

📊 5. Reduce Rows Early

✅ Use LIMIT for pagination:

SELECT * FROM products WHERE category = 'tech' LIMIT 50;

✅ Add filters as early as possible:

SELECT * FROM logs WHERE event_time > NOW() - INTERVAL '1 day';

🧠 6. Materialize or Cache Results

  • Use materialized views for expensive queries (PostgreSQL):
CREATE MATERIALIZED VIEW recent_orders AS
SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '1 day';

Use temporary tables for intermediate steps.

🔁 7. Check for Table Bloat and Stats

✅ PostgreSQL

  • Run ANALYZE or VACUUM ANALYZE to update stats.
  • Use pg_stat_user_tables to find large or bloated tables.

✅ MySQL

  • Use ANALYZE TABLE to update optimizer statistics.

🧪 8. Test and Compare

Use query performance benchmarks:

  • Run test queries with/without index
  • Monitor rows, cost, and execution time via EXPLAIN ANALYZE

✅ Example: Optimization in Action

❌ Bad:

SELECT * FROM orders WHERE user_id = 5 ORDER BY created_at;
  • No index on user_id
  • Sort is slow

✅ Improved:

CREATE INDEX idx_orders_user_date ON orders(user_id, created_at);

SELECT id, created_at, total FROM orders
WHERE user_id = 5
ORDER BY created_at;
Posted in Без рубрики | Comments Off on Database.Middle.How would you optimize a slow SQL query?

Database.Middle.ClusterIndex.Examples

🐘 PostgreSQL

📌 1. Create a Table

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

✅ Clustered Index in PostgreSQL

PostgreSQL does not have a true clustered index like SQL Server or MySQL’s InnoDB. But you can physically reorder the table using the CLUSTER command on an index:

-- Create a regular (non-clustered) index
CREATE INDEX idx_users_created_at ON users(created_at);

-- Cluster the table based on that index (reorders the data on disk)
CLUSTER users USING idx_users_created_at;

-- Optional: mark table as clustered (for documentation)
-- Note: PostgreSQL doesn't automatically keep it clustered after new inserts

⚠️ In PostgreSQL, clustering is a one-time operation and not maintained automatically after inserts.

✅ Non-Clustered Index in PostgreSQL

PostgreSQL’s default indexes (e.g. CREATE INDEX) are non-clustered:

-- Index for faster email lookups (non-clustered)
CREATE INDEX idx_users_email ON users(email);

🐬 MySQL (InnoDB Engine)

📌 1. Create a Table

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    created_at DATETIME
) ENGINE=InnoDB;

✅ Clustered Index in MySQL

In InnoDB, the primary key is always the clustered index:

-- id is PRIMARY KEY, so it's also the CLUSTERED index
-- Data is physically ordered by 'id'

You can also create a different clustered index by defining a primary key or a unique key first (InnoDB clusters by the first unique NOT NULL index):

-- This would make 'email' the clustered index (if no PK is defined first)
CREATE TABLE users (
    email VARCHAR(100) NOT NULL UNIQUE,
    name VARCHAR(100),
    created_at DATETIME
) ENGINE=InnoDB;

⚠️ Only one clustered index per table in MySQL (InnoDB).

✅ Non-Clustered Index in MySQL

Any other secondary index is non-clustered:

-- Secondary index (non-clustered)
CREATE INDEX idx_users_created_at ON users(created_at);

🧠 Summary

FeaturePostgreSQLMySQL (InnoDB)
Clustered IndexEmulated via CLUSTERAlways on PRIMARY KEY
Maintains clustering?❌ Not automatically✅ Always maintained
Non-Clustered IndexDefault for all indexesAny secondary (non-PK) index
Posted in Без рубрики | Comments Off on Database.Middle.ClusterIndex.Examples

Database.Middle.What are the differences between clustered and non-clustered indexes?

🔹 Clustered Index

FeatureDescription
Data StoragePhysically reorders the table’s rows to match the index. The index is the table.
Only OneA table can have only one clustered index because data rows can be sorted in only one order.
Faster for Range QueriesIdeal for queries that return a range of values (e.g. BETWEEN, ORDER BY, GROUP BY) because data is stored in order.
Primary Key by DefaultIn many databases, the primary key automatically becomes the clustered index (unless specified otherwise).
Example UseIndexing customer_id in a Customers table for fast lookup and sorting by ID.

🔹 Non-Clustered Index

FeatureDescription
Separate from DataStores a copy of indexed columns and a pointer to the actual data row. The data remains in the original order.
Multiple AllowedA table can have many non-clustered indexes (limited by DBMS).
Slower for Range QueriesLess efficient for large range scans due to extra lookup steps (called bookmark lookups or key lookups).
FlexibleCan be created on any column(s), useful for filtering or joining on non-primary fields.
Example UseIndexing the email column in a Users table to speed up login queries.

🧠 Quick Analogy

  • Clustered index: Like a sorted dictionary, where entries are arranged by the word itself.
  • Non-clustered index: Like the index at the back of a textbook, pointing you to page numbers (locations of data).

✅ Summary Table

AspectClustered IndexNon-Clustered Index
Physical order of dataMatches indexUnchanged
Number per tableOneMany
Speed for range queriesFasterSlower
Points to data?Is the dataPoints to data
Ideal forSorting, range filtersLookups on non-key columns
Posted in Без рубрики | Comments Off on Database.Middle.What are the differences between clustered and non-clustered indexes?

Database.Beginner.How do you prevent SQL injection?

What is SQL Injection?

SQL Injection happens when untrusted input (like user input) is mixed into a SQL query without proper handling — allowing an attacker to inject malicious SQL code.

🧨 This can lead to:

  • Data leaks (stealing confidential data).
  • Data destruction (deleting rows, dropping tables).
  • Bypassing authentication.
  • Gaining unauthorized access.

How Do You Prevent SQL Injection?

Here’s a full toolkit of defenses:


1. Use Prepared Statements (Parameterized Queries) 🚀 BEST Practice

✅ DO NOT build SQL by concatenating strings.

Bad (Vulnerable):

-- Unsafe (Directly embedding user input!)
"SELECT * FROM Users WHERE username = '" + userInput + "';"

Good (Safe with Parameters):

-- Safe prepared statement
SELECT * FROM Users WHERE username = ?;
  • User input is treated as data, not code.
  • SQL engine understands the query structure — no one can “inject” extra SQL.

✅ Examples in Code:

  • Java (JDBC):
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Users WHERE username = ?");
stmt.setString(1, userInput);

Python (with psycopg2 for PostgreSQL):

cursor.execute("SELECT * FROM Users WHERE username = %s", (userInput,))

2. Use ORM Frameworks

Frameworks like:

  • Hibernate (Java)
  • Entity Framework (C#)
  • Sequelize (Node.js)
  • Django ORM (Python)

✅ They generate SQL safely for you — making injections much harder.

3. Validate and Sanitize Input

✅ Validate:

  • Check if input matches expected format:
    • If you expect an integer, make sure it’s an integer.
    • If you expect an email, validate email format.

✅ Sanitize:

  • Strip or reject unexpected characters.
  • Forbid semicolons, comments (--), or SQL keywords when not needed.

But note: Validation alone is NOT enough — always combine with prepared statements.

4. Use Least Privilege Principle

✅ Your application’s database user should have:

  • Only the permissions it needs.
  • No DROP TABLE, no DELETE unless required.

🚫 Don’t connect your app as a superuser or root!

Even if someone manages an injection, they can’t destroy the whole database.

5. Disable Multiple Statements (Batching)

✅ Some database drivers can be configured to disallow multiple SQL statements in one call.

Example:

  • Disable this in MySQL drivers (allowMultiQueries=false).

It blocks attacks like:

' OR 1=1; DROP TABLE Users; --

6. Error Handling and Logging

  • Don’t show raw SQL errors to users — they can leak database structure!
    ✅ Always:
  • Log detailed errors internally.
  • Show a generic error message to the user.

Summary of Defense Strategy

DefensePurpose
Prepared StatementsTreat user input as data, not code.
ORMsAuto-generate safe SQL.
Input Validation/SanitizationCatch bad inputs early.
Least Privilege Database UserMinimize damage even if attacked.
Disable Multi-StatementsPrevent attacks with ; chaining.
Safe Error HandlingDon’t leak database details to attackers.

Real-Life Analogy

SQL Injection is like:

🧨 Allowing a stranger to write a custom command in your flight cockpit.

🚫 Prepared Statements are like: “I only let you press certain buttons, not type anything you want.”


In Short

Always use prepared statements — it’s the #1 safest, most reliable defense against SQL injection.

Posted in Без рубрики | Comments Off on Database.Beginner.How do you prevent SQL injection?

Database.Beginner.What is the difference between UNION and UNION ALL?

Difference Between UNION and UNION ALL

FeatureUNIONUNION ALL
DuplicatesRemoves duplicate rows (DISTINCT).Keeps all rows, including duplicates.
PerformanceSlower — must sort and remove duplicates.Faster — no extra work to remove duplicates.
Result SizeSmaller or equal (no duplicates).Can be bigger (duplicates allowed).
SortingMay add overhead due to de-duplication.No sorting unless you explicitly order.

1. UNION

Combines the result sets of two queries and removes duplicates automatically.

Example

SELECT city FROM Customers
UNION
SELECT city FROM Suppliers;

✅ Combines all cities from Customers and Suppliers, but if the same city appears in both — it will show only once.

  • Behind the scenes: database does a DISTINCT to eliminate duplicates.
  • Slower on big datasets because of sorting/de-duplication.

2. UNION ALL

Combines the result sets without removing duplicates — faster.

Example

SELECT city FROM Customers
UNION ALL
SELECT city FROM Suppliers;

✅ Combines all cities — if “New York” appears 10 times, it will appear 10 times in the result.

  • No DISTINCT — much faster because no need to sort or compare rows.
  • Duplicates are kept exactly as they are.

Visual Example

Query 1 ResultQuery 2 Result
ParisNew York
LondonParis
BerlinTokyo

  • UNION result:
Paris
London
New York
Berlin
Tokyo

(✅ No duplicates — Paris appears once.)

UNION ALL result:

Paris
London
New York
Paris
Berlin
Tokyo

(✅ Duplicates kept — Paris appears twice.)

When Should You Use Which?

Use UNION whenUse UNION ALL when
You want unique rows only.You want all rows including duplicates.
You are okay with slower performance for clean results.You need faster performance and duplicates don’t matter (or you’ll clean them later).
Data might have overlaps you want to eliminate.Data should preserve frequency/count.

Important Notes

  • Both queries must have the same number of columns and compatible types.
  • Column names are taken from the first query.
  • If you want an ordered result, you must add an ORDER BY at the end: sqlCopyEdit
SELECT city FROM Customers
UNION
SELECT city FROM Suppliers
ORDER BY city;

In Short

UNION removes duplicates (DISTINCT).
UNION ALL keeps all rows (no DISTINCT).

✅ UNION = clean but slower.
✅ UNION ALL = fast but may have duplicates.

Posted in Без рубрики | Comments Off on Database.Beginner.What is the difference between UNION and UNION ALL?

Database.Beginner.StoredProcedures

What are Stored Procedures in SQL?

✅ A Stored Procedure is:

A saved collection of SQL statements (plus optional logic like loops, conditions) that you can execute later by calling its name.

  • Think of it like a function in programming — but for your database.
  • You write it once and reuse it many times.
  • Stored inside the database — not in your app code.
  • Can have input parameters, output parameters, and return values.

Why Use Stored Procedures?

  • ✅ Reuse code — no repeating the same SQL.
  • ✅ Encapsulate logic — database workflows, business rules.
  • ✅ Improve performance — precompiled, cached by the database.
  • ✅ Improve security — control what users can execute without giving table access.

Basic Example

Imagine you want to get all users from a specific city.

Without stored procedure:

SELECT * FROM Users WHERE city = 'New York';

You write this everywhere you need it.


With a Stored Procedure:

CREATE PROCEDURE GetUsersByCity(IN city_name VARCHAR(100))
BEGIN
    SELECT * FROM Users WHERE city = city_name;
END;

Now to call it:

CALL GetUsersByCity('New York');

What Can Stored Procedures Do?

FeatureDescription
Accept parametersInput/output values — dynamic SQL.
Contain control logicIF, CASE, WHILE, loops — not just plain SQL.
Perform multiple stepsInsert, Update, Delete — all in one procedure.
Return resultsOutput data sets or variables.
Error handlingTRY…CATCH (depends on DB).

Stored Procedure Syntax Overview

(Example in MySQL — syntax varies slightly in SQL Server, PostgreSQL, Oracle.)

CREATE PROCEDURE ProcedureName(IN param1 DATATYPE, OUT param2 DATATYPE)
BEGIN
    -- SQL statements here
END;

IN: Input parameter (you pass a value in).

OUT: Output parameter (procedure sets a value).

INOUT: Can do both.

Real-World Use Cases

ScenarioStored Procedure Purpose
User registrationValidate, insert, send welcome message.
Monthly reportsCollect and process data from different tables.
Batch updatesUpdate many rows based on complex rules.
TransactionsExecute multiple steps with commit/rollback logic.

Stored Procedure vs Functions

Stored ProcedureFunction
Can return zero or multiple results.Must return a single value (scalar or table).
Can have complex logic, transactions.Usually one calculation, no transactions.
Called with CALL or EXEC keyword.Used inside SELECT queries.

In Short

Stored Procedures are saved programs inside the database — they bundle SQL logic into reusable, callable blocks.

✅ They make SQL modular, reusable, faster, and safer.

Posted in Без рубрики | Comments Off on Database.Beginner.StoredProcedures

Database.Beginner.How is a correlated subquery different from a regular subquery?

Difference Between Regular Subquery and Correlated Subquery

FeatureRegular (Non-Correlated) SubqueryCorrelated Subquery
Depends on outer query?❌ No — runs independently.✅ Yes — refers to columns from the outer query.
ExecutionExecuted once.Executed once for each row of the outer query.
PerformanceUsually faster — one execution.Can be slower — multiple executions.
UsageCommon in WHERE, HAVING, SELECT, FROM.Often in WHERE or SELECT clauses.

1. Regular (Non-Correlated) Subquery

Runs once — result is used for the entire outer query.

Example

Find all products with a price higher than the average price:

SELECT name, price
FROM Products
WHERE price > (SELECT AVG(price) FROM Products);
  • (SELECT AVG(price) FROM Products) is calculated once.
  • The outer query uses that value to compare for all rows.

✅ Independent — no reference to the outer query.

2. Correlated Subquery

The subquery depends on the current row of the outer query — it repeats for each row.

Example

Find all employees who earn more than the average salary in their department:

SELECT e1.name, e1.department, e1.salary
FROM Employees e1
WHERE e1.salary > (
    SELECT AVG(e2.salary)
    FROM Employees e2
    WHERE e2.department = e1.department
);
  • For each employee e1, the subquery:
    • Calculates the average salary of that employee’s department.
  • e2.department = e1.department — references the outer query.

✅ Dependent — the subquery uses values from the current row of the outer query.

Key Differences Visually

Regular Subquery (Run Once)
Outer query (uses one subquery result)
└── Subquery (runs once, gives a constant)
Correlated Subquery (Run per Row)
Outer query (for each row)
└── Subquery (uses values from the outer row)

Why Correlated Subqueries Are Slower

  • If you have 10,000 rows, a regular subquery runs once.
  • A correlated subquery might run 10,000 times — once for each row!
  • More work → more time → slower on big data.

Real-Life Analogy

Regular SubqueryCorrelated Subquery
Ask: “What’s the average height of all people?”Ask: “What’s the average height of people in your city?” (for every person)

In Short

Regular SubqueryCorrelated Subquery
Runs once.Runs once for each row.
Independent of outer query.Depends on outer query’s current row.
Posted in Без рубрики | Comments Off on Database.Beginner.How is a correlated subquery different from a regular subquery?

Database.Beginner.What is a subquery?

What is a Subquery in SQL?

✅ A subquery is:

A query inside another query.

  • It’s enclosed in parentheses (...).
  • It is executed first, and its result is used by the outer query.
  • It can return:
    • A single value (scalar).
    • A row.
    • A table (multiple rows and columns).

Where Can You Use a Subquery?

You can use subqueries in:

ClauseExample
SELECT listIn column selection
FROM clauseAs a derived table
WHERE clauseTo filter rows
HAVING clauseTo filter groups

Simple Example

Suppose you have a table Products:

product_idnameprice
1Phone800
2Laptop1200
3Tablet600

Find products that are more expensive than the average price.

Step-by-step:

  1. First, calculate the average price:
SELECT AVG(price) FROM Products;

Then, find products with price > average:

SELECT name, price
FROM Products
WHERE price > (SELECT AVG(price) FROM Products);

✅ The part:

(SELECT AVG(price) FROM Products)

is a subquery — it returns a single value (the average price) and is used in the WHERE clause.

Types of Subqueries

TypeDescription
Scalar SubqueryReturns a single value (one row, one column).
Row SubqueryReturns a single row (multiple columns).
Table SubqueryReturns a full table (multiple rows, multiple columns).
Correlated SubqueryRefers to columns from the outer query. Runs row-by-row.

What is a Correlated Subquery?

A correlated subquery depends on the outer query — it runs once for each row.

Example:
Find customers who placed more orders than average for their city:

SELECT c.customer_id, c.city
FROM Customers c
WHERE 
    (SELECT COUNT(*)
     FROM Orders o
     WHERE o.customer_id = c.customer_id) 
    > 5;

Here, the subquery depends on c.customer_id from the outer query.

It’s executed once for each customer — correlated.

Key Points About Subqueries

FeatureDetails
Executed firstInner query runs before the outer query.
Can be used in many clausesSELECT, FROM, WHERE, HAVING.
Helps build complex logicBreaks down complex problems.
Scalar, Row, or TableCan return different amounts of data.
Correlated vs Non-correlatedCorrelated re-executes per row; non-correlated runs once.

In Short

Subquery = Query inside another query — used to get values or tables dynamically inside your SQL statements.

Posted in Без рубрики | Comments Off on Database.Beginner.What is a subquery?