简体   繁体   English

SSRS计算字段与SQL Server存储过程中的算法

[英]Arithmetic in SSRS calculated field vs. SQL Server stored procedure

I am designing a SSRS report that uses a stored procedure as the dataset. 我正在设计一个使用存储过程作为数据集的SSRS报告。 The stored procedure currently looks like this: 存储过程目前看起来像这样:

SELECT
    sq.Name,
    SUM(PassPartA) AS 'TotalPassPartA'
    SUM(PassPartB) AS 'TotalPassPartB'
    COUNT(*) AS 'TotalTests'
FROM 
    (
    -- Sub-query here
    ) AS sq
GROUP BY
    sq.Name

The results will look something like this: 结果将如下所示:

+-------------------------------------------------------+
| Name    | TotalPassPartA | TotalPassPartB | TotalTests|
+-------------------------------------------------------+
| Eddard  |             24 |             23 |        25 |
| Benjen  |              2 |              3 |         4 |
| Lyanna  |             10 |             10 |        10 |
---------------------------------------------------------

This gets me what I need from the database, where I can then get additional information needed for the report with arithmetic and logical expressions in calculated fields . 这从数据库中获取了我需要的东西,然后我可以在计算字段中获得具有算术和逻辑表达式的报告所需的其他信息。

Example of some additional information for report: 报告的一些其他信息的示例:

  • PartAPassPercentage = TotalPassPartA / TotalTests PartAPassPercentage = TotalPassPartA / TotalTests
  • PartBPassPercentage = TotalPassPartB / TotalTests PartBPassPercentage = TotalPassPartB / TotalTests
  • IsPartAPassedAt90Percent = PartAPassPercentage >= 0.9 IsPartAPassedAt90Percent = PartAPassPercentage> = 0.9
  • IsPartBPassedAt90Percent = PartBPassPercentage >= 0.9 IsPartBPassedAt90Percent = PartBPassPercentage> = 0.9
  • PartAMagicNumber = IIF(IsPartAPassedAt90Percent, 1, 0) PartAMagicNumber = IIF(IsPartAPassedAt90Percent, 1, 0)
  • PartAMagicNumber = IIF(IsPartBPassedAt90Percent, 1, 0) * 2 PartAMagicNumber = IIF(IsPartBPassedAt90Percent, 1, 0) * 2
  • TotalMagicNumber = PartAMagicNumber + PartBMagicNumber + 1 TotalMagicNumber = PartAMagicNumber + PartBMagicNumber + 1

This approach works, but I wonder if doing the "work" of the arithmetic and logic would be more efficient in the SQL Server stored procedure. 这种方法有效,但我想知道在SQL Server存储过程中,算术和逻辑的“工作”是否更有效。 All of the columns I would need could use "CASE" or "IF/ELSE" statements to return what is needed. 我需要的所有列都可以使用“CASE”或“IF / ELSE”语句来返回所需的内容。

Another consideration that I may be overlooking is code maintenance. 我可能忽略的另一个考虑因素是代码维护。 Are there any advantages to having the "work" done in the calculated fields or the stored procedure? 在计算字段或存储过程中完成“工作”有什么好处吗? (This may just be an opinion/preference though...) (这可能仅仅是一个意见/偏好......)

I am not familiar or experienced with performance tools to test the results separately, and I am working on a DEV machine with decent hardware, and I would assume performance would be better/worse when deployed to a server with better/worse hardware and other processes running. 我不熟悉或经验不足以单独测试结果的性能工具,我正在开发具有合适硬件的DEV机器,我认为当部署到具有更好/更差硬件和其他进程的服务器时,性能会更好/更差运行。

Which approach would have better performance and is the best place to do the "work". 哪种方法会有更好的表现,是做“工作”的最佳场所。

It is good to go for calculated fields in the stored procedure itself. 最好去存储过程本身的计算字段。 If you use calculated fields in the SSRS and it is going to take a little time if the data set is huge. 如果您在SSRS中使用计算字段,并且如果数据集很大,则需要一些时间。

My suggestion is doing at stored procedure is much better. 我的建议是在存储过程中做得好多了。

Simple things, such as plain percentage, can be implemented as calculated columns in the report. 简单的事物(例如普通百分比)可以作为报告中的计算列实现。 This way you can reduce network IO for your database instance. 这样,您可以减少数据库实例的网络IO。

With more complex stuff, however, it's not that obvious. 然而,对于更复杂的东西,它并不那么明显。 Just think - what kind / amount of work will be needed when, for example: 试想一下 - 例如:当需要什么样的/大量的工作时:

  • Definitions of some of these derived columns will change (happens all the time); 其中一些派生列的定义会发生变化(一直发生);
  • Data availability for some of these columns will become dependant on, say, security permissions of a particular user who is running the report; 其中一些列的数据可用性将取决于运行报告的特定用户的安全权限;
  • Same as above, but configuration parameters are taken from another database or server to which user should have no direct access. 与上面相同,但配置参数取自用户无法直接访问的其他数据库或服务器。

In short, try to estimate what can be considered common knowledge and fairly static, and what depends on the business requirements, and as such can evolve with time. 简而言之,尝试估计什么可以被认为是常识和相当静态,什么取决于业务需求,因此可以随着时间的推移而发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM