1

The actual execution plan for my Azure SQL Database stored procedure indicated the following warning:

enter image description here

So I went and added a persisted calculated column in dbo.Interest_rate_changes like this:

[Effective_date]  AS (CONVERT([date],CONVERT([nchar](8),[Effective_date_int]),(112))) PERSISTED NOT NULL

I then replaced all references to Interest_date_int in the stored procedure with references to Interest_date.

I ran the query again but the same warning appeared in the actual execution plan, even though no reference to Effective_date_int now exists in the stored procedure.

I tried to clear the cache with DBCC FREEPROCCACHE but Azure SQL Database will not allow that.

What am I doing wrong?

In response to comments, the actual execution plan is here.

The query is this:

EXEC TEST_PopulateCachedDailyInterest 11,'2017-12-31'

The stored procedure is this (apologies I am not a pro):

CREATE PROCEDURE [dbo].[TEST_PopulateCachedDailyInterest] (@entityid int = 0, @enddate date)
AS

SET NOCOUNT ON;

TRUNCATE TABLE TEST_CachedDailyInterest;

WITH A AS
(
SELECT
    MyEntity AS Entity,
    MyInstrument AS Instrument,
    Accrual_date AS AccrualDate,
    Balance_x_Par_value_x_Effective_rate AS AccrualNumerator,
    Yearfrac_reciprocal AS AccrualDenominator

FROM

(SELECT
    MyEntity,
    MyInstrument,
    Instrument_currency,
    Interest_convention,
    Yearfrac_date_shift,
    Calendar_date AS Accrual_date,
    (SELECT SUM(Units) FROM Unit_transactions_indexed WITH (NOEXPAND) WHERE Entity = MyEntity AND Instrument = MyInstrument AND DATEADD(day,Opening_balance_date_shift,Transaction_date) < Calendar_date) * Par_value *
    (SELECT TOP 1 Interest_rate  FROM Interest_rate_changes WHERE Instrument = MyInstrument AND DATEADD(day,Interest_date_shift, Effective_date) <= Calendar_date ORDER BY Effective_date DESC) AS Balance_x_Par_value_x_Effective_rate

FROM

(SELECT  MyEntity, MyInstrument, Min_date, Max_date, Opening_balance_date_shift, Interest_date_shift, Interest_convention, Yearfrac_date_shift, Par_value, Instrument_currency FROM
(SELECT MyEntity, MyInstrument, DATEADD(day,-3,MIN(Transaction_date)) AS Min_date, IIF(SUM(Units)<>0, @enddate, IIF(DATEADD(day,3,MAX(Transaction_date))>=@enddate, @enddate, DATEADD(day,3,MAX(Transaction_date)))) AS Max_date
FROM
(SELECT MyEntity, MyInstrument, Transaction_date, Units FROM
(SELECT Entity AS MyEntity, Instrument AS MyInstrument, Transaction_date, Units FROM Unit_transactions_indexed WITH (NOEXPAND)
WHERE Entity = @entityid AND Units <> 0 AND Units IS NOT NULL) AS A
INNER JOIN
(SELECT ID FROM Instruments WHERE Interest_type>0)  B
ON  A.MyInstrument = B.ID) C
GROUP BY MyEntity, MyInstrument)  D
INNER JOIN Instruments ON D.MyInstrument = Instruments.ID
INNER JOIN Interest_types ON Interest_type = Interest_types.ID) AS D

CROSS JOIN Calendar_dates WHERE Calendar_date BETWEEN Min_date AND Max_date) AS F

INNER JOIN Yearfracs_reciprocal WITH (NOEXPAND) ON Balance_x_Par_value_x_Effective_rate<>0 AND
            Balance_x_Par_value_x_Effective_rate IS NOT NULL AND
            Yearfrac_reciprocal IS NOT NULL AND
            DATEADD(day,-F.Yearfrac_date_shift,F.Accrual_date) = Yearfracs_reciprocal.Calendar_date AND
            Interest_convention = Convention_ID
)

INSERT INTO TEST_CachedDailyInterest
SELECT
Entity AS EntityId,
Instrument AS InstrumentId,
AccrualDate,
AccrualNumerator,
AccrualDenominator
FROM A
WHERE ISNULL(AccrualNumerator,0)<>0

The table schema for dbo.Interest_rate_changes is this:

CREATE TABLE [dbo].[Interest_rate_changes](
    [Instrument] [int] NOT NULL,
    [Effective_date_int] [int] NOT NULL,
    [Interest_rate] [decimal](9, 7) NOT NULL,
    [Effective_date]  AS (CONVERT([date],CONVERT([nchar](8),[Effective_date_int]),(112))) PERSISTED NOT NULL,
 CONSTRAINT [PK_Instrument_Date] PRIMARY KEY CLUSTERED 
(
    [Instrument] ASC,
    [Effective_date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
Meneghino
  • 971
  • 6
  • 13
  • This post indicates that adding the column to dbo.Interest_rate_changes should have cleared the cache, but it has not https://stackoverflow.com/questions/1873025/how-can-i-clear-the-sql-server-query-cache?rq=1 – Meneghino Oct 28 '17 at 09:04
  • can you share your query and table schema as text – TheGameiswar Oct 28 '17 at 10:27
  • See this answer :https://dba.stackexchange.com/questions/143735/how-to-avoid-implicit-conversion-for-an-integer-column – TheGameiswar Oct 28 '17 at 10:44
  • Posting the plan would help clear this up. It's likely that it's a false positive. You can reference the column (in the SELECT statement), get the warning, but not actually have to worry at all about your statistics and the plan. Seeing the query and the plan would clear that up. – Grant Fritchey Oct 28 '17 at 11:09
  • @GrantFinchley the issue is that I have removed all references to the column, so I am not sure why the warning remains in the actual execution plan. – Meneghino Oct 28 '17 at 12:49
  • 2
    A reference to effective_date will still show the warning (which then still shows the underlying calculation column of effective_date_int). I can reproduce this with a simple SELECT effective_date FROM interest_rate_changes. I'll share this feedback with the team that introduced the warning. – Joe Sack Nov 06 '17 at 22:58

0 Answers0