• Advanced SQL: Relational division in jOOQ


     

     
     
     
     
     
     
    i
     
    Rate This

    Relational algebra has its treats. One of the most academic features is the relational division. It is hardly ever used, but comes in handy every now and then. And when you need it, you’ll probably hate yourself for having slept during the relevant classes at the university.

    What is relational division?

    Relational division is the inverse of a cross join operation. The following is an approximate definition of a relational division:

    Assume the following cross join / cartesian product 
    C = A × B 
    
    Then it can be said that 
    A = C ÷ B 
    B = C ÷ A

    What does it mean, typically?

    Let’s have a look at the sample provided on Wikipedia:

    Wikipedia example of a relational division

    Wikipedia example of a relational division

    This looks sensible. The division of Completed ÷ DBProject leads to a list of students that have completed all projects.

    Now how to phrase that in SQL??

    That’s not so simple as it looks. The most commonly documented solution involves a doubly-nested select statement using anti-joins. In human language (using double negative), it is like Fred and Sarah saying “there is no DBProject that we have not Completed“. Or in SQL:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    SELECT DISTINCT "c1".Student FROM Completed "c1"
    WHERE NOT EXISTS (
      SELECT 1 FROM DBProject
      WHERE NOT EXISTS (
        SELECT 1 FROM Completed "c2"
        WHERE "c2".Student = "c1".Student
        AND "c2".Task = DBProject.Task
      )
    )

    Now, no one sane wants to remember this just for the 1-2 times in a SQL developer’s life that they actually need it. So they use jOOQ, which wraps up the above monster in a concise syntax:

    1
    2
    3
    4
    5
    create.select().from(
      Completed.divideBy(DBProject)
               .on(Completed.Task.equal(DBProject.Task))
               .returning(Completed.Student)
    );

    Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(…) and returning(…) clauses.

    More information

    For more information about relational division and some nice, real-life examples, see

  • 相关阅读:
    2021/9/20 开始排序算法
    快速排序(自己版本)
    2021/9/17(栈实现+中后缀表达式求值)
    2021/9/18+19(中缀转后缀 + 递归 迷宫 + 八皇后)
    20212021/9/13 稀疏数组
    2021/9/12 线性表之ArrayList
    开发环境重整
    Nginx入门
    《财富的帝国》读书笔记
    Linux入门
  • 原文地址:https://www.cnblogs.com/sthinker/p/6078638.html
Copyright © 2020-2023  润新知