🔥 Topic: SQL 📄 Title: Stop Using Row Indexes on Fact Tables — Use Columnstore 🚨 Problem Your FactSales table has 50 million rows. Aggregation queries scan every row on every run. Power BI DirectQuery reports time out under load. Adding more row-store indexes barely moves the needle. Row-store indexes were built for OLTP — not analytics. 🛠️ Solution Add a Columnstore Index to your fact tables for analytics workloads: • Stores data by column not by row — aggregations read only what they need • Built-in compression reduces storage by up to 90% • Batch execution mode processes millions of rows simultaneously • Works alongside existing row-store indexes — no trade-off required One index. Transformational performance for analytics queries. 📊 Example Add a non-clustered columnstore index to your fact table: CREATE NONCLUSTERED COLUMNSTORE INDEX ncci_FactSales ON FactSales ( OrderDate, CustomerID, ProductID, Region, Amount, Discount ); Before columnstore — aggregation query on 50M rows: SELECT Region, SUM(Amount) AS TotalSales FROM FactSales WHERE OrderDate >= '2024-01-01' GROUP BY Region; -- Execution time: 18,400 ms After columnstore — same query, same data: -- Execution time: 340 ms 54x faster. Zero changes to the query or the report. ✅ Result ⚡ Aggregation queries up to 100x faster on large fact tables 🧠 Power BI DirectQuery reports load in seconds not minutes 🔒 Storage compressed by up to 90% automatically 📊 Purpose-built for Finance and Retail analytics workloads #SQL #SQLServer #ColumnstoreIndex #DataEngineering #DataAnalytics #QueryOptimisation #ETL #PowerBI #FinancialReporting #RetailAnalytics #DatabasePerformance #UKTech #HiringUK #LondonData #Analytics
Boost SQL Performance with Columnstore Indexes on Fact Tables
More Relevant Posts
-
💬 SQL Challenge of the Day Problem: Given a table "orders" with columns (order_id, customer_id, order_date, total_amount), write a SQL query to calculate the running total of total_amount for each customer ordered by order_date. Query: ```sql SELECT order_id, customer_id, order_date, total_amount, SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_total FROM orders ``` Answer: The SQL query calculates the running total of total_amount for each customer based on the order_date. Explanation: The query uses a window function with the PARTITION BY clause to separate the data into partitions by customer_id and then calculates the running total of total_amount within each partition ordered by order_date. Example: Consider the following "orders" table: | order_id | customer_id | order_date | total_amount | |----------|-------------|------------|--------------| | 1 | A | 2022-01-01 | 100 | | 2 | A | 2022-01-03 | 150 | | 3 | B | 2022-01-02 | 200 | | 4 | A | 2022-01-05 | 120 | The query will output: | order_id | customer_id | order_date | total_amount | running_total | |----------|-------------|------------|--------------|---------------| | 1 | A | 2022-01-01 | 100 | 100 | | 2 | A | 2022-01-03 | 150 | 250 | | 4 | A | 2022-01-05 | 120 | 370 | | 3 | B | 2022-01-02 | 200 | 200 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
3 SQL query patterns that cut my dashboard load times by 25% ⚡ Small changes. Big performance impact. When I was working on BI dashboards, slow queries were the biggest bottleneck. Optimizing SQL made a bigger difference than changing the tool itself. Here are 3 patterns that consistently improved performance: 1. Pre-aggregating instead of re-calculating 🔄 Instead of calculating metrics on the fly: SELECT customer_id, SUM(revenue) FROM transactions GROUP BY customer_id; I created aggregated tables upstream and queried those instead. ✅ Reduced compute at query time ✅ Faster dashboard load 2. Using proper indexing / partitioning 📂 Filtering without indexes: SELECT * FROM orders WHERE order_date >= '2025-01-01'; After partitioning or indexing on order_date, queries scanned far less data. ✅ Huge improvement for large tables ✅ Especially critical for time-based dashboards 3. Replacing subqueries with joins or CTEs ⚙️ Instead of nested subqueries: SELECT * FROM orders o WHERE customer_id IN ( SELECT customer_id FROM customers WHERE region = 'US' ); I used joins: SELECT o.* FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE c.region = 'US'; ✅ Better execution plans ✅ Faster and easier to maintain Real impact: These optimizations helped reduce dashboard load times by ~25% and improved overall user experience. SQL performance isn’t about writing more code. It’s about writing smarter queries. What’s one SQL optimization that made a big difference for you? 🤔 #SQL #DataAnalytics #DataEngineering #PerformanceTuning #BigData #OpenToWork
To view or add a comment, sign in
-
💬 SQL Challenge of the Day Problem: You are given a table named `sales_data` with the following columns: `order_id`, `customer_id`, `order_date`, and `order_amount`. Write a SQL query to calculate the running total of `order_amount` for each `customer_id` ordered by `order_date`. Query: ```sql SELECT order_id, customer_id, order_date, order_amount, SUM(order_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_total FROM sales_data; ``` Answer: The SQL query calculates the running total of `order_amount` for each `customer_id` ordered by `order_date`. Explanation: The `SUM(order_amount) OVER (PARTITION BY customer_id ORDER BY order_date)` window function is used to calculate the running total of `order_amount` for each `customer_id` as the `order_date` progresses. Example: Consider the `sales_data` table: | order_id | customer_id | order_date | order_amount | |---------|------------|------------|-------------| | 1 | 101 | 2022-01-01 | 100 | | 2 | 102 | 2022-01-02 | 150 | | 3 | 101 | 2022-01-03 | 200 | | 4 | 103 | 2022-01-04 | 120 | The output of the query will be: | order_id | customer_id | order_date | order_amount | running_total | |---------|------------|------------|-------------|--------------| | 1 | 101 | 2022-01-01 | 100 | 100 | | 3 | 101 | 2022-01-03 | 200 | 300 | | 2 | 102 | 2022-01-02 | 150 | 150 | | 4 | 103 | 2022-01-04 | 120 | 120 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
I spent the last few weeks building a full ETL pipeline in SQL Server and here's what I learned. The dataset: Olist e-commerce 100,000+ transactions, 9 raw tables, real messy data. Here's exactly what the pipeline looked like: 𝟭. Extracted raw CSVs → SQL Server Loaded everything into raw tables (R_ prefix) untouched, safe. 𝟮. Built a staging layer (STG_ tables) Never clean raw data directly. I copied everything into staging tables first, so I always had a fallback. 𝟯. Validated nulls and duplicates across all 9 tables No blind trust. Every key column checked. Found what needed fixing before touching anything. 𝟰. Diagnosed a many-to-many problem in geolocation data ZIP codes were duplicated across customers, sellers, and geolocation. I resolved it by building a normalized LOCATION table turning an M:M mess into clean 1:M relationships. 𝟱. Pre-validated all 8 foreign key relationships before loading Found 278 orphan customers, 7 orphan sellers, and 13 orphan products. Handled them deliberately not silently dropped. 𝟲. Rebuilt the schema with proper data types and constraints New tables. Correct types. Check constraints (e.g. review score must be 1–5). Composite primary keys where needed. 𝟳. Loaded clean data using CAST then enforced foreign keys Inserted from STG_ → final tables with explicit casting. Added all FK constraints only after data integrity was confirmed. The result: a fully relational, constraint-enforced schema ready for analysis in Power BI or Tableau. What I'd do differently next time: → Log orphan records to an audit table instead of just reassigning them → Add row count reconciliation checks after every INSERT → Use DECIMAL instead of FLOAT for money columns Data doesn't clean itself. But a good pipeline makes sure you know exactly what you changed and why. #DataAnalytics #SQL #ETL #DataEngineering #DataCleaning #PortfolioProject #DataAnalyst #HRAnalytics #SQLServer #BusinessIntelligence
To view or add a comment, sign in
-
-
💬 SQL Challenge of the Day Problem: Given a table named "sales" with the following columns: - order_id (unique identifier for each order) - order_date (date of the order) - revenue (revenue generated by the order) Write a SQL query to calculate the 30-day running total revenue for each order, considering the current order and the revenue from the previous 29 days. Query: ```sql SELECT order_id, order_date, revenue, SUM(revenue) OVER (ORDER BY order_date ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) AS running_total FROM sales ``` Answer: The SQL query calculates the 30-day running total revenue for each order by using a window function to sum the revenue of the current order and the previous 29 days. Explanation: - The query uses the SUM() window function with the OVER clause to calculate the running total. - The PARTITION BY clause is not needed as we want the running total for all orders combined. - The ORDER BY clause orders the rows by the order_date. - The ROWS BETWEEN 29 PRECEDING AND CURRENT ROW specifies the window frame to consider for the running total calculation. Example: Consider the "sales" table: | order_id | order_date | revenue | |----------|------------|---------| | 1 | 2021-01-01 | 100 | | 2 | 2021-01-05 | 150 | | 3 | 2021-01-10 | 200 | | 4 | 2021-01-15 | 120 | The output of the query would be: | order_id | order_date | revenue | running_total | |----------|------------|---------|---------------| | 1 | 2021-01-01 | 100 | 100 | | 2 | 2021-01-05 | 150 | 250 | | 3 | 2021-01-10 | 200 | 450 | | 4 | 2021-01-15 | 120 | 570 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
💬 SQL Challenge of the Day Problem: You have a table named "orders" that contains order information with columns: order_id, customer_id, order_date, and order_amount. Write a SQL query to calculate the running total of order amounts for each customer, ordered by the order_date. Query: ```sql SELECT order_id, customer_id, order_date, order_amount, SUM(order_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_total FROM orders ``` Answer: The SQL query calculates the running total of order amounts for each customer, ordered by the order_date. Explanation: The query uses a window function with the SUM() function to calculate the running total of order amounts for each customer. The PARTITION BY clause is used to reset the running total for each customer. The ORDER BY clause ensures that the running total is calculated based on the order_date. Example: Consider the "orders" table: | order_id | customer_id | order_date | order_amount | |----------|-------------|------------|--------------| | 1 | 101 | 2022-01-01 | 100 | | 2 | 102 | 2022-01-02 | 200 | | 3 | 101 | 2022-01-03 | 150 | | 4 | 102 | 2022-01-04 | 300 | The query will return: | order_id | customer_id | order_date | order_amount | running_total | |----------|-------------|------------|--------------|---------------| | 1 | 101 | 2022-01-01 | 100 | 100 | | 3 | 101 | 2022-01-03 | 150 | 250 | | 2 | 102 | 2022-01-02 | 200 | 200 | | 4 | 102 | 2022-01-04 | 300 | 500 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
💬 SQL Challenge of the Day Problem: You have a table named "orders" that contains order information for customers. Each order has a unique order_id, customer_id, order_date, and total_amount. Write a SQL query to calculate the running total of total_amount for each customer, ordered by order_date. Query: ```sql SELECT order_id, customer_id, order_date, total_amount, SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS running_total FROM orders ``` Answer: The SQL query calculates the running total of total_amount for each customer based on the order_date. Explanation: The query uses a window function with the PARTITION BY clause to partition the data by customer_id and order the results by order_date. The SUM window function then calculates the running total of total_amount within each partition. Example: Consider the following "orders" table: | order_id | customer_id | order_date | total_amount | |----------|-------------|------------|-------------| | 1 | 100 | 2022-01-01 | 50 | | 2 | 100 | 2022-01-02 | 30 | | 3 | 200 | 2022-01-01 | 100 | | 4 | 100 | 2022-01-03 | 20 | The query will output: | order_id | customer_id | order_date | total_amount | running_total | |----------|-------------|------------|-------------|---------------| | 1 | 100 | 2022-01-01 | 50 | 50 | | 2 | 100 | 2022-01-02 | 30 | 80 | | 4 | 100 | 2022-01-03 | 20 | 100 | | 3 | 200 | 2022-01-01 | 100 | 100 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
💬 SQL Challenge of the Day Problem: Given a table "orders" with the following columns: order_id, customer_id, order_date, and total_amount. Write a SQL query to calculate the cumulative sum of total_amount for each customer_id ordered by order_date. Query: ```sql SELECT order_id, customer_id, order_date, total_amount, SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date) AS cumulative_total FROM orders; ``` Answer: The SQL query calculates the cumulative sum of total_amount for each customer_id based on the order_date. Explanation: The query uses a window function with the `PARTITION BY` clause to partition the data by customer_id and then calculates the cumulative sum of total_amount ordered by the order_date. Example: Consider the following "orders" table: | order_id | customer_id | order_date | total_amount | |----------|-------------|------------|--------------| | 1 | 101 | 2022-01-05 | 50 | | 2 | 102 | 2022-01-10 | 30 | | 3 | 101 | 2022-01-15 | 70 | | 4 | 103 | 2022-01-20 | 40 | The query will output: | order_id | customer_id | order_date | total_amount | cumulative_total | |----------|-------------|------------|--------------|------------------| | 1 | 101 | 2022-01-05 | 50 | 50 | | 3 | 101 | 2022-01-15 | 70 | 120 | | 2 | 102 | 2022-01-10 | 30 | 30 | | 4 | 103 | 2022-01-20 | 40 | 40 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
💬 SQL Challenge of the Day Problem: You are given a table named "sales_data" with the following columns: - order_id: The unique identifier of an order - order_date: The date when the order was placed - revenue: The amount of revenue generated by the order Write a SQL query to calculate the 7-day rolling average revenue for each order, considering only the orders from the previous 7 days (including the current day). Query: ```sql SELECT order_id, order_date, revenue, AVG(revenue) OVER ( ORDER BY order_date RANGE BETWEEN INTERVAL '6' DAY PRECEDING AND CURRENT ROW ) AS rolling_avg_revenue FROM sales_data; ``` Answer: The SQL query calculates the 7-day rolling average revenue for each order, considering only the orders from the previous 7 days (including the current day). Explanation: - The query uses a window function with the AVG() function to calculate the rolling average revenue. - The OVER clause is used to define the window frame as the range between 6 days before the current row and the current row. - This allows us to calculate the average revenue over a moving 7-day window for each order. Example: Consider the "sales_data" table: | order_id | order_date | revenue | |----------|------------|---------| | 1 | 2022-01-01 | 100 | | 2 | 2022-01-02 | 150 | | 3 | 2022-01-03 | 200 | | 4 | 2022-01-04 | 180 | | 5 | 2022-01-05 | 220 | The query will output: | order_id | order_date | revenue | rolling_avg_revenue | |----------|------------|---------|---------------------| | 1 | 2022-01-01 | 100 | 100.00 | | 2 | 2022-01-02 | 150 | 125.00 | | 3 | 2022-01-03 | 200 | 150.00 | | 4 | 2022-01-04 | 180 | 157.50 | | 5 | 2022-01-05 | 220 | 170.00 | #Hashtags #PowerBIChallenge #PowerInterview #LearnPowerBi #LearnSQL #TechJobs #DataAnalytics #DataScience #BigData #DataAnalyst #MachineLearning #Python #SQL #Tableau #DataVisualization #DataEngineering #ArtificialIntelligence #CloudComputing #BusinessIntelligence #Data
To view or add a comment, sign in
-
Pivoting rows to columns is one of those SQL skills that immediately changes how you think about reporting. Instead of exporting raw data and reshaping it in Excel, you can produce a clean, wide-format summary table directly in your query — one row per entity, one column per category, ready for any dashboard or report. The CASE WHEN approach works in every database and makes the logic completely transparent. The native PIVOT operator in SQL Server keeps things concise. Dynamic pivots handle the cases where your data shape changes over time. Once this technique clicks, you will find yourself reaching for it constantly — in sales reports, HR analytics, survey summaries, and anywhere else your data lives in a tall format but needs to be read in a wide one. Read the full post here: https://lnkd.in/e4wt4tem #SQL #DataScience #DataAnalysis #DataEngineering #Analytics #BusinessIntelligence
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development