简体   繁体   中英

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. I would then call this SP from within a main proc and then join this temp table with the main insert statement. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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