Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using ANALYZE to Collect Table Statistics in PostgreSQL
📖 Scenario: You are a database administrator for a small online bookstore. You want to help PostgreSQL make better decisions when running queries by collecting up-to-date statistics about your book sales data.
🎯 Goal: Learn how to use the ANALYZE command in PostgreSQL to collect statistics on a table, which helps the database optimize query performance.
📋 What You'll Learn
Create a table named sales with columns id, book_title, and quantity.
Insert sample data into the sales table.
Use the ANALYZE command to collect statistics on the sales table.
Verify that statistics have been collected by querying the system catalog.
💡 Why This Matters
🌍 Real World
Database administrators regularly run ANALYZE to keep query performance fast by updating statistics.
💼 Career
Knowing how to collect and check statistics is essential for roles like database administrator and backend developer.
Progress0 / 4 steps
1
Create the sales table
Create a table called sales with these columns: id as an integer primary key, book_title as text, and quantity as integer.
PostgreSQL
Hint
Use CREATE TABLE with the specified columns and types.
2
Insert sample data into sales
Insert these three rows into the sales table: (1, 'The Great Gatsby', 5), (2, '1984', 8), and (3, 'To Kill a Mockingbird', 3).
PostgreSQL
Hint
Use a single INSERT INTO statement with multiple rows.
3
Run ANALYZE on the sales table
Write the SQL command to run ANALYZE on the sales table to collect statistics.
PostgreSQL
Hint
Use the ANALYZE command followed by the table name.
4
Check collected statistics in pg_stat_all_tables
Write a query to select relname and last_analyze from pg_stat_all_tables where relname is 'sales'.
PostgreSQL
Hint
Use a SELECT statement with a WHERE clause filtering on relname.
Practice
(1/5)
1. What is the main purpose of the ANALYZE command in PostgreSQL?
easy
A. To create indexes on tables
B. To delete old data from tables
C. To backup the database
D. To collect statistics about tables for query planning
Solution
Step 1: Understand ANALYZE function
The ANALYZE command collects statistics about the contents of tables.
Step 2: Purpose of statistics
These statistics help the database decide the best way to run queries efficiently.
Final Answer:
To collect statistics about tables for query planning -> Option D
Quick Check:
ANALYZE = collect statistics [OK]
Hint: ANALYZE gathers table stats to improve query plans [OK]
Common Mistakes:
Confusing ANALYZE with data deletion
Thinking ANALYZE creates indexes
Assuming ANALYZE backs up data
2. Which of the following is the correct syntax to run ANALYZE on a specific table named employees with detailed output?
easy
A. ANALYZE VERBOSE employees;
B. ANALYZE employees VERBOSE;
C. ANALYZE TABLE employees VERBOSE;
D. ANALYZE VERBOSE ON employees;
Solution
Step 1: Recall ANALYZE syntax
The correct syntax is ANALYZE [VERBOSE] table_name; with VERBOSE before the table name.
Step 2: Check each option
ANALYZE VERBOSE employees; matches the correct syntax exactly. Others have incorrect order or extra keywords.