0% found this document useful (0 votes)
3 views

query (1)

The document outlines key optimization techniques for SQL queries, including the use of indexes, avoiding SELECT *, optimizing JOIN operations, and filtering with WHERE before GROUP BY or ORDER BY. It also discusses advanced techniques like partitioning, materialized views, and query caching, along with best practices to follow and avoid. The conclusion emphasizes the importance of optimized queries for better performance and resource utilization.

Uploaded by

noorfatimacs819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

query (1)

The document outlines key optimization techniques for SQL queries, including the use of indexes, avoiding SELECT *, optimizing JOIN operations, and filtering with WHERE before GROUP BY or ORDER BY. It also discusses advanced techniques like partitioning, materialized views, and query caching, along with best practices to follow and avoid. The conclusion emphasizes the importance of optimized queries for better performance and resource utilization.

Uploaded by

noorfatimacs819
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

Key Optimization Techniques with Examples


1. Use Indexes for Faster Searches

Problem: Full table scans are slow for large tables.


Solution: Create indexes on frequently queried columns.

Example:

sql
Copy
-- Without Index (Full Scan)
SELECT * FROM Employees WHERE department = 'Sales';

-- With Index (Faster)


CREATE INDEX idx_dept ON Employees(department);
SELECT * FROM Employees WHERE department = 'Sales';

Explanation:

• Without an index, the database scans every row.


• With an index, it performs an index seek (O(log n) time).

2. Avoid SELECT * (Use Column Projection)

Problem: Retrieving unnecessary columns wastes I/O and memory.


Solution: Fetch only required columns.

Example:

sql
Copy
-- Bad: Fetches all columns
SELECT * FROM Employees;

-- Good: Only retrieves needed columns


SELECT employee_id, name, salary FROM Employees;

Explanation:

• SELECT * reads all columns, increasing disk I/O.


• Explicit column selection reduces data transfer.

3. Optimize JOIN Operations

Problem: Joining large tables inefficiently causes performance issues.


Solution: Use proper join strategies and indexing.
Example:

sql
Copy
-- Inefficient (Cartesian Product)
SELECT * FROM Employees, Departments;

-- Optimized (Explicit JOIN with condition)


SELECT e.name, d.department_name
FROM Employees e
JOIN Departments d ON e.dept_id = d.id;

-- Even better with an index


CREATE INDEX idx_emp_dept ON Employees(dept_id);

Explanation:

• Cartesian Product generates all possible row combinations (very slow).


• Indexed JOIN reduces the search space.

4. Use WHERE Before GROUP BY / ORDER BY

Problem: Filtering after sorting/grouping is inefficient.


Solution: Apply WHERE conditions first.

Example:

sql
Copy
-- Bad: Processes all rows before filtering
SELECT department, AVG(salary)
FROM Employees
GROUP BY department
HAVING department = 'Engineering';

-- Good: Filters early


SELECT department, AVG(salary)
FROM Employees
WHERE department = 'Engineering'
GROUP BY department;

Explanation:

• HAVING filters after aggregation (slower).


• WHERE filters before aggregation (faster).

5. Avoid Subqueries When Possible


Problem: Nested subqueries can be slow.
Solution: Use JOIN instead.

Example:

sql
Copy
-- Subquery (Slower)
SELECT name FROM Employees
WHERE dept_id IN (SELECT id FROM Departments WHERE location = 'NY');

-- JOIN (Faster)
SELECT e.name
FROM Employees e
JOIN Departments d ON e.dept_id = d.id
WHERE d.location = 'NY';

Explanation:

• Subqueries run once per row.


• JOINs process data in a single pass.

6. Use LIMIT for Large Results

Problem: Fetching millions of rows consumes memory.


Solution: Use LIMIT (or TOP in SQL Server).

Example:

sql
Copy
-- Bad: Fetches all rows
SELECT * FROM Orders;

-- Good: Only fetches 100 rows


SELECT * FROM Orders LIMIT 100;

Explanation:

• LIMIT reduces network and memory overhead.

7. Avoid Functions in WHERE Clauses

Problem: Functions prevent index usage.


Solution: Restructure queries to avoid functions on indexed columns.

Example:
sql
Copy
-- Bad: Index not used due to UPPER()
SELECT * FROM Employees WHERE UPPER(name) = 'JOHN';

-- Good: Use case-insensitive search or store precomputed values


SELECT * FROM Employees WHERE name = 'John';

Explanation:

• Functions force a full scan instead of an index seek.

8. Use EXPLAIN to Analyze Query Plans

Problem: Hard to know why a query is slow.


Solution: Use EXPLAIN (MySQL/PostgreSQL) or EXPLAIN ANALYZE.

Example:

sql
Copy
EXPLAIN SELECT * FROM Employees WHERE salary > 50000;

Output Analysis:

• Seq Scan (Full Table Scan) → Needs an index.


• Index Scan (Efficient) → Good performance.

3. Advanced Optimization Techniques


1. Partitioning Large Tables

• Splits a table into smaller chunks (e.g., by date ranges).


• Example:

sql

Copy

CREATE TABLE Orders (


id INT,
order_date DATE,
amount DECIMAL
) PARTITION BY RANGE (order_date);

2. Materialized Views (Precomputed Results)


• Stores query results for fast retrieval.
• Example:

sql

Copy

CREATE MATERIALIZED VIEW emp_salary_summary AS


SELECT department, AVG(salary) as avg_salary
FROM Employees
GROUP BY department;

3. Query Caching

• Stores frequent query results in memory.


• Example (MySQL):

sql

Copy

-- Enable query cache


SET GLOBAL query_cache_size = 1000000;

4. Summary of Best Practices


Do’s ✅ Don’ts ❌
Use indexes on filtered columns Use SELECT * unnecessarily
Filter early with WHERE Apply functions in WHERE
Prefer JOIN over subqueries Use HAVING without WHERE
Limit result sets (LIMIT) Run unoptimized joins
Analyze query plans (EXPLAIN) Ignore database statistics

5. Conclusion
• Optimized queries run faster and use fewer resources.
• Use indexing, proper joins, and filtering strategies.
• Always check execution plans with EXPLAIN.

Next Steps:

• Try optimizing a slow query in your database.


• Experiment with EXPLAIN to see optimization effects.

You might also like