简体   繁体   English

何时使用临时SQL表与数据表

[英]When to use Temporary SQL Tables vs DataTables

I don't know whether it is better to use temporary tables in SQL Server or use the DataTable in C# for a report. 我不知道在SQL Server中使用临时表还是在C#中为报表使用DataTable更好。 Here is the scope of the report: it will be copied into a workbook with about 10 worksheets - each worksheet containing about 1000 rows and about 30 columns so it's a lot of data. 这是报告的范围:它将被复制到包含约10个工作表的工作簿中-每个工作表包含约1000行和约30列,因此其中包含大量数据。 There is some guidance out there but I could not find anything specific regarding the amount of data that is too much for a DataTable. 有一些指导,但是我找不到关于DataTable太多数据的具体信息。 According to https://msdn.microsoft.com/en-us/library/system.data.datatable.aspx , 16M rows but my data set seems unwieldy considering the number of columns I have. 根据https://msdn.microsoft.com/zh-CN/library/system.data.datatable.aspx的数据,行数为1600万,但考虑到我拥有的列数,我的数据集显得笨拙。 Plus, I will either have to make multiple SQL queries to collect the data in my report or try to write a stored procedure in SQL to collect that data. 另外,我将不得不进行多个SQL查询以收集报告中的数据,或者尝试用SQL编写存储过程来收集该数据。 How do I figure out this quandary? 我如何解决这个难题?

There's a lot of missing context here - how is this report going to be accessed and run? 这里缺少很多上下文-如何访问和运行此报告? Is this going to run as a scripted event every day? 这会每天作为脚本事件运行吗?

Have you considered SSRS? 您考虑过SSRS吗?

In my opinion it's best to abstract away your business logic by creating Views or Stored Procedures in the database. 我认为最好是通过在数据库中创建视图或存储过程来抽象您的业务逻辑。 Stored Procedures would probably be the way to go but it really depends on your specific environment. 存储过程可能是可行的方法,但实际上取决于您的特定环境。 Then you can point whatever tools you want to use at the database object. 然后,您可以将想要使用的任何工具指向数据库对象。 This has several advantages: 这有几个优点:

  • if you end up having different versions or different formats of the report, and your logic ever changes, you can update the logic in one place rather than many. 如果最终报表的版本不同或格式不同,并且逻辑不断变化,则可以在一个地方而不是多个地方更新逻辑。

  • your code is simpler and cleaner, typically: 您的代码更加简洁明了,通常是:

select v.col1, v.col2, v.col3 from MY_VIEW v where v.date between @startdate and @enddate

I assume your 10 spreadsheets are going to be something like 我假设您的10个电子表格将类似于

Summary Page | 摘要页面| Department 1 | 部门1 | Department 2 | 部门2 | ... ...

So you could make a generalized View or SP, create a master spreadsheet linked to the db object that pulls all the relevant data from SQL, and use Pivot Tables or filters or whatever else you want, and use that to generate your copies that get sent out. 因此,您可以制作一个通用的View或SP,创建链接到db对象的主电子表格,该对象从SQL中提取所有相关数据,并使用数据透视表或过滤器或其他所需的东西,然后使用该数据表生成要发送的副本出来。

But before going to all that trouble, I would make sure that SSRS is not an option, because if you can use that, it has a lot of baked in functionality that would make your life easier (export to Excel, automatic date parameters, scheduled execution, email subscriptions, etc). 但是在解决所有麻烦之前,我将确保不要选择SSRS,因为如果可以使用它,它具有许多强大的功能,可以简化您的工作(导出到Excel,自动日期参数,执行,电子邮件订阅等)。

My rule of thumb is that if it can be processed on the database server, it probably should. 我的经验法则是,如果可以在数据库服务器上对其进行处理,则可能应该这样做。 Keep in mind, no matter how efficient your C# code is, SQL Server will mostly likely to it faster and more efficiently, after all it was designed for data manipulation. 请记住,无论您的C#代码有多高效,毕竟SQL Server都是为数据处理而设计的,它最有可能更快,更高效地运行它。

There is no shame in using #temp tables. 使用#temp表不会感到羞耻。 They maintain stats, can be indexed, and/or manipulated. 他们维护统​​计信息,可以对其进行索引和/或操纵。 One recent example, a developer create an admittedly elegant query using cte, the performance was 12-14 seconds vs mine at 1 second using #temps. 最近的一个例子是,开发人员使用cte创建了一个公认的优雅查询,性能为12-14秒,而使用#temps则为1秒。

Now, one carefully structured stored procedure could produce and return the 10 data-sets for your worksheets. 现在,一个精心构造的存储过程可以为您的工作表生成并返回10个数据集。 If you are using a product like SpreadSheetLight (there are many options available), it becomes a small matter of passing the results and creating the tabs (no cell level looping... unless you want or need to). 如果您使用的是SpreadSheetLight之类的产品(有很多可用的选项),那么传递结果并创建选项卡就变得无关紧要了(除非您想要或不需要,否则不会进行单元级循环播放)。

I would also like to add, you can dramatically reduce the number of touch points and better enforce the business logic by making SQL Server do the heavy lifting. 我还想补充一点,通过使SQL Server承担繁重的工作,您可以大大减少接触点的数量并更好地实施业务逻辑。 For example, a client introduced a 6W risk rating, which was essentially a 6.5. 例如,一位客户介绍了6W的风险等级,实际上是6.5。 HUNDREDS of legacy reports had to be updated, while I only had to add the 6W into my mapping table. 数以百计的旧报告必须进行更新,而我只需要在映射表中添加6W。

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

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