如何使 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 查询中的重复自定义表达式可维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)