简体   繁体   English

硬编码或魔术文字可以提高SQL Server存储过程的性能?

[英]Hard Coding or Magic Literals improve performance in SQL Server Stored Procedures?

From day 1 of my programming career I have always been told hard-coding/magic numbers or literals are bad. 从我编程生涯的第一天开始,我总是被告知硬编码/魔术数字或文字不好。 One should avoid it. 一个人应该避免它。 I followed this principle even in Stored Procedures in SQL Server. 即使在SQL Server中的存储过程中,我也遵循了这一原则。

Recently I came across this article by Ian Jose and it came as a rude shock to me. 最近,我碰到了伊恩·何塞(Ian Jose) 撰写的这篇文章 ,这震惊了我。 He has nicely explained the problem with this practice in SQL Server Stored Procedures with evidence. 他用证据很好地解释了SQL Server存储过程中这种做法的问题。 Ian has demonstrated that the performance takes a big hit with this approach. Ian证明了这种方法对性能的影响很大。

See this article by Chris Hedgate to know what my reaction was when I read Ian's blog. 请参阅Chris Hedgate的这篇文章,以了解我阅读Ian博客时的反应。

I understand if it is a smaller code snippet and the hard coded value is not used more than once, then some people take the liberty keeping them instead of declaring them as variables or constants and using the variables or constants instead. 我知道,如果这是一个较小的代码段,并且硬编码值没有被多次使用,那么某些人会保留它们的自由,而不是将它们声明为变量或常量,而是使用变量或常量。 But it seems hard coding should be encouraged in Stored Procedures? 但是似乎应该鼓励在存储过程中使用硬编码吗?

I did find a similar discussion on SO but that thread isn't evaluating the performance hit, rather it is talking about how to avoid hard-coding, something that even I used to consider myself very good at. 我确实在SO上找到了类似的讨论,但是该线程并没有在评估性能损失,而是在谈论如何避免硬编码,甚至我以前都认为自己很擅长。 But now after reading Ian's blog, I feel I am adding more problem there than solving it. 但是现在,在阅读Ian的博客之后,我觉得我在这里添加的问题比解决问题还要多。

I would like to request the other experts to share their experience on this topic as this seems to be a violation of fundamental programming guidelines. 我想请其他专家分享他们在该主题上的经验,因为这似乎违反了基本的编程准则。 Could this be because of the way SQL Server is designed? 难道是因为SQL Server的设计方式吗? Is this not an issue in Oracle or MySQL? 这在Oracle或MySQL中不是问题吗? Oracle in fact suggest these 3 nice approaches to avoid hard coding. Oracle实际上建议使用这三种不错的方法来避免硬编码。

Edit: Thank you all for your valuable comments. 编辑:谢谢大家的宝贵意见。 I would like to request for some more statistics, to constructively challenge Ian's numbers. 我想要求提供更多统计数据,以建设性地挑战伊恩的数字。 Just to conclude that avoiding hard coding does not have that bad an impact on performance, which is what Ian has demonstrated. 可以得出结论,避免硬编码不会对性能产生不良影响,这正是Ian证明的。 I am sorry if I am asking for too much and eagerly look forward to your responses. 很抱歉,如果我要求太多,并热切期待您的答复。

I had also opened a thread on LinkedIn as I didn't get many responses here at first. 我也已经在LinkedIn上打开了一个话题 ,因为起初我没有在这里得到很多回复。 For people who have the same confusion as me can refer to that for some more info. 对于与我有相同困惑的人,可以参考它以获取更多信息。

Here's how I understand the problem: 这是我对问题的理解:

  1. Hard coded literals in a DBMS (SQL / Oracle) do improve performance. DBMS(SQL / Oracle)中的硬编码文字确实可以提高性能。
  2. However, the second time you execute the query, where the hard coded literal changes, it must be recompiled, which fills up the cache. 但是,第二次执行查询时,硬编码文字会发生更改,必须重新编译查询,这会填满缓存。
  3. This means that the cache can't be used efficiently for the rest of the DB. 这意味着缓存不能有效地用于其余的数据库。

So, whilst: 因此,虽然:

SELECT * FROM X WHERE Y = '123'

Might be slightly faster than: 可能会比:

SELECT * FROM X WHERE Y = @myVar

If you then want to do: 如果您想这样做:

SELECT * FROM X WHERE Y = '234'

The DBMS sees that as a different query . DBMS将其视为不同的查询 This then fills up the cache, meaning that the next time you issue a query that should be cached, it will be slower. 然后,这将填满缓存,这意味着下次您发出应缓存的查询时,它会变慢。

Parameters are better, since the same plan can be used for multiple parameter values. 参数更好,因为同一计划可用于多个参数值。 An SP in SQL Server is a contract with the app writer that one plan can be used for multiple invocations. SQL Server中的SP是与应用编写者签订的合同,其中一个计划可用于多次调用。 It has been eight years since I worked in SQL Server, and much may have changed. 自从我从事SQL Server工作已经八年了,可能已经发生了很多变化。 However, the optimizer will "sniff" the parm value for optimization purposes. 但是,优化器将“嗅探”参数值以进行优化。 It is nice if that value is "typical". 如果该值是“典型”,那就太好了。 Unfortunately, recompile can happen at any time, and hence with any parameter value. 不幸的是,重新编译可以在任何时间发生,因此可以使用任何参数值。 Plan guides are one technique to make an application immune to this danger. 计划指南是使应用程序免受此危险影响的一种技术。 There are likely other techniques as well. 可能还有其他技术。

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

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