I have a database created in approx. a decade, this same bank has several records and obviously, being in production, can not be changed from day to night.
The problem is:
The products registered in the bank have serial, these serial at the beginning of all there was no need to record the check digit, however, nowadays the digit is necessary to generate reports and everything.
In order to calculate this digit, it is a module of 10 with the 19 numbers of the serial, to perform this calculation, we have a SQL server function that starts a while
to break and add the digits of the serial. Another while
to perform the required calculations and then a if
, to return the equivalent number in delta.
Below the function:
DECLARE @delta AS TABLE ( indice INT, valor INT )
INSERT INTO @delta
( indice, valor )
VALUES ( 0, 0 ),
( 1, 1 ),
( 2, 2 ),
( 3, 3 ),
( 4, 4 ),
( 5, -4 ),
( 6, -3 ),
( 7, -2 ),
( 8, -1 ),
( 9, 0 )
DECLARE @serie VARCHAR(50) = '############'
DECLARE @deltaindex INT
DECLARE @deltavalue INT
DECLARE @soma INT = 0
DECLARE @i INT = 1
WHILE ( @i <= LEN(@serie) )
BEGIN
SET @soma = @soma + CAST(SUBSTRING(@serie, @i, 1) AS INT)
SET @i = @i + 1
END
SET @i = LEN(@serie)
WHILE ( @i > 0 )
BEGIN
SET @deltaindex = CAST(SUBSTRING(@serie, @i, 1) AS INT)
SELECT @deltavalue = valor
FROM @delta
WHERE indice = @deltaindex
SET @soma = @soma + @deltavalue
SET @i = @i - 2
END
DECLARE @mod10 INT
SET @mod10 = @soma % 10
SET @mod10 = 10 - @mod10
IF ( @mod10 = 10 )
BEGIN
SET @mod10 = 0
END
PRINT @mod10
That said, we have approx. 1.5KK of records that need to be displayed in a report and each record has between 1 and 2 serial and this query by serial ends up quite delaying the display of the report, enough that I can not issue a report by the system much less by the bank , taking around 1H30 to issue and even then I had to cancel the query.
I would like to know if there is any way to optimize the 10-module so that the query does not go so far.