General rules for simplifying SQL statements

前端 未结 8 1559
心在旅途
心在旅途 2020-12-22 17:15

I\'m looking for some \"inference rules\" (similar to set operation rules or logic rules) which I can use to reduce a SQL query in complexity or size. Does there exist some

8条回答
  •  余生分开走
    2020-12-22 17:46

    I like to replace all sort of subselect by join query.

    This one is obvious :

    SELECT  *
    FROM    mytable mo
    WHERE   EXISTS
            (
              SELECT  *
              FROM    othertable o
              WHERE   o.othercol = mo.col
            )
    

    by

    SELECT  mo.*
    FROM    mytable mo inner join othertable o on o.othercol = mo.col
    

    And this one is under estimate :

    SELECT  *
    FROM    mytable mo
    WHERE   NOT EXISTS
            (
              SELECT  *
              FROM    othertable o
              WHERE   o.othercol = mo.col
            )
    

    by

    SELECT  mo.*
    FROM    mytable mo left outer join othertable o on o.othercol = mo.col
    WHERE   o.othercol is null
    

    It could help the DBMS to choose the good execution plan in a big request.

提交回复
热议问题