简体   繁体   English

他们是重写此SQL查询的更好方法吗?

[英]Is their a better way to rewrite this SQL Query?

Good day, I have a query that utilizes a nested select to gather data from several tables... Is there a far better way to rewrite this query to speed up its process? 美好的一天,我有一个查询,该查询利用嵌套选择从多个表中收集数据。是否有更好的方法重写该查询以加快其处理速度? The most time consuming part is the batch insert... hope you can help... 最耗时的部分是批量插入...希望您能提供帮助...

Here is what I would do assuming that your tables are indexed as you have said: I would rip out that select distinct statement and stick it into a separate SP, obviously the data will be in a temp table which is indexed. 这就是我假设您的表已按照您所说的那样进行索引的情况:我将剔除选择独特的语句并将其粘贴到单独的SP中,显然数据将在已建立索引的临时表中。 I would then call this SP from within a main proc and then join this temp table with the main insert statement. 然后,我将从主proc内调用此SP,然后将此temp表与主insert语句连接在一起。 This will allow the optimiser to know the distribution of the data in the temp table and make some optimisations. 这将使优化器知道临时表中数据的分布并进行一些优化。 Let me know if that was not clear. 让我知道是否不清楚。 I use this technique all the time. 我一直在使用这种技术。 It also results in easier to maintain and read code. 它还使维护和读取代码更加容易。

Okay, given the givens, I think a good bet would be to use indexed views. 好的,鉴于给定的条件,我认为一个不错的选择是使用索引视图。 This allows alot to your joins and computations to be done at insert time and will seriously reduce the complexity of the actual insert SP. 这使您可以在插入时进行大量的连接和计算,并且将大大降低实际插入SP的复杂性。

see http://technet.microsoft.com/en-us/library/dd171921(v=sql.100).aspx 参见http://technet.microsoft.com/zh-cn/library/dd171921(v=sql.100).aspx

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

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