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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端