久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使 SQL 查詢中的重復自定義表達式可維護

How to make repeating custom expressions in SQL queries maintainable(如何使 SQL 查詢中的重復自定義表達式可維護)
本文介紹了如何使 SQL 查詢中的重復自定義表達式可維護的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

想象一下下面的簡單查詢,我得到了過去 3 天內創建的所有用戶的列表,邏輯或示例并不重要

Imagine the following simple query, where I get a list of all users that were created within the last 3 days, the logic or example is not important

SELECT
    ...
    , DATEDIFF(DAY, U.DateCreated, GETUTCDATE())
    ...
FROM
    dbo.AspNetUsers U
WHERE
    DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3

我重復了一些代碼 DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) <3,當我有更復雜的上述示例時,我不想維護兩次或多次需要該邏輯.

I've repeated some code DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3 which, when I have much more complicated examples of the above I don't want to maintain twice or however many times I need that logic.

考慮到性能,應該如何處理這個問題?

How should one go about dealing with this with performance in mind?

謝謝

推薦答案

如果您考慮到性能,那么您最好在需要時重復表達式.具體來說,不要試圖將它們放在用戶定義的函數中.眾所周知,它們會使 SQL Server 中的查詢變慢.

If you have performance in mind, then you'd better repeat expressions when needed. Specifically, don't try to put them in a user-defined functions. They are known to make queries slow in SQL Server.

話雖如此,SQL Server 中至少有兩種方法可以在不影響性能的情況下使查詢更具可讀性:

Having said that, there are at least two methods in SQL Server to make queries more readable without affecting the performance:

  1. CTE
  2. 交叉申請

CTE 使用示例:

WITH
CTE
AS
(

    SELECT
        ...
        , DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) AS CalculatedColumn
        ...
    FROM
        dbo.AspNetUsers U

)
SELECT
    ...
    CalculatedColumn
    ...
FROM CTE
WHERE
    CalculatedColumn < 3
;

使用CROSS APPLY的例子:

而不是在以下查詢中重復部分公式:

Instead of repeating parts of the formula in the following query:

SELECT
    ColA + ColB AS ColSum
    ,ColA - ColB AS ColDiff
    ,(ColA + ColB) * (ColA - ColB) AS Result
    ,(ColA + ColB) * (ColA - ColB) * 100.0 AS Percentage
FROM Table

你可以用CROSS APPLY這樣寫:

SELECT
    ColSum
    ,ColDiff
    ,Result
    ,Result * 100.0 AS Percentage
FROM
    Table
    CROSS APPLY
    (
        SELECT
            ColA + ColB AS ColSum
            ,ColA - ColB AS ColDiff
    ) AS A1
    CROSS APPLY
    (
        SELECT ColSum * ColDiff AS Result
    ) AS A2

<小時>

順便說一下性能,


By the way, speaking about performance,

WHERE DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3

很糟糕,因為它不能在 DateCreated 上使用索引(因為您將列包裝在一個函數中).

is terrible, because it can't use index on DateCreated (because you wrapped the column in a function).

你最好把它改寫成這樣

WHERE U.DateCreated > DATEADD(DAY, -3, GETUTCDATE())

這篇關于如何使 SQL 查詢中的重復自定義表達式可維護的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品网页 | 亚洲国产精品一区二区久久 | 亚洲国产精品一区二区三区 | 色爱综合网| 成人激情视频在线播放 | 紧缚调教一区二区三区视频 | 欧美日韩久久 | 四虎影视一区二区 | 日韩免费福利视频 | 99reav| 欧美日韩视频 | 国内精品久久久久 | 国产日韩欧美一区二区 | 久久精品视频在线观看 | 久久精品国产精品青草 | 欧美精品一区在线 | 欧美一区二区三区在线观看 | 欧美中文在线 | 一级毛片视频 | 日韩精品在线一区 | 欧美极品在线播放 | 国产成人精品久久二区二区91 | 国产精品成人在线 | 中文字幕亚洲一区二区三区 | 久久色视频 | 亚洲精品av在线 | 第四色狠狠 | 91亚洲欧美 | 国产综合在线视频 | 羞羞视频在线网站观看 | 69xxx免费| 久久久久久国产精品 | 天天操网 | 日韩成人性视频 | 久久视频一区 | 91av免费观看 | 国内精品久久久久久 | 久久精品免费观看 | 欧美1—12sexvideos| 午夜精品视频 | 日本a级大片 |