0
0
PostgreSQLquery~20 mins

GiST index for geometric and text in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GiST Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GiST index usage on geometric data

Consider a PostgreSQL table shapes with a box column storing geometric boxes. A GiST index is created on this column. What will be the output of the following query?

SELECT count(*) FROM shapes WHERE box && '((1,1),(5,5))'::box;

Assume the table has these rows:

  • box '((0,0),(2,2))'
  • box '((3,3),(6,6))'
  • box '((6,6),(8,8))'
PostgreSQL
CREATE TABLE shapes (id SERIAL PRIMARY KEY, box box);
INSERT INTO shapes (box) VALUES ('((0,0),(2,2))'), ('((3,3),(6,6))'), ('((6,6),(8,8))');
CREATE INDEX shapes_box_gist_idx ON shapes USING gist(box);

SELECT count(*) FROM shapes WHERE box && '((1,1),(5,5))'::box;
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

The && operator checks if two boxes overlap.

🧠 Conceptual
intermediate
1:30remaining
Understanding GiST index benefits for text search

Why is a GiST index useful for full-text search in PostgreSQL?

AIt automatically corrects spelling mistakes in queries.
BIt supports indexing complex data types like tsvector for fast text search.
CIt compresses text data to save disk space.
DIt stores full text documents directly for faster retrieval.
Attempts:
2 left
💡 Hint

Think about what GiST indexes can do with special data types.

📝 Syntax
advanced
2:00remaining
Correct GiST index creation for text search

Which of the following SQL statements correctly creates a GiST index on a tsvector column named document_vector in a table articles?

ACREATE INDEX articles_gist_idx ON articles USING gist(document_vector);
BCREATE INDEX articles_gist_idx ON articles USING btree(document_vector);
CCREATE INDEX articles_gist_idx ON articles USING gist(document_vector text_pattern_ops);
DCREATE INDEX articles_gist_idx ON articles USING hash(document_vector);
Attempts:
2 left
💡 Hint

GiST indexes require the gist keyword and do not use operator classes like text_pattern_ops for tsvector.

optimization
advanced
2:00remaining
Optimizing spatial queries with GiST index

You have a table locations with a point column storing coordinates. You want to find all points within a rectangle quickly. Which query will best use a GiST index on the point column?

ASELECT * FROM locations WHERE point <@ box '((1,1),(5,5))';
BSELECT * FROM locations WHERE point = '(3,3)';
CSELECT * FROM locations WHERE point && box '((1,1),(5,5))';
DSELECT * FROM locations WHERE point < box '((1,1),(5,5))';
Attempts:
2 left
💡 Hint

Look for the operator that checks if a point is inside a box.

🔧 Debug
expert
3:00remaining
Diagnosing GiST index usage issue on text search

A developer created a GiST index on a tsvector column content_vector in table documents. However, queries using to_tsvector('english', content) in the WHERE clause are slow and do not use the index. Why?

AThe <code>content_vector</code> column must be of type <code>text</code> for GiST index to work.
BGiST indexes do not support <code>tsvector</code> columns for text search.
CThe index must be created with the <code>GIN</code> method, not GiST, to be used.
DThe index is on <code>content_vector</code>, but the query uses <code>to_tsvector('english', content)</code> directly, so the index is not used.
Attempts:
2 left
💡 Hint

Check if the query expression matches the indexed column.