未加星标

What’s New in SQL Server 2019 CTP 2.1: Faster Functions

字体大小 | |
[数据库(mssql) 所属分类 数据库(mssql) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

A while back, we talked you through a public whitepaper about how Microsoft was working on making user-defined functions go faster . Now that the next preview (CTP2.1) of SQL Server 2019 is out , you can start getting your hands on Froid, the performance-boosting feature. Here’s the documentation on it let’s see how it works.

Using the StackOverflow2010 database , say our company has a scalar user-defined function that we use to calculate how many badges a user has earned:

CREATE OR ALTER FUNCTION dbo.ScalarFunction ( @uid INT ) RETURNS BIGINT WITH RETURNS NULL ON NULL INPUT, SCHEMABINDING AS BEGIN DECLARE @BCount BIGINT; SELECT@BCount = COUNT_BIG(*) FROMdbo.Badges AS b WHERE b.UserId = @uid GROUP BY b.UserId; RETURN @BCount; END; GO

In this past, performance for this has really sucked. If I grab 1,000 users, and call the function to get their badge counts:

SELECT TOP 1000 u.DisplayName, dbo.ScalarFunction(u.Id) FROM dbo.Users AS u GO

The execution plan looks simple:


What’s New in SQL Server 2019 CTP 2.1: Faster Functions

2017 plan that hides the scalar’s work

But it’s actually terrible:

It doesn’t show all the stuff the scalar function does It wildly underestimates the work involved (scalars have fixed tiny costs regardless of the work they do) It doesn’t show the logical reads performed by the function And of course, it calls that function 1,000 times once for each user we return

Metrics:

Runtime: 17 seconds CPU time: 56 seconds Logical reads:6,643,089 Now let’s try in SQL Server 2019.

My database has to be in 2019 compat mode to enable Froid, the function-inlining magic. Run the same query again, and the metrics are wildly different:

Runtime: 4 seconds CPU time: 4 seconds Logical reads:3,247,991 (which still sounds bad, but bear with me)

The execution plan looks worse:


What’s New in SQL Server 2019 CTP 2.1: Faster Functions

2019 plan showing the inlined function’s terribility

Your first thought is probably, “Mother Nature, that plan looks like more work,” but the point is that it’s now showing the work involved in the function. Before, you had to use tools like sp_BlitzCache to figure out which functions were getting called, and how often.

Now, you can see that SQL Server is building a spool, or as Erik likes to say, passively-aggressively building its own missing index on the fly without bothering to tell you that it needs one (note that there’s no missing index request on the plan):


What’s New in SQL Server 2019 CTP 2.1: Faster Functions

Spoolin’ up

That’s great! That means I can easily fix that just by doing index tuning myself. Granted, I have to know how to do that level of index tuning but that’s a piece of cake when suddenly the plan makes it more obvious as to what it’s really doing.

How to find out if your functions will go faster

The documentation lists a lot of T-SQL constructs that will or won’t go inline:


What’s New in SQL Server 2019 CTP 2.1: Faster Functions

From the documentation

But reading that would require you to open the code in your user-defined functions, too, and that’s one of the top causes for data professional suicide. I need all the readers I can get.

So instead, just go download the SQL Server 2019 preview , install it on a test VM, restore your database into it, and run:

SELECT * FROM sys.sql_modules;

There’s a new is_inlineable column that tells you which of your functions can be inlined:


What’s New in SQL Server 2019 CTP 2.1: Faster Functions

sys.sql_modules

Then to find out which of your functions are actually getting called the most often in production today, use sp_BlitzCache:

sp_BlitzCache @SortOrder = 'executions';

Make an inventory of those, and while you’re in there looking at functions, try CPU, Reads, or Memory Grant as different sort orders. Keep an eye out for table variables, high memory grants, or low grants that lead to spills, because SQL Server 2019 improves those too:

What’s New in SQL Server 2019: Faster Table Variables What’s New in SQL Server 2019: Adaptive Memory Grants

That’ll help you build a business case to management. You’ll be able to explain which of your queries are going to magically run faster as soon as you upgrade, and how much of an impact that’ll have on your SQL Server overall all without changing code. That’s especially powerful for third party apps where you can’t change the code, and you need a performance boost.

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

分页:12
转载请注明
本文标题:What’s New in SQL Server 2019 CTP 2.1: Faster Functions
本站链接:https://www.codesec.net/view/611801.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(13)