Creating databases and tables in Hadoop - Performance & Efficiency
When we create databases and tables in Hadoop, we want to know how long it takes as we add more commands or data.
We ask: How does the time to create these structures grow when we create more of them?
Analyze the time complexity of the following code snippet.
CREATE DATABASE IF NOT EXISTS sales_db;
USE sales_db;
CREATE TABLE IF NOT EXISTS transactions (
id INT,
amount FLOAT,
date STRING
) STORED AS PARQUET;
CREATE TABLE IF NOT EXISTS customers (
customer_id INT,
name STRING
) STORED AS ORC;
This code creates one database and two tables inside it, defining their structure and storage format.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Executing each CREATE statement one by one.
- How many times: Once per database or table creation command.
Each new database or table requires a separate command to run.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 commands executed |
| 100 | 100 commands executed |
| 1000 | 1000 commands executed |
Pattern observation: The time grows directly with the number of create commands.
Time Complexity: O(n)
This means the time to create databases and tables grows in a straight line as you add more create commands.
[X] Wrong: "Creating multiple tables happens all at once, so time stays the same no matter how many tables."
[OK] Correct: Each create command runs separately, so more tables mean more commands and more time.
Understanding how time grows with commands helps you explain system behavior clearly and shows you think about efficiency in real projects.
"What if we batch multiple table creations into a single script? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of a database
A database is used to organize data logically into separate groups or collections.Step 2: Differentiate database from tables
Tables store data in rows and columns inside a database, but the database itself is for grouping.Final Answer:
To organize data into separate groups -> Option CQuick Check:
Database = Organize data groups [OK]
- Confusing database with table
- Thinking database stores data directly
- Assuming database speeds up queries automatically
sales_db in Hive?Solution
Step 1: Recall Hive syntax for database creation
The correct command to create a database in Hive isCREATE DATABASE database_name;.Step 2: Check each option
Only CREATE DATABASE sales_db; matches the correct syntax exactly.Final Answer:
CREATE DATABASE sales_db; -> Option AQuick Check:
CREATE DATABASE = correct syntax [OK]
- Using MAKE or NEW instead of CREATE
- Abbreviating DATABASE as DB
- Missing semicolon at end
CREATE TABLE employees (id INT, name STRING, salary FLOAT);
Solution
Step 1: Analyze the CREATE TABLE statement
The query uses correct syntax to create a table named employees with columns id, name, and salary.Step 2: Check for errors or missing parts
Database name is optional if default database is used; no syntax error here.Final Answer:
Creates a table named employees with three columns -> Option AQuick Check:
CREATE TABLE with columns = creates table [OK]
- Thinking database name is mandatory
- Confusing table and database creation
- Assuming syntax error without checking carefully
CREATE TABLE users id INT, name STRING;
Solution
Step 1: Review the syntax for CREATE TABLE
Column definitions must be enclosed in parentheses after the table name.Step 2: Check the given query
The query misses parentheses aroundid INT, name STRING, causing syntax error.Final Answer:
Missing parentheses around column definitions -> Option BQuick Check:
Columns need parentheses [OK]
- Forgetting parentheses
- Assuming table name is wrong
- Thinking data types are invalid
company_db and inside it a table departments with columns dept_id INT and dept_name STRING. Which sequence of commands is correct?Solution
Step 1: Create the database first
UseCREATE DATABASE company_db;to make the database.Step 2: Switch to the new database
UseUSE company_db;to set the context for table creation.Step 3: Create the table with correct syntax
UseCREATE TABLE departments (dept_id INT, dept_name STRING);inside the database.Final Answer:
CREATE DATABASE company_db; USE company_db; CREATE TABLE departments (dept_id INT, dept_name STRING); -> Option DQuick Check:
Create DB, switch DB, then create table [OK]
- Trying to create table before database
- Wrong order of USE and CREATE DATABASE
- Missing parentheses in CREATE TABLE
Ready to prove it?
Pass the test and this pattern moves unknown
