Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

abeer93/reports

Repository files navigation

Reporting API - README

Overview

This project provides optimized reporting APIs for generating sales and category reports based on MySQL 8.0. The focus was on optimizing query performance, database schema improvements, and efficient data seeding for large-scale testing.

Implemented Reports

  1. Monthly Sales by Region

    • Aggregates total sales and order count per region, grouped by year and month.
    • Optimized for performance using indexing and schema enhancements.
  2. Top Categories by Store

    • Retrieves the top 10 highest-selling product categories per store.
    • Utilizes MySQL's RANK() window function for efficient ranking.

Database Enhancements

1. Schema Optimizations

  • Added appropriate indexes to speed up query performance.
  • Optimized column data types for better storage efficiency.
  • Ensured foreign keys and constraints were properly defined to maintain data integrity.
  • Normalize structure by removing redundant store_storeId and product_productId in orders.

2. Indexing for Performance

Indexes were added to frequently queried columns:

CREATE INDEX idx_order_date ON orders(order_date);
CREATE INDEX idx_store_region ON stores(region_id);
CREATE INDEX idx_product_category ON products(category_id);

3. Efficient Data Seeding

A seeding mechanism was implemented to generate realistic and large-scale test data. This helped in testing the efficiency of queries under high data volume.

  • Seeded users, stores, products, and orders tables.
  • Used bulk inserts for fast data generation.

API Endpoints

1. Fetch Monthly Sales by Region

Endpoint: GET /api/reports/monthly-sales Parameters:

  • startDate (YYYY-MM-DD)
  • endDate (YYYY-MM-DD)

Response:

[
  {
    "year": 2024,
    "month": 1,
    "region_id": 2,
    "total_sales": 15000.50,
    "number_of_orders": 230
  }
]

2. Fetch Top Categories by Store

Endpoint: GET /api/reports/top-categories Parameters:

  • startDate (YYYY-MM-DD)
  • endDate (YYYY-MM-DD)

Response:

[
  {
    "store_id": 1,
    "category_id": 5,
    "total_sales": 32000.75,
    "rank_within_store": 1
  }
]

Query Optimizations

Monthly Sales Query

Optimized query using indexes and avoiding unnecessary functions:

SELECT
    YEAR(order_date) AS year,
    MONTH(order_date) AS month,
    s.region_id,
    SUM(o.quantity * o.unit_price) AS total_sales,
    COUNT(o.order_id) AS number_of_orders
FROM orders o
JOIN stores s ON o.store_id = s.store_id
WHERE o.order_date BETWEEN :startDate AND :endDate
GROUP BY year, month, region_id;

Top Categories Query

Efficient ranking using RANK():

WITH ranked_categories AS (
    SELECT
        o.store_id,
        p.category_id,
        SUM(o.quantity * o.unit_price) AS total_sales,
        RANK() OVER (PARTITION BY o.store_id ORDER BY SUM(o.quantity * o.unit_price) DESC) AS rank_within_store
    FROM orders o
    JOIN products p ON o.product_id = p.product_id
    WHERE o.order_date BETWEEN :startDate AND :endDate
    GROUP BY o.store_id, p.category_id
)
SELECT * FROM ranked_categories WHERE rank_within_store <= 10;

Setup Instructions

  1. Run Migrations
php app.php migrate
  1. Seed the Database
php app.php seed:database
  1. Start the Server
php app.php serve

Conclusion

This project successfully implemented optimized queries and database improvements for efficient reporting. Future enhancements could include caching for frequently accessed reports and additional analytics features.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published