Showing posts with label UDF. Show all posts
Showing posts with label UDF. Show all posts

Tuesday, May 2, 2017

Use of functions on indexed columns in predicates

Use of functions on indexed columns which are used in predicates results in index not getting used for seek operation.

For example:

Use tempdb
set nocount on
go

IF OBJECT_ID(N'Customers', N'U') IS NOT NULL
   DROP TABLE dbo.Customers;
  
CREATE TABLE dbo.Customers (
 customer_nbr INT NOT NULL PRIMARY KEY,
 customer_name VARCHAR(35) NOT NULL);

CREATE NONCLUSTERED INDEX ix_customers
ON dbo.Customers(customer_name);

INSERT dbo.Customers VALUES(1, 'Joe');
INSERT dbo.Customers VALUES(2, 'Leo');
INSERT dbo.Customers VALUES(3, 'Dave');
INSERT dbo.Customers VALUES(4, 'Lenny');
INSERT dbo.Customers VALUES(5, 'Larry');
INSERT dbo.Customers VALUES(6, 'Lefty');
INSERT dbo.Customers VALUES(7, 'Lemur');

GO

If I want to write a query on this table to get all the customers whose first name starts with 'L'.
SELECT customer_name
FROM dbo.Customers
WHERE LEFT(customer_name, 1) = 'L';

Looking at the execution plan for above query. We can see index scan in here and not seek:










Now, what if we rewrite this query without the use of LEFT function as below:
SELECT customer_name
FROM dbo.Customers
WHERE customer_name LIKE 'L%';

Looking at the execution plan for above query shows us index seek and the output of query is same as before.










Functions on indexed column used in predicates makes them non-sargable and impacts performance.



Reference:
https://sqlbits.com/Sessions/Event14/Common_TSQL_Mistakes









Monday, August 15, 2011

SQL Server -- Stored Procedure vs UDF


Difference between Stored Procedure and User Defined Function (UDF)

UDF (User Defined Function)

  • User Defined Functions (UDFs) can to executed in the SQL statements anywhere in the WHERE/HAVING/SELECT section.
  • UDFs that return tables can be treated as another dataset. This can be used in JOINs with other tables.
  • A UDF returns table variables.
  • UDFs can't change the server environment or your operating system environment.
  • Inline UDF’s can be though of as views that take parameters and can be used in JOINs and other  Rowset operations.
  • UDF's doesn't support error handling. TRY - CATCH cannot be used in User defined functions.

Stored Procedure

  • Stored procedures can not be used in WHERE/HAVING/SELECT section, they have to use EXECUTE or EXEC to run.
  • SPROC can't return a table variable although it can create a table.
  • SPROCs can change server or operating system environment variables by using the system store procedure. (eg: Change the values for job execution (enable or disable)
  • Stored Procedure can have a function in it, while a function cannot have a Stored Procedure in it.