Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, December 23, 2019

Scalar UDF Inlining (SQL 2019)- What is it and how do we know if we can use it?



I have been pretty excited about the new SQL 2019 feature “Scalar UDF inlining”. I want to share what I learned about Scalar UDF inlining because it's awesome. It makes functions easier to implement without becoming a plague to performance, thus functions will be used more often (finally).

Scalar UDF inlining in SQL 2019 simply makes most (non-inlined) UDF’s execute faster. The SQL 2019 query engine interprets the scalar UDF’s logic and makes it set-based (inlined), ready to take advantage of the performance improvements that come with set based processing.

So how do we know if an existing function would be able to use this new feature?

In my opinion, the easiest way to know if your function is able to take advantage of this is to simply query the sys.sql_modules view (SQL 2019). The sys.sql_modules catalog view includes a property called “is_inlinable”, which indicates whether a UDF is inlinable or not.
·         A value of 1 indicates that it is inlinable, and 0 indicates otherwise
·         This property will also have a value of 1 for inline table-valued functions, since they are inlinable by definition.

In my next blog post I will cover how to manually change scalar UDFs into an inlinable (for those that won’t be going to 2019 any time soon).

Reference:
Microsoft has a large list of requirements that let you know if a Scalar UDF can be inlined:
A scalar T-SQL UDF can be inline if all of the following conditions are true:
·         The UDF is written using the following constructs:
o    DECLARESET: Variable declaration and assignments.
o    SELECT: SQL query with single/multiple variable assignments1.
o    IF/ELSE: Branching with arbitrary levels of nesting.
o    RETURN: Single or multiple return statements.
o    UDF: Nested/recursive function calls2.
o    Others: Relational operations such as EXISTSISNULL.
·         The UDF does not invoke any intrinsic function that is either time-dependent (such as GETDATE()) or has side effects3 (such as NEWSEQUENTIALID()).
·         The UDF uses the EXECUTE AS CALLER clause (the default behavior if the EXECUTE AS clause is not specified).
·         The UDF does not reference table variables or table-valued parameters.
·         The query invoking a scalar UDF does not reference a scalar UDF call in its GROUP BY clause.
·         The query invoking a scalar UDF in its select list with DISTINCT clause does not have ORDER BY clause.
·         The UDF is not used in ORDER BY clause.
·         The UDF is not natively compiled (interop is supported).
·         The UDF is not used in a computed column or a check constraint definition.
·         The UDF does not reference user-defined types.
·         There are no signatures added to the UDF.
·         The UDF is not a partition function.
1 SELECT with variable accumulation/aggregation (for example, SELECT @val += col1 FROM table1) is not supported for inlining.
2 Recursive UDFs will be inlined to a certain depth only.
3 Intrinsic functions whose results depend upon the current system time are time-dependent. An intrinsic function that may update some internal global state is an example of a function with side effects. Such functions return different results each time they are called, based on the internal state.

Monday, February 29, 2016

How to turn off IntelliSense in SQL Server Management Studio

IntelliSense in SQL Server Management Studio is beneficial to some, but irritating to others. For me, when I type out a line of simple code, IntelliSense will spam me with suggestions and actually cause my script to become gibberish if I ignore the suggestions. The suggestions will automatically be selected if I type too fast and don’t hit the escape key every time a seemingly meaningless IntelliSense option is selected. I will note that in SQL 2014 IntelliSense has become better, and I do use it now; however, while working in 2008R2 it was a major PIA and I always disabled it. Here is the way to turn it off, for good, without having to hit the IntelliSense Button in the SQL Editor Toolbar every time you load SQL.

IntelliSense Button in the SQL Editor Toolbar



Turn off IntelliSense


·         From SSMS go to the Tools Menu, select Options

·         Under Text Editor, Transact-SQL, select IntelliSense

·         If Enable IntelliSense is selected, clear the checkbox and hit OK.

Tuesday, February 23, 2016

Using INTERSECT and EXCEPT to compare records

I was asked for a simple way to compare two tables that should be identical and have the same schema. I provided the following scripts to show records that are identical (Intersect) and different (Except):

IF OBJECT_ID(N'dbo.test1') IS NOT NULL
    
DROP TABLE test1
CREATE TABLE
dbo.test1 (name VARCHAR(20), dob DATETIME)

IF
OBJECT_ID(N'dbo.test2') IS NOT NULL
    
DROP TABLE test2
CREATE TABLE
dbo.test2 (name VARCHAR(20), dob DATETIME)

INSERT INTO
test1 (name, dob)
        
VALUES ('Fred','20010102')
INSERT INTO
test1 (name, dob)
        
VALUES ('Fred1','20010103')
INSERT INTO
test1 (name, dob)
        
VALUES ('Fred2','20010104')
INSERT INTO
test1 (name, dob)
        
VALUES ('Fred3','20010105')
INSERT INTO
test1 (name, dob)
        
VALUES ('Fred4','20010106')
INSERT INTO
test1 (name, dob)
        
VALUES ('Fred5','20010107')
INSERT INTO
test2 (name, dob)
        
VALUES ('Fred','20010102')
INSERT INTO
test2 (name, dob)
        
VALUES ('Fred2','20010104')
INSERT INTO
test2 (name, dob)
        
VALUES ('Fred3','20010105')
INSERT INTO
test2 (name, dob)
        
VALUES ('Fred7','20010106')
INSERT INTO
test2 (name, dob)
        
VALUES ('Fred5','20010107')
    

--exist and are the same in both tables test 1, not in test 2

SELECT
name, dob
FROM dbo.test1
INTERSECT
SELECT
name, dob
FROM dbo.test2

--exists in test 1, does not exist in test 2

SELECT
name, dob
FROM dbo.test1
EXCEPT
SELECT
name, dob
FROM dbo.test2

--exists in test 2, does not exist in test 1

SELECT
name, dob
FROM dbo.test2
EXCEPT
SELECT
name, dob
FROM dbo.test1


As you can see by running the script above, intersect shows values from the first query where there are identical record matches in the second query. Except shows values in the first query where there are No identical record matches in the second query. Additional benefit is that it also includes null values in the comparison, you do not get the same functionality with null values on joins (when you just enter the column names) from 2 tables (without adding specific null handling).
 

Thursday, January 17, 2013

Make a SQL Server Shortcut to Change Database Connections

In SSMS, to change databases most people do one of the following:
1. Click the Change Connection toolbar button
2. Right click anywhere in the query pane -> Connection -> Change Connection
3. Go to the query menu -> Connection -> Change Connection (Alt+Q, C, H)

The change connection menu button actually does have a shortcut key predefined (Alt+H); however this action has a lower priority than the (Alt+H) shortcut for the Help Menu. Thanks Microsoft! I'll show you how to enable this the Change Connection action using (Alt+G).

The work around is actually quite simple, but it is not very intuitive.
  • Right click anywhere on the toolbar and select Customize
  • Ignore (but don't close this yet) the Customize window that pops up
  • Right Click on the Change Connection toolbar button and select "Image & Text"
    • This enables the hotkey to be use inside the query window but you can not activate it yet because Help Menu shortcut (Alt + H) still has precedence.
  • Right Click on the Change Connection toolbar button again
  • Change the Name field from "C&hange Connection..." to "Chan&ge Connection..."
  • Close the Customize window that popped up earlier.
Now you can use the Alt + G keyboard shortcut to change connections.