dsvs50005 2017-01-17 15:00
浏览 26
已采纳

我应该分开普通的SQL查询或CTE并将其放在变量中吗?

I have some sql queries that have some common parts in them. The CTE used to get the data about houses look similar in both queries.

const GetUserListSQL = `
  WITH "HouseData" AS (
    SELECT
      "UserId",
      json_object_agg(
        "Id",
        (SELECT x FROM (SELECT
          "Price",
          "Area",
          "Address"
        ) x)
      ) AS "HouseMap"
    FROM "Houses"
    GROUP BY "UserId"
  )
  SELECT
    "Id",
    "Name",
  FROM "Users"
  LEFT JOIN "HouseData" ON "Users"."Id" = "HouseData"."UserId"
`


const GetUserSQL = `
  WITH "HouseData" AS (
    SELECT
      json_object_agg(
        "Id",
        (SELECT x FROM (SELECT
          "Price",
          "Area",
          "Address"
        ) x)
      ) AS "HouseMap"
    FROM "Houses"
    WHERE "UserId" = $1
  )
  SELECT
    "Id",
    "Name",
  FROM "Users"
  LEFT JOIN "HouseData" ON TRUE
  WHERE "Users"."Id" = $1
`

Should I make the CTE to get house data or user fields a separate const? Will it increase reusability (like you only need to add fields in one place)? Will it reduce readability (it looks kind of weird somehow). Someone suggested that I wrote those SQL like below:

const HouseDataCTE = `
  SELECT
    "UserId",
    json_object_agg(
      "Id",
      (SELECT x FROM (SELECT
        "Price" 
        "Area",
        "Address"
      ) x)
    ) AS "HouseMap"
  FROM "Houses"
`

const UserCommonSQL = `
  SELECT
    "Id",
    "Name"
  FROM "Users"
`

const GetUserListSQL = `
  WITH "HouseData" AS (
    ` + HouseDataCTE + `
    GROUP BY "UserId"
  )
  ` + UserCommonSQL + `
  LEFT JOIN "HouseData" ON "Users"."Id" = "HouseData"."UserId"
`

const GetUserSQL = `
  WITH "HouseData" AS (
    ` + HouseDataCTE + `
    WHERE "UserId" = $1
  )
  ` + UserCommonSQL + `
  LEFT JOIN "HouseData" ON TRUE
  WHERE "Users"."Id" = $1
`

If I write it like that I feel like I have to jump back and forth to inspect the SQL (the queries are not placed close together), which is kind of annoying in my opinion. Are there any convention or best practices for these kinds of situation ?

EDIT: The sql queries above are shorter version of an actual long SQL queries I'm using. The longer version has 4-5 CTEs from different tables and the common fields for "Users" are about 20 columns. What should be done in that situation then?

  • 写回答

1条回答 默认 最新

  • dreamlife2014 2017-01-17 15:06
    关注

    in my experience the CTEs tended to get jumbled in each SP. Is it possible to leverage a view on the CTE instead n your use cases? That would clean up your documentation and code maintenance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题