Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Query Optimization | Section
Оптимізація SQL та Особливості Запитів
Секція 1. Розділ 14
single

single

bookChallenge: Query Optimization

Свайпніть щоб показати меню

In this practical challenge, you will analyze and optimize a join query using the products and sales tables. Query optimization is essential for efficient database performance, especially as data volumes grow. A slow query can often be improved by understanding how the database executes it, applying the right indexes, and rewriting the query for better execution plans.

Start by examining the following join query that retrieves the total quantity sold for each product in the "Electronics" category:

SELECT p.name, SUM(s.quantity) AS total_sold
FROM products p
JOIN sales s ON p.product_id = s.product_id
WHERE p.category = 'Electronics'
GROUP BY p.name;

This query may not perform optimally as the dataset grows, especially if the join or filtering step is inefficient. Your goal is to analyze the query using EXPLAIN, identify possible bottlenecks, and apply optimization strategies. Consider the following steps:

  • Use EXPLAIN to review the current query execution plan and look for any sequential scans or inefficient joins;
  • Determine which columns would benefit from indexing based on the join and filter conditions;
  • Create appropriate indexes to speed up the join and filtering;
  • Rewrite the query if necessary to further optimize performance, such as by filtering rows earlier or adjusting the join order.

By applying these techniques, you will gain hands-on experience with real-world SQL optimization tasks that can dramatically improve query speed and resource usage.

Завдання

Swipe to start coding

You are given a join query that retrieves the total quantity sold for each product in the "Electronics" category. Analyze and optimize this query for better performance.

  • Use EXPLAIN to analyze the execution plan of the provided join query.
  • Identify and create any indexes that would improve the performance of the join and filtering steps.
  • Optionally, rewrite the query to further optimize execution, such as by filtering before joining.
  • Demonstrate the effect of your optimizations by running EXPLAIN on your optimized query.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 14
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt