Changes between Version 1 and Version 2 of StorageConfigurationOptimizationScore


Ignore:
Timestamp:
01/29/26 03:52:02 (10 days ago)
Author:
233051
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • StorageConfigurationOptimizationScore

    v1 v2  
    1 = Complex DB Reports (SQL, Stored Procedures, Relational Algebra) =
    2 
    3 ===  Storage Configuration Optimization Score ===
     1==  Storage Configuration Optimization Score ==
    42
    53Report on which storage configurations (SSD + HDD combinations) provide the best balance between speed, reliability, cost-efficiency, failure rate and user approval within different build price ranges.
     
    86
    97{{{
     8CREATE OR REPLACE FUNCTION get_report_storage_optimization()
     9    RETURNS TABLE (
     10                      config_type TEXT,
     11                      ssd_brand TEXT,
     12                      builds_count BIGINT,
     13                      avg_storage_cost NUMERIC,
     14                      avg_total_capacity_gb NUMERIC,
     15                      avg_build_rating NUMERIC,
     16                      storage_cost_pct NUMERIC,
     17                      optimization_score NUMERIC
     18                  )
     19    LANGUAGE sql
     20AS $$
    1021WITH storage_configs AS (
    11   SELECT
    12     b.id AS build_id,
    13     b.total_price,
    14     ssd.brand AS ssd_brand,
    15     ssd.capacity AS ssd_capacity,
    16     ssd.price AS ssd_price,
    17     hdd.capacity AS hdd_capacity,
    18     hdd.price AS hdd_price,
    19     CASE
    20       WHEN hdd.capacity IS NULL THEN 'SSD-Only'
    21       WHEN ssd.capacity < 512 THEN 'SSD-Boot-HDD-Storage'
    22       ELSE 'SSD-Primary-HDD-Archive'
    23     END AS config_type
    24   FROM build b
    25   JOIN build_component bc_ssd ON b.id = bc_ssd.build_id
    26   JOIN components ssd ON bc_ssd.component_id = ssd.id
    27   LEFT JOIN build_component bc_hdd ON b.id = bc_hdd.build_id
    28   LEFT JOIN components hdd ON bc_hdd.component_id = hdd.id AND hdd.type = 'HDD'
    29   WHERE ssd.type = 'SSD'
    30     AND b.is_approved = TRUE
    31     AND b.created_at >= CURRENT_DATE - INTERVAL '1 year'
     22    SELECT
     23        b.id AS build_id,
     24        b.total_price,
     25
     26        ssd_comp.brand AS ssd_brand,
     27        ssd_storage.capacity AS ssd_capacity,
     28        ssd_comp.price AS ssd_price,
     29
     30        hdd_storage.capacity AS hdd_capacity,
     31        hdd_comp.price AS hdd_price,
     32
     33        CASE
     34            WHEN hdd_storage.capacity IS NULL THEN 'SSD-Only'
     35            WHEN ssd_storage.capacity < 512 THEN 'SSD-Boot-HDD-Storage'
     36            ELSE 'SSD-Primary-HDD-Archive'
     37            END AS config_type
     38    FROM build b
     39             JOIN build_component bc_ssd ON b.id = bc_ssd.build_id
     40             JOIN components ssd_comp ON bc_ssd.component_id = ssd_comp.id
     41             JOIN storage ssd_storage ON ssd_storage.component_id = ssd_comp.id
     42
     43             LEFT JOIN build_component bc_hdd ON b.id = bc_hdd.build_id
     44             LEFT JOIN components hdd_comp ON bc_hdd.component_id = hdd_comp.id
     45             LEFT JOIN storage hdd_storage ON hdd_storage.component_id = hdd_comp.id
     46
     47    WHERE ssd_comp.type = 'storage'
     48      AND b.is_approved = TRUE
     49      AND b.created_at >= CURRENT_DATE - INTERVAL '1 year'
     50
     51      AND (
     52        ssd_storage.type ILIKE '%nvme%'
     53            OR ssd_storage.type ILIKE '%ssd%'
     54            OR ssd_storage.type ILIKE '%m.2%'
     55            OR ssd_storage.form_factor ILIKE '%m.2%'
     56        )
     57      AND (
     58        hdd_storage.component_id IS NULL
     59            OR hdd_storage.type ILIKE '%hdd%'
     60            OR hdd_storage.form_factor ILIKE '%3.5%'
     61        )
    3262),
    33 config_performance AS (
    34   SELECT
    35     sc.config_type,
    36     sc.ssd_brand,
    37     COUNT(sc.build_id) AS builds_count,
    38     AVG(sc.ssd_price + COALESCE(sc.hdd_price, 0)) AS avg_storage_cost,
    39     AVG(sc.ssd_capacity + COALESCE(sc.hdd_capacity, 0)) AS avg_total_capacity,
    40     AVG(rb.value) AS avg_build_rating,
    41     AVG((sc.ssd_price + COALESCE(sc.hdd_price, 0)) / sc.total_price) AS storage_cost_ratio
    42   FROM storage_configs sc
    43   LEFT JOIN rating_build rb ON sc.build_id = rb.build_id
    44   GROUP BY sc.config_type, sc.ssd_brand
    45   HAVING COUNT(sc.build_id) >= 5
    46 )
     63     config_performance AS (
     64         SELECT
     65             sc.config_type,
     66             sc.ssd_brand,
     67             COUNT(sc.build_id) AS builds_count,
     68             AVG(sc.ssd_price + COALESCE(sc.hdd_price, 0)) AS avg_storage_cost,
     69             AVG(sc.ssd_capacity + COALESCE(sc.hdd_capacity, 0)) AS avg_total_capacity,
     70             AVG(rb.value) AS avg_build_rating,
     71             AVG((sc.ssd_price + COALESCE(sc.hdd_price, 0)) / NULLIF(sc.total_price, 0)) AS storage_cost_ratio
     72         FROM storage_configs sc
     73                  LEFT JOIN rating_build rb ON sc.build_id = rb.build_id
     74         GROUP BY sc.config_type, sc.ssd_brand
     75         HAVING COUNT(sc.build_id) >= 2
     76     )
    4777SELECT
    48   config_type,
    49   ssd_brand,
    50   builds_count,
    51   ROUND(avg_storage_cost, 2) AS avg_storage_cost,
    52   ROUND(avg_total_capacity, 0) AS avg_total_capacity_gb,
    53   ROUND(avg_build_rating, 2) AS avg_build_rating,
    54   ROUND(storage_cost_ratio * 100, 1) AS storage_cost_pct,
    55   ROUND(
    56     (avg_build_rating / 5.0 * 40) +
    57     (avg_total_capacity / avg_storage_cost * 0.5) +
    58     ((1 - storage_cost_ratio) * 30) +
    59     (LN(builds_count) * 5),
    60     2
    61   ) AS optimization_score
     78    config_type,
     79    ssd_brand,
     80    builds_count,
     81    ROUND(CAST(avg_storage_cost AS numeric), 2) AS avg_storage_cost,
     82    ROUND(CAST(avg_total_capacity AS numeric), 0) AS avg_total_capacity_gb,
     83    ROUND(CAST(COALESCE(avg_build_rating, 0) AS numeric), 2) AS avg_build_rating,
     84    ROUND(CAST(COALESCE(storage_cost_ratio, 0) * 100 AS numeric), 1) AS storage_cost_pct,
     85    ROUND(
     86            CAST(
     87                    (COALESCE(avg_build_rating, 0) / 5.0 * 40) +
     88                    (avg_total_capacity / NULLIF(avg_storage_cost, 0) * 0.5) +
     89                    ((1 - COALESCE(storage_cost_ratio, 0)) * 30) +
     90                    (LN(CAST(builds_count + 1 AS numeric)) * 5)
     91                AS numeric),
     92            2
     93    ) AS optimization_score
    6294FROM config_performance
    6395ORDER BY optimization_score DESC, builds_count DESC
    6496LIMIT 15;
     97$$;
    6598}}}
    6699