Search code examples
sqlsql-server-2008-r2sql-optimization

Sql query optimization for calculating ratio


I have 2 tables (it is only part of table columns that are needed for this query):

Items: Id int, OwnerId int
ItemsRelations: RelationId int, ItemId Int (FK Items(Id))

For each OwnerId i need to calculate result of ratio function: Count of user elements that have links to ItemsRelationstable / count of all user elements.

I wrote this query, but it is slow and not optimal. Is there easier and faster method to do that?

SELECT OwnerId , (100 * SUM(HasRelation))/COUNT(Id) AS Ratio
FROM (
  SELECT 
    oi.OwnerId,
    oi.Id,
    (CASE WHEN SUM(ir.Id) > 0 THEN 1 ELSE 0 END) HasRelation
  FROM Items AS oi
  LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
  GROUP BY oi.Id, oi.OwnerId) tempTab 
GROUP BY OwnerId 

Solution

  • SELECT oi.OwnerId, Ratio = 1.0 * Count(ir.ItemId) / Count(*)
    FROM Items AS oi
    LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
    GROUP BY oi.OwnerId
    

    Notes:

    • 1.0 is added to convert the expression into a float result
    • Count(ir.ItemId) skips NULLS, so it counts only those that have a relationship

    If Items to ItemsRelations is one to many, then you may need to DISTINCT it

    SELECT oi.OwnerId, Ratio = 1.0 * Count(DISTINCT ir.ItemId) / Count(DISTINCT oi.Id)
    FROM Items AS oi
    LEFT JOIN ItemsRelations AS ir ON ir.ItemId  = oi.Id
    GROUP BY oi.OwnerId