MS SQL Server Tips

2 posts / 0 new
Last post
benzzon's picture
MS SQL Server Tips

SQL Server System Functions:

Swedish SQL User Group:
Intro to User Defined Functions (Updated)
Formatera sql-kod till andra format, ex.vis vb-sträng:
Free database info tool (query analyzer m.m):
(verkar tyvärr ej gå att kopiera resultat i query fönster..)
Workaround för att i SQL 2000 få "getdate()" att fungera i en udf (user defined function) (i SQL2005 kan getdate användas direkt..)

DECLARE @a datetime
SET @a = (SELECT * FROM OPENROWSET('SQLOLEDB', 'localhost';'sa';'password', 'SELECT GETDATE()'))
How to share data between stored procedurs (info about user defined functions also..)
Torskat på följande när det gäller user defined functions:
"EXECUTE cannot be used as a source when inserting into a table variable.
Msg 197, Level 15, State 1, Procedure fnDYF_Criteria_A1, Line 49"
"Only functions and extended stored procedures can be executed from within a function."
SQL Reference & examples:
MSDTC (Oracle, MTS, Error 7391..)

You receive error 7391 when you run a distributed transaction:;en-us;329332
You may recieve a 7391 error:
(denna länk gjorde att proc-anrop "stpDYF_Drawing_SPSYS_Get" hoppade igång hos LEB.
Proceduren innehåller oracle-anrop, och gav fel vid försök att göra insert av resultatet till temp-tabell i sql server.)

benzzon's picture
Tips for high performance SQL

Copied from:

This article was originally published in an internal site by our System Admin/DBA Mike Parrino. You can reach Mike at mparrino at

These tips apply broadly when writing high-performance stored procedures. Unfortunately, unlike some tips, you can’t simply apply most of them without first considering the nature and schema of the data you’re querying.

1. Avoid using cursors (as well as other looping structures) as much as possible. Cursors are inefficient, and database engines usually don’t have the best loop implementations in terms of performance.

2. On the database side, you can usually replace code involving cursors with aggregate SQL statements (SELECT, INSERT, and UPDATE) that use vector tables. All database engines are heavily optimized for aggregate statements, so even if a loop is unavoidable, it is always better to execute a few aggregate statements in a loop with a small number of iterations, than to create a cursor and execute simple statements over a large number of iterations.

3. Even if initial performance tests, especially with a small amount of data, show cursors to be more efficient than a complex aggregate statement, it is worthwhile to try to optimize the operation by breaking it into smaller portions or using other approaches—unless you can guarantee that the data value will stay small. Cursor approaches will not scale.

4. Make wise distinctions between temp tables and table variables. Table variables are in-memory structures that may work from 2-100 times faster than temp tables. But keep in mind that access to table variables gets slower as the volume of data they contain grows. At some point, table variables will overflow the available memory and that kills the performance. Therefore, use table variables only when their data content is guaranteed not to grow unpredictably; the breaking size is around several thousand records. For larger data volumes, I recommend temp tables with clustered indexes. Interestingly, I’ve found that a temp table with one clustered index is often faster than having multiple simple indexes. In contrast, multiple simple indexes with physical tables are often faster than one clustered index.

5. Make careful distinctions between hard rules and assumptions. This is more of a business design tip, which applies more to code design than to performance and scalability design in general. In real life however, performance and scalability are generally the first things to suffer from improper design. When rules are implemented as assumptions, they usually cause unnecessary calculations to be performed, affecting performance. However, when assumptions are implemented as rules they tend to cause errors and algorithm failures, which usually requires an urgent redesign. That, in turn, is usually performed with business constraints and results in inefficient final algorithms. That’s because bad design decisions are often corrected in a rush and without sufficient resources—sometimes under pressure from customers whose businesses are usually in a critical stage when problems are uncovered, but must continue operating during the process.

6. Pay attention to join order. Using proper join order sometimes lets the database engine generate hints that execute joins with an optimal amount of records. Most database engines also support hard hints, but in most cases you should avoid using hard hints and let the database engine figure out the best way to do its job on its own.

7. Be careful when joining complex views to other views and database tables in complex SELECT statements. When the database contains a significant amount of data, SQL Server engine tends to recalculate the execution plan of the resulting statement, which often results in an inefficient execution plan and may kill the performance. The most difficult part is that the behavior of SQL Server engine is inconsistent in that respect, and heavily depends on the database size, indexes, foreign keys, and other database structures and constraints. The consistent work-around is to pre-select data from the view into a temp table with the reasonable pre-filters, and then use that temp table in place of the underlying view.

8. Create indexes on temp tables wisely. As mentioned in Tip 4, clustered indexes are usually the best in terms of performance for temp tables; however, there is a difference between creating the index before or after inserting data into the temp table. Creating the index before the insert complicates the insert, because the database engine must order the selection. For complex selections such as those mentioned in Tip 7, the extra ordering may overcomplicate the overall statement and drastically degrade the performance. On the other hand, creating the index after the insert forces the database engine to recalculate the execution plan of the stored procedure every time it is called. Therefore, the decision is always a trade-off and you should make it based on the relative costs of the two possibilities.

9. In general, try to avoid execution plan recalculation. One common cause of recalculation occurs when the stored procedure contains several paths that depend on values passed in parameters. However, whether avoiding recalculation is possible depends on the complexity of the stored procedure and on other circumstances, such as those described in tip 8. When the engine does recalculate execution, performance always suffers; however, recalculating the execution plan of the caller does not force the execution plan recalculation of the called procedure (or view or function). Therefore, the workaround is to divide one stored procedure into multiple procedures (depending on the passed-in parameters), and then call the children from the parent conditionally. You should perform this subdivision very carefully though, because it can be a maintenance nightmare—but sometimes it seems to be the only way to achieve acceptable database performance and scalability.

Finally, although this isn’t either a performance or a scalability tip, I urge you to format your stored procedure scripts legibly. It’s best to agree on common practices such as clause order and formatting rules with your coworkers in advance. Not only does that help avoid errors, it also clearly shows the logical structure of the statements and often aids in figuring out faulty filters and joins.