Simple SQL query timing out depending on input parameters

0

I have a very strange issue where depending on the query parameters the database call from the code below will fail with:

Exception.Message = Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

The code is:

        public IList<ProductCommissionMinimal> FindProductCommissionMinimal(string premiumTypeCode, string sellerId, string eposProductId, string singlePremiumPolicy)
        {
            var sql = @"
SELECT distinct
        CSP.ProductSubtypeId, PC.ProductFamilyId, PC.ProductId
    FROM
        ProductCommission PC,
        CommissionTemplate CT,
        Seller S,
        Product P,
        CoreSystemProduct CSP
    WHERE
        PC.ProductFamilyId = CSP.ProductFamilyId AND PC.ProductId = CSP.ProductId
        AND P.ProductFamilyId = CSP.ProductFamilyId AND P.ProductId = CSP.ProductId
        AND CT.PremiumTypeCd = @premiumTypeId
        AND S.SellerId = @sellerId
        AND CT.CommissionTemplateSeq = PC.CommissionTemplateSeq
        AND CT.StatusCd = 'O'
        AND PC.DistributionId = S.DistributionId
        AND CT.AvailabilityCd <> 'I'
        AND CSP.CoreProductId = @eposProductId";

            var parameters = new Dictionary<string, object>
            {
                { "@premiumTypeId", premiumTypeCode },
                { "@sellerId", sellerId },
                { "@eposProductId", eposProductId },
            };

            if (premiumTypeCode == "1")
            {
                sql = sql + " AND P.IsSinglePremiumOnlyCd = @singlePremiumPolicy";
                parameters["@singlePremiumPolicy"] = singlePremiumPolicy;
            }

            return Query<ProductCommissionMinimal>(sql, parameters);
        }

The Query method executes a generic method which ultimately executes System.Data.Common.DbCommand.ExecuteReader() This same generic method is used throughout our code base in hundreds of different inline queries without issue so I don't think there is any issue there. This generic method also uses the default command timeout i.e. none is specified so 30 seconds. However this SQL query should return almost immediately so I would not think that this is just simply a long running query.

The issue I'm seeing is that for some parameter combinations this works fine but errors for others. For example it seems to error for: premiumTypeCode=5&sellerId=NQ49&eposProductId=UPR3SAA&singlePremiumPolicy=Y but not premiumTypeCode=5&sellerId=NQ49&eposProductId=ABC3SAA&singlePremiumPolicy=Y

The one unique thing about this particular method is that it is the only one in our code base that uses the CoreProductId column. It seems to be if I change that parameter value by a few letters it works fine.

I'm losing my mind with this. Any insight would help greatly. The backend database is SQL Server 2016.

EDIT I've updated the query to this:

    public IList<ProductCommissionMinimal> FindProductCommissionMinimal(string premiumTypeCode, string sellerId, string eposProductId, string singlePremiumPolicy)
    {
        var sql = @"
SELECT distinct CSP.ProductSubtypeId, PC.ProductFamilyId, PC.ProductId
                    FROM
                        ProductCommission PC
                    INNER JOIN
                           CommissionTemplate CT 
                    ON 
                           CT.CommissionTemplateSeq = PC.CommissionTemplateSeq
                    INNER JOIN
                        Seller S 
                    ON 
                           PC.DistributionId = s.DistributionId
                    INNER JOIN
                        CoreSystemProduct CSP 
                    ON  
                           PC.ProductFamilyId = CSP.ProductFamilyId AND PC.ProductId = CSP.ProductId
                    INNER JOIN
                        Product P 
                    ON 
                           P.ProductId = CSP.ProductId AND P.ProductFamilyId = CSP.ProductFamilyId  
                    WHERE
                        CT.PremiumTypeCd = @premiumTypeId
                        AND S.SellerId = @sellerId       
                        AND CT.StatusCd = 'O'
                        AND PC.DistributionId = S.DistributionId
                        AND CT.AvailabilityCd <> 'I'
                        AND CSP.CoreProductId = @eposProductId";

        var parameters = new Dictionary<string, object>
        {
            { "@premiumTypeId", premiumTypeCode },
            { "@sellerId", sellerId },
            { "@eposProductId", eposProductId },
        };

        if (premiumTypeCode == "1")
        {
            sql = sql + " AND P.IsSinglePremiumOnlyCd = @singlePremiumPolicy";
            parameters["@singlePremiumPolicy"] = singlePremiumPolicy;
        }

        return Query<ProductCommissionMinimal>(sql, parameters);
    }

As recommended. However this is still failing for certain parameter combinations e.g. premiumTypeCode=1&sellerId=TC99&eposProductId=UPO2SAA&singlePremiumPolicy=N

c#
sql
sql-server
database
system.data
asked on Stack Overflow Apr 14, 2021 by Ben • edited Apr 15, 2021 by Ben

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0