ms sql server函数性能问题

roejwanj  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(401)

希望有人能帮我解决mssql函数的性能问题。我有下面的函数来匹配同一个表中的用户。用户a正在搜索他可以匹配的用户。如果他发现一个用户b符合他的标准,用户b将检查用户a是否符合他们的标准。如果它们已经匹配,则在excludedcandidates表中。我遇到的问题是查询时间太长。
我尝试了很多东西,但没有更多的想法如何改善它。
也许对查询的一些索引或更改可以在这里有所帮助。任何帮助都会得到报答。

create function [dbo].[FindUser](@UserId uniqueidentifier, @dis int, @gen int, @age datetime, @f int, @ti int, @s int, @La float, @Lo float)
returns @T table (UserId uniqueidentifier, Profile nvarchar(MAX), Filter nvarchar(MAX), F int, I bit, IsP bit)
as
BEGIN

    declare @Tmp table(UserId uniqueidentifier, a nvarchar(MAX), b nvarchar(MAX), c int, d bit, e bit)

    DECLARE @source geography
    select @source = geography::Point(@La, @Lo, 4326)

    insert into @Tmp
    SELECT TOP 10 U.UserId, U.Profile, U.F, @s as Fil, U.Ve, U.TT from Users AS U WITH (NOLOCK)
    WHERE ((@gen & U.Ge) != 0) AND
    (Sea = 1 OR Sea = 2) AND
    @s = U.Sear AND
    U.La is not null and U.Lon is not null AND
    @dis >= (@source.STDistance(geography::Point(U.La, U.Lon, 4326)) / 1000) AND
    (@f <= YEAR(GETUTCDATE()) - YEAR(@age)) AND (@ti >= YEAR(GETUTCDATE()) - YEAR(@age)) AND
    U.UserId != @UserId
    and not exists
    (select TOP 1 IC1.InitiatorUserId from ExcludedCandidates AS IC1 with (NOLOCK)
    where (IC1.InitiatorUserId = @UserId and IC1.PartnerUserId = U.UserId) OR
    (IC1.InitiatorUserId = U.UserId and IC1.PartnerUserId = @UserId)) 
    and exists(

        SELECT U.UserId from Users UserP with (NOLOCK)
        WHERE ((JSON_VALUE(UserP.Filter, '$.gender') & U.Ge) != 0) AND
        (Sea = 1 OR Sea = 2) AND
        @s = Sea AND
        (JSON_VALUE(UserP.Filter, '$.age.lo') <= YEAR(GETUTCDATE()) - YEAR(@age)) AND (JSON_VALUE(UserP.Filter, '$.age.up') >= YEAR(GETUTCDATE()) - YEAR(@age)) AND
         JSON_VALUE(UserP.Filter, '$.di') >= (geography::Point(UserP.La, UserP.Lon, 4326).STDistance(geography::Point(@La, @Lo, 4326)) / 1000) AND
        UserId = U.UserId

    )
    order by U.Sea DESC

    insert into @T
    select UserId, a, b, c, d, e from @Tmp

    return 
END

我们使用的索引

CREATE NONCLUSTERED INDEX [NonClusteredIndex_Users_Search] ON [dbo].[Users]
(
    [Sea] DESC
)WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex_Users_SearchSearchState] ON [dbo].[Users]
(
    [Sear] ASC,
    [Sea] ASC
)
INCLUDE (   [Filter],
    [La],
    [Lon]) WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO
chhkpiq4

chhkpiq41#

这里有一个多行表值函数,已知它的性能很差。您很可能会发现,将其转换为内联表值函数将提供更好的性能。
由于没有其他对象的定义,我无法对此进行测试(因此没有检查任何语法错误),但是,这是到内联表值函数的文本转换。然而,在这里您可能可以做更多的事情(例如 @dis >= (V.srv.STDistance(geography::Point(U.La, U.Lon, 4326)) / 1000) 不是可搜索的),但如果没有一个真正的目标,以及样本数据和预期结果,这将是不可能做更多的猜测:

CREATE FUNCTION [dbo].[FindUser](@UserId uniqueidentifier, @dis int, @gen int, @age datetime, @f int, @ti int, @s int, @La float, @Lo float)
RETURNS table --@T table (UserId uniqueidentifier, Profile nvarchar(MAX), Filter nvarchar(MAX), F int, I bit, IsP bit)
AS RETURN

    --declare @Tmp table(UserId uniqueidentifier, a nvarchar(MAX), b nvarchar(MAX), c int, d bit, e bit)
    --
    --DECLARE @source geography
    --select @source = geography::Point(@La, @Lo, 4326)

    SELECT TOP (10)
           U.UserId,
           U.Profile,
           U.F,
           @s as Fil,
           U.Ve,
           U.TT
    FROM dbo.Users AS U-- WITH (NOLOCK) --Unless you really undersatnd what NOLOCK does, you shou.dn't be using this.
         CROSS APPLY(VALUES(geography::Point(@La, @Lo, 4326)))V(src)
    WHERE ((@gen & U.Ge) != 0)
      AND (Sea = 1 OR Sea = 2)
      AND @s = U.Sear
      AND U.La IS NOT NULL
      and U.Lon IS NOT NULL
      AND @dis >= (V.srv.STDistance(geography::Point(U.La, U.Lon, 4326)) / 1000)
      AND (@f <= YEAR(GETUTCDATE()) - YEAR(@age)) AND (@ti >= YEAR(GETUTCDATE()) - YEAR(@age))
      AND U.UserId != @UserId
      AND NOT EXISTS (SELECT 1 --No need for a TOP (1) here
                      FROM ExcludedCandidates AS IC1-- with (NOLOCK) --Unless you really undersatnd what NOLOCK does, you shou.dn't be using this.
                      WHERE (IC1.InitiatorUserId = @UserId AND IC1.PartnerUserId = U.UserId)
                         OR (IC1.InitiatorUserId = U.UserId AND IC1.PartnerUserId = @UserId)) 
      AND exists(SELECT 1
               from Users UserP-- with (NOLOCK) --Unless you really undersatnd what NOLOCK does, you shou.dn't be using this.
               WHERE ((JSON_VALUE(UserP.Filter, '$.gender') & U.Ge) != 0)
                 AND (Sea = 1 OR Sea = 2)
                 AND @s = Sea
                 AND (JSON_VALUE(UserP.Filter, '$.age.lo') <= YEAR(GETUTCDATE()) - YEAR(@age))
                 AND (JSON_VALUE(UserP.Filter, '$.age.up') >= YEAR(GETUTCDATE()) - YEAR(@age))
                 AND JSON_VALUE(UserP.Filter, '$.di') >= (geography::Point(UserP.La, UserP.Lon, 4326).STDistance(geography::Point(@La, @Lo, 4326)) / 1000)
                 AND UserId = U.UserId)
    ORDER BY U.Sea DESC;

GO
rvpgvaaj

rvpgvaaj2#

如果没有明确的理由需要函数,我想我会把它改成一个存储过程。创建一个函数,将结果包含在另一个表的select语句中。听起来你在这里不是这么做的。。。
另外,我将把解析json数据的逻辑从表中分离出来,并在主查询之外执行它。这些解析语句可能占用了您最多的时间。你可以单独优化。
最后,确保你的网站上有一个索引 ExcludedCandidates 像下面这样的表格。。。

CREATE INDEX [IX_ExcludedCandidates_UserIDs] ON [dbo].[ExcludedCandidates] (
    InitiatorUserId, PartnerUserId
)

这是我想尝试的过程定义。。。

CREATE PROC [dbo].[FindUser] (
    @UserId uniqueidentifier,
    @dis int,
    @gen int,
    @age datetime,
    @f int,
    @ti int,
    @s int,
    @La float,
    @Lo float
) AS BEGIN
    DECLARE @source geography = geography::Point(@La, @Lo, 4326)

    SELECT
        *
    INTO
        #UserInfo
    FROM (
        SELECT
            UserId,
            La,
            Lon,
            Sea,
            JSON_VALUE(Filter, '$.gender') gender,
            JSON_VALUE(Filter, '$.age.lo') age_lo,
            JSON_VALUE(Filter, '$.age.up') age_up,
            JSON_VALUE(Filter, '$.di') di
        FROM
            Users
        ) S
    WHERE
        di >= (geography::Point(La, Lon, 4326).STDistance(geography::Point(@La, @Lo, 4326)) / 1000) 

    SELECT TOP 10
        U.UserId,
        U.Profile,
        U.F,
        @s AS Fil,
        U.Ve,
        U.TT
    FROM
        Users AS U WITH (NOLOCK)
    WHERE
        ((@gen & U.Ge) != 0) AND (
            Sea = 1
            OR Sea = 2
        ) AND 
        @s = U.Sear AND
        U.La IS NOT NULL AND
        U.Lon IS NOT NULL AND
        @dis >= (@source.STDistance(geography::Point(U.La, U.Lon, 4326)) / 1000) AND 
        (@f <= YEAR(GETUTCDATE()) - YEAR(@age)) AND 
        (@ti >= YEAR(GETUTCDATE()) - YEAR(@age)) AND 
        U.UserId != @UserId AND 
        NOT EXISTS (
            SELECT TOP 1
                IC1.InitiatorUserId
            FROM
                ExcludedCandidates AS IC1 WITH (NOLOCK)
            WHERE (
                    IC1.InitiatorUserId = @UserId AND
                    IC1.PartnerUserId = U.UserId
                ) OR (
                    IC1.InitiatorUserId = U.UserId AND
                    IC1.PartnerUserId = @UserId
                )
        ) AND 
        EXISTS(
            SELECT
                U.UserId
            FROM
                #UserInfo UserP WITH (NOLOCK)
            WHERE
                (UserP.gender & U.Ge) != 0 AND 
                UserP.Sea IN (1, 2) AND
                @s = UserP.Sea AND (UserP.age_lo <= YEAR(GETUTCDATE()) - YEAR(@age)) AND 
                (UserP.age_up >= YEAR(GETUTCDATE()) - YEAR(@age)) AND 
                UserP.UserId = U.UserId
        )
    ORDER BY
        U.Sea DESC
END

相关问题