yessql icon indicating copy to clipboard operation
yessql copied to clipboard

Update index instead of deleting and adding

Open msc2402 opened this issue 4 years ago • 2 comments

Hi,

I was wondering if there is any way to update an existing MapIndex instead of deleting it and adding it. I'm currently facing the issue that the index table has an Integer Id with a limited max value.

I'm keeping track of certain crypto coins and storing them in the database. Using a websocket I'm receiving the latest prices continually of a specific coin which I will then save to the database. Currently my ID of the index table increments rather quickly, as I'm updating the Document record frequently.

What I fear is that I will reach the limitations of the int ID quickly this way.

TLDR: Is there any way to let an IndexProvider update an existing MapIndex record, instead of deleting + adding.

namespace Core.Indexes
{
    public class CandleIndex : MapIndex
    {
        public string Symbol { get; set; }
        public TimeInterval Interval { get; set; }

        public DateTime StartDate { get; set; }
    }

    public class CandleIndexProvider : IndexProvider<Candle>
    {
        public override void Describe(DescribeContext<Candle> context)
        {
            context.For<CandleIndex>()
                .Map(candle =>
                {
                    return new CandleIndex
                    {
                        Symbol = candle.Symbol,
                        Interval = candle.Interval,
                        StartDate = candle.StartDate
                    };
                });
        }
    }
}

The ID distance between yesterday's price candle index and today: image

EDIT Using SQL Server

msc2402 avatar Dec 23 '21 07:12 msc2402

I would suggest if it becomes a problem to reset the identity value

For sql server (you don't specify your database) https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-checkident-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15

deanmarcussen avatar Dec 23 '21 10:12 deanmarcussen

I don't think that solves the problem Dean as I'm not deleting my records and therefore I think this section in the remarks is relevant:

"If the table is not empty, setting the identity value to a number less than the maximum value in the identity column can result in one of the following conditions:

-If a PRIMARY KEY or UNIQUE constraint exists on the identity column, error message 2627 will be generated on later insert operations into the table because the generated identity value will conflict with existing values.

-If a PRIMARY KEY or UNIQUE constraint does not exist, later insert operations will result in duplicate identity values."

msc2402 avatar Dec 23 '21 11:12 msc2402