sql—如何从表值函数返回多个值以加快查询速度

tnkciper  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(151)

我正在整理一些地址。我有一个表,有城市,州,邮编都在一行,我试图解析出来。我创建了一个表值函数,因为我的理解是它更快。
customeraddress表有点不稳定,每行地址都有一个条目。所以一个客户id可能有2到4条记录。我的where查找最后4个数字,其中有一个,第一行不应该有城市,州,邮编。
customeraddress表如下所示:

customer_id address_line_id address_data
10447           3           CULVER CITY, CA  90233-6011
10893           3           VIRGINIA BEACH, VA  23452
10461           3           DOWNERS GROVE,  IL  60515
10894           1           1111 BOHM DRIVE
10894           2           PO BOX 109
10894           3           LITTLE CHUTE, WI  54140-0109
10895           1           1990 WEST 32ND AVE.
10895           2           DENVER, CO 80211

我的问题是:

SELECT customer_id, address_line_id, address_data, oa.*
  INTO #tmpAddress
  FROM customerAddress
  OUTER APPLY
 ( SELECT * FROM [dbo].[parseCityStateZip](customerAddress.address_data)    ) oa
  WHERE
    address_data LIKE '%,%' AND 
    ISNUMERIC( RIGHT(address_data,4) ) = 1 AND
    address_line_id != 1
ORDER BY customer_id, address_line_id

这是查询的执行计划。如果我读对了(我可能没有读错),表值函数运行了9米33秒,但是过滤器是123%??https://www.brentozar.com/pastetheplan/?id=s13iitvzw
我用了 OUTER APPLY 因为我认为这比为每个字段调用函数要好,而且我没有任何东西可以在一段时间内加入它 LEFT JOIN .
这是我在 OUTER APPLY ```
ALTER FUNCTION [dbo].[parseCityStateZip] (@inStr varchar(255) )
RETURNS @returnTable
TABLE (
[city] varchar(255),
[state] varchar(255),
[zip] varchar(12),
[country] varchar(50) NULL,
[intlFlag] bit NOT NULL DEFAULT(0))
AS
BEGIN

DECLARE
    @newStr varchar(255), @strCity varchar(255), @strState varchar(255), @strZip varchar(12), @strCountry varchar(255),
    @strIntlFlag bit

-- Determines if Zip Code is 5 or 9 Digits
--  Stores to variable and chops it off saving the remainign to @newStr
IF CHARINDEX('-', @inStr, 0) > 0
  BEGIN
    SELECT @strZip = RIGHT(@inStr, 10)
    SELECT @newStr = TRIM(SUBSTRING(@inStr, 0, LEN(@inStr) - 9))
  END
ELSE
  BEGIN
    SELECT @strZip = RIGHT(@inStr, 5)
    SELECT @newStr = TRIM(SUBSTRING(@inStr, 0, LEN(@inStr) - 4))
  END

-- Looks for the , separating City, State
--  Saves everything before the , and City, and after as State
--  Use TRIM to remove extra spaces before or after the State
SELECT @strCity =  TRIM( SUBSTRING( @newStr, 0, CHARINDEX(',', @newStr, 0) ) )
SELECT @strState = ULTRA.dbo.RemoveNonAlphaCharacters( TRIM( SUBSTRING( @newStr, CHARINDEX(',', @newStr, 0) + 1, LEN(@newStr) - CHARINDEX(',', @newStr, 0) ) ) )

-- Checks to see if the State matches a state in the info table, and is in the United States
IF EXISTS( SELECT * FROM [ULTRA].[dbo].[CityStateInfo] WHERE [Country] = 'United States' AND ([State_long] = @strState OR [State_abbr] = @strState))
    SELECT @strCountry = 'United States', @strIntlFlag = 0
ELSE
    SELECT @strCountry = 'INTERNATIONAL', @strIntlFlag = 1

INSERT INTO @returnTable VALUES (@strCity, @strState, @strZip, @strCountry, @strIntlFlag)
RETURN  

END

这是一个很好的执行计划:https://www.brentozar.com/pastetheplan/?id=bkfcqawzp
有没有一种方法可以将所有字段内联到查询中,例如:

SELECT
customer_id,
address_data,
(SELECT * FROM parseCityStateZip)
FROM customerAddress

这会加快查询速度吗?如果没有,可以做些什么来加速?
我正在使用最新的sql server和ssms

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题