在 SQL Server 2005 中获取列总计的最优化方法+
我正在为供各个州使用的应用程序创建一些报告。该数据库有可能变得非常大。我想知道哪种方式是获取列总计的最佳方式。
目前我的 SQL 类似于以下内容:
SELECT count(case when prg.prefix_id = 1 then iss.id end) +
count(case when prg.prefix_id = 2 then iss.id end) as total,
count(case when prg.prefix_id = 1 then iss.id end) as c1,
count(case when prg.prefix_id = 2 then iss.id end) as c2
FROM dbo.TableName
WHERE ...
如您所见,这些列在那里出现了两次。在一种情况下,我将它们相加并显示总数,在另一种情况下,我仅显示报告所需的各个值。
这是 SQL 的一个非常小的样本,有 20 多个列,并且有时会对其中 4 个或更多列进行求和。
我正在考虑声明一些@Parameters并将每个列设置为等于@Parameter,然后我可以将显示列总数所需的@Parameters相加,即: SET @Total = @c1 + @c2
但是,SQL Server 引擎是否关心这些列是否多次出现在其中?有更好的方法吗?
I am creating some reports for an application to be used by various states. The database has the potential to be very large. I would like to know which way is the best way to get column totals.
Currently I have SQL similar to the following:
SELECT count(case when prg.prefix_id = 1 then iss.id end) +
count(case when prg.prefix_id = 2 then iss.id end) as total,
count(case when prg.prefix_id = 1 then iss.id end) as c1,
count(case when prg.prefix_id = 2 then iss.id end) as c2
FROM dbo.TableName
WHERE ...
As you can see, the columns are in there twice. In one instance, im adding them and showing the total, in the other im just showing the individual values which is required for the report.
This is a very small sample of the SQL, there are 20+ columns and w/i those columns 4 or more of them are being summed at times.
I was thinking of declaring some @Parameters and setting each of the columns equal to a @Parameter, then I could just add up which ever @Parameters I needed to show the column totals, IE: SET @Total = @c1 + @c2
But, does the SQL Server engine even care the columns are in there multiple times like that? Is there a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这样做的任何原因,因为
它会给您留下 prefix_id 的结果集和每个 prefix_ID 的行数...可能比一系列 count(case) 语句更优先,而且我认为它应该更快,但我无法确定。
在我自己求助于 @vars 之前,我会使用子查询。像这样的事情:
Any reason this isn't done as
It would leave you with a result set of the prefix_id and the count of rows for each prefix_ID...might be preferential over a series of count(case) statements, and I think it should be quicker, but I can't confirm for sure.
I would use a subquery before resorting to @vars myself. Something like this:
如果可以的话,在诉诸 T-SQL 过程逻辑之前使用直接 SQL。根据经验,如果可以用 SQL 完成,就用 SQL 完成。如果您想使用直接 SQL 模拟静态值,请尝试像这样的内联视图:
这样您在逻辑上就可以获取一次计数,并可以根据这些计数计算值。不过,我认为 SQL Server 足够聪明,不必扫描 count n 次,因此我不知道您的计划是否与我发送的 SQL 和您拥有的 SQL 不同。
Use straight SQL if you can before resorting to T-SQL procedure logic. Rule of thumb if you can do it in SQL do it in SQL. If you want to emulate static values with straight SQL try a inline view like this:
This way you logically are getting the counts once and can compute values based on those counts. However I think SQL Server is smart enough to not have to scan for the count n number of times so I don't know that your plan would differ from the SQL I sent and the SQL you have.