CodeBlanks Logo Icon CodeBlanks

B-tree structure

Learn the B-tree index structure.

B-tree structure

B-tree is the most common index structure. It is a balanced tree of root, branch and leaf nodes. Search, insert and delete run in O(log n) time.

CREATE INDEX idx_products_name
ON products(name);

B-tree supports equality and range queries.

The index idx_products_name is created.

B-tree structure — details

Leaves are kept sorted; this makes range queries like BETWEEN, > and < fast. Each leaf carries a pointer to the table row.

Practice B-tree structure

Create the idx_products_name index on the name column of products.

Starter code

query.sql
-- B-tree index
CREATE INDEX

Sample solution

query.sql
CREATE INDEX idx_products_name ON products(name);

Expected output

CREATE INDEX

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which query type does B-tree support?

  • Range queries Correct answer
  • Full-text search only
  • Geographic data only
  • Arrays only

Explanation: B-tree supports range queries.

2 Create an index

Question
CREATE INDEX idx_products_name ON ____
Answer
CREATE INDEX idx_products_name ON products(name)

Explanation: products(name) is the right answer.

3 B-tree structure: match the terms

Term Meaning
B-tree balanced tree
leaf leaf
range range
pointer pointer

All lessons in Expert