{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-sql-performance-monitoring","conceptKey":"sql-performance-monitoring","subjectId":"subject-sql-performance-monitoring","title":"SQL Performance Monitoring","summary":"SQL Performance Monitoring is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>SQL Performance Monitoring</h2><p>SQL Performance Monitoring is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>EXPLAIN SELECT * FROM orders WHERE customer_id = 42 ORDER BY ordered_at DESC;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An orders query that was fast with a thousand rows becomes slow at ten million rows. The team must measure the plan and improve it without changing the result.</p><h3>Advanced example</h3><pre><code>CREATE INDEX idx_orders_customer_date\n  ON orders (customer_id, ordered_at DESC);\n\nEXPLAIN\nSELECT id, total, ordered_at\nFROM orders\nWHERE customer_id = 42\nORDER BY ordered_at DESC\nLIMIT 20;</code></pre><h3>Expected result</h3><p>The execution plan shows a measured reduction in unnecessary scanning or sorting without changing query results.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>products, orders</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>CREATE INDEX idx_orders_customer_date\n  ON orders (customer_id, ordered_at DESC);\n\nEXPLAIN SELECT order_id, total\nFROM orders\nWHERE customer_id = 1\nORDER BY ordered_at DESC\nLIMIT 20;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>SQL Performance Monitoring: MySQL and PostgreSQL</h3><p>Index and optimizer behavior is vendor-specific. Compare EXPLAIN output on the actual MySQL or PostgreSQL version and measure with production-shaped data before keeping an index.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-sql-performance-monitoring-6b91a726","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-sql-performance-monitoring","conceptKey":"sql-performance-monitoring","subjectId":"subject-sql-performance-monitoring","title":"SQL Performance Monitoring","summary":"SQL Performance Monitoring is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>SQL Performance Monitoring</h2><p>SQL Performance Monitoring is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>EXPLAIN SELECT * FROM orders WHERE customer_id = 42 ORDER BY ordered_at DESC;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An orders query that was fast with a thousand rows becomes slow at ten million rows. The team must measure the plan and improve it without changing the result.</p><h3>Advanced example</h3><pre><code>CREATE INDEX idx_orders_customer_date\n  ON orders (customer_id, ordered_at DESC);\n\nEXPLAIN\nSELECT id, total, ordered_at\nFROM orders\nWHERE customer_id = 42\nORDER BY ordered_at DESC\nLIMIT 20;</code></pre><h3>Expected result</h3><p>The execution plan shows a measured reduction in unnecessary scanning or sorting without changing query results.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>products, orders</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>CREATE INDEX idx_orders_customer_date\n  ON orders (customer_id, ordered_at DESC);\n\nEXPLAIN SELECT order_id, total\nFROM orders\nWHERE customer_id = 1\nORDER BY ordered_at DESC\nLIMIT 20;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>SQL Performance Monitoring: MySQL and PostgreSQL</h3><p>Index and optimizer behavior is vendor-specific. Compare EXPLAIN output on the actual MySQL or PostgreSQL version and measure with production-shaped data before keeping an index.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-sql-performance-monitoring-6b91a726","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}