November 13, 2015 at 9:10 am
I have been working with t-sql 2012 for awhile. I have noticed that some questions run faster than other queries. I know that this is based upon the way the queries are executed. However I am wondering if certain types of t-sql statements execute faster than others? If so, can you tell me and/or point me to a reference that will show me which t-sql statements run slower and what are the replacement t-sql statements that will make the query execute faster?
November 13, 2015 at 10:43 am
It's not really that simple.
Most of the time, speed comes down to how many rows the query is processing, whether the query can use indexes properly and whether there are useful indexes to support the query.
So make sure that you're not returning data that isn't needed, that you're not writing queries so that they can't use indexes, and make sure that useful indexes exist to support the queries. And stay away from scalar user-defined functions used in queries.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 13, 2015 at 12:08 pm
what are scalar user-defined functions used in queries?
November 13, 2015 at 12:19 pm
wendy elizabeth (11/13/2015)
what are scalar user-defined functions used in queries?
https://msdn.microsoft.com/en-CA/library/ms191320.aspx
Google is your friend. 😉
November 13, 2015 at 1:36 pm
When functions are necessary, use inline table-valued functions if at all possible. Even if you only have to return a single column (like a scalar function) write it as an ITVF instead of a scalar function.
I remember seeing a reference to an inline scalar function somewhere on the Microsoft site once, but i don't remember where. As far as I know, there's no ISF object type in any version of SQL Server, but someone at Microsoft must have thought of it somewhere along the line.
November 13, 2015 at 9:25 pm
wendy elizabeth (11/13/2015)
I have been working with t-sql 2012 for awhile. I have noticed that some questions run faster than other queries. I know that this is based upon the way the queries are executed. However I am wondering if certain types of t-sql statements execute faster than others? If so, can you tell me and/or point me to a reference that will show me which t-sql statements run slower and what are the replacement t-sql statements that will make the query execute faster?
There are certain types of queries that generally run more slowly than other types. The faster queries are usually based on what is known as "set-based" logic and the slower stuff is normal caused by RBAR (Row By Agonizing Row). RBAR generally includes While Loops, Recursive CTEs that count, Triangular Joins, non-SARGable predicates, unintended many-to-many joins, and the use of Scalar and Mult-statement Inline Table Values Functions.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 16, 2015 at 8:42 pm
Any feedback, Wendy?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply