Delen via


Use transactions with dedicated SQL pool in Azure Synapse Analytics

Tips for implementing transactions with dedicated SQL pool in Azure Synapse Analytics for developing solutions.

Wat u kunt verwachten

As you would expect, dedicated SQL pool supports transactions as part of the data warehouse workload. However, to ensure the performance of dedicated SQL pool is maintained at scale some features are limited when compared to SQL Server. This article highlights the differences and lists the others.

Transaction isolation levels

Dedicated SQL pool implements ACID transactions. Het isolatieniveau van de transactionele ondersteuning is standaard ingesteld op READ UNCOMMITTED. You can change it to READ COMMITTED SNAPSHOT ISOLATION by turning ON the READ_COMMITTED_SNAPSHOT database option for a user database when connected to the master database.

Zodra deze database is ingeschakeld, worden alle transacties in deze database uitgevoerd onder READ COMMITTED SNAPSHOT ISOLATION en wordt het instellen van READ UNCOMMITTED op sessieniveau niet gehonoreerd. Controleer de opties voor ALTER DATABASE SET (Transact-SQL) voor meer informatie.

Transactiegrootte

Eén transactie voor het wijzigen van gegevens is beperkt in grootte. De limiet wordt per distributie toegepast. As such, the total allocation can be calculated by multiplying the limit by the distribution count.

Als u het maximum aantal rijen in de transactie wilt benaderen, deelt u de distributielimiet door de totale grootte van elke rij. Voor kolommen met variabele lengte kunt u overwegen om een gemiddelde kolomlengte te nemen in plaats van de maximale grootte te gebruiken.

In the table below the following assumptions have been made:

  • Er is een gelijkmatige verdeling van gegevens opgetreden
  • The average row length is 250 bytes

Gen2

DWU Cap per distribution (GB) Aantal distributies MAXIMALE transactiegrootte (GB) # Rows per distribution Maximum aantal rijen per transactie
DW100c 1 60 60 4.000.000 240,000,000
DW200c 1.5 60 90 6,000,000 360.000.000
DW300c 2,25 60 135 9,000,000 540,000,000
DW400c 3 60 180 12,000,000 720,000,000
DW500c 3,75 60 225 15.000.000 900,000,000
DW1000c 7.5 60 450 30,000,000 1,800,000,000
DW1500c 11,25 60 675 45,000,000 2,700,000,000
DW2000c 15 60 900 60.000.000 3,600,000,000
DW2500c 18.75 60 1125 75,000,000 4,500,000,000
DW3000c 22.5 60 1,350 90,000,000 5,400,000,000
DW5000c 37,5 60 2,250 150,000,000 9,000,000,000
DW6000c 45 60 2,700 180,000,000 10,800,000,000
DW7500c 56.25 60 3,375 225,000,000 13,500,000,000
DW10000c 75 60 4.500 300,000,000 18,000,000,000
DW15000c 112.5 60 6,750 450,000,000 27,000,000,000
DW30000c 225 60 13,500 900,000,000 54,000,000,000

Gen1

DWU Cap per distribution (GB) Aantal distributies MAXIMALE transactiegrootte (GB) # Rows per distribution Maximum aantal rijen per transactie
DW100 1 60 60 4.000.000 240,000,000
DW200 1.5 60 90 6,000,000 360.000.000
DW300 2,25 60 135 9,000,000 540,000,000
DW400 3 60 180 12,000,000 720,000,000
DW500 3,75 60 225 15.000.000 900,000,000
DW600 4.5 60 270 18.000.000 1,080,000,000
DW1000 7.5 60 450 30,000,000 1,800,000,000
DW1200 9 60 540 36,000,000 2,160,000,000
DW1500 11,25 60 675 45,000,000 2,700,000,000
DW2000 15 60 900 60.000.000 3,600,000,000
DW3000 22.5 60 1,350 90,000,000 5,400,000,000
DW6000 45 60 2,700 180,000,000 10,800,000,000

De limiet voor de transactiegrootte wordt per transactie of bewerking toegepast. Deze wordt niet toegepast op alle gelijktijdige transacties. Daarom mag elke transactie deze hoeveelheid gegevens naar het logboek schrijven.

To optimize and minimize the amount of data written to the log, refer to the Transactions best practices article.

Waarschuwing

De maximale transactiegrootte kan alleen worden bereikt voor HASH- of ROUND_ROBIN-gedistribueerde tabellen waarbij de verspreiding van de gegevens gelijkmatig is. Als de transactie gegevens op een scheve manier naar de distributies schrijft, wordt de limiet waarschijnlijk bereikt vóór de maximale transactiegrootte.

Transactiestatus

Dedicated SQL pool uses the XACT_STATE() function to report a failed transaction using the value -2. Deze waarde betekent dat de transactie is mislukt en alleen is gemarkeerd voor terugdraaien.

Opmerking

Het gebruik van -2 door de XACT_STATE-functie om een mislukte transactie aan te geven, vertegenwoordigt een ander gedrag voor SQL Server. SQL Server gebruikt de waarde -1 om een niet-commiteerbare transactie weer te geven. SQL Server kan bepaalde fouten in een transactie tolereren zonder dat deze als niet-commiteerbaar moet worden gemarkeerd. For example SELECT 1/0 would cause an error but not force a transaction into an uncommittable state. SQL Server also permits reads in the uncommittable transaction. However, dedicated SQL pool does not let you do this. If an error occurs inside a dedicated SQL pool transaction it will automatically enter the -2 state and you will not be able to make any further select statements until the statement has been rolled back. It is therefore important to check that your application code to see if it uses XACT_STATE() as you may need to make code modifications.

For example, in SQL Server you might see a transaction that looks like the following:

SET NOCOUNT ON;
DECLARE @xact_state smallint = 0;

BEGIN TRAN
    BEGIN TRY
        DECLARE @i INT;
        SET     @i = CONVERT(INT,'ABC');
    END TRY
    BEGIN CATCH
        SET @xact_state = XACT_STATE();

        SELECT  ERROR_NUMBER()    AS ErrNumber
        ,       ERROR_SEVERITY()  AS ErrSeverity
        ,       ERROR_STATE()     AS ErrState
        ,       ERROR_PROCEDURE() AS ErrProcedure
        ,       ERROR_MESSAGE()   AS ErrMessage
        ;

        IF @@TRANCOUNT > 0
        BEGIN
            ROLLBACK TRAN;
            PRINT 'ROLLBACK';
        END

    END CATCH;

IF @@TRANCOUNT >0
BEGIN
    PRINT 'COMMIT';
    COMMIT TRAN;
END

SELECT @xact_state AS TransactionState;

De voorgaande code geeft het volgende foutbericht:

Msg 111233, niveau 16, staat 1, regel 1 111233; De huidige transactie is afgebroken en alle wijzigingen die in behandeling zijn, zijn teruggedraaid. Cause: A transaction in a rollback-only state wasn't explicitly rolled back before a DDL, DML, or SELECT statement.

U krijgt geen uitvoer van de ERROR_*-functies.

In dedicated SQL pool, the code needs to be slightly altered:

SET NOCOUNT ON;
DECLARE @xact_state smallint = 0;

BEGIN TRAN
    BEGIN TRY
        DECLARE @i INT;
        SET     @i = CONVERT(INT,'ABC');
    END TRY
    BEGIN CATCH
        SET @xact_state = XACT_STATE();

        IF @@TRANCOUNT > 0
        BEGIN
            ROLLBACK TRAN;
            PRINT 'ROLLBACK';
        END

        SELECT  ERROR_NUMBER()    AS ErrNumber
        ,       ERROR_SEVERITY()  AS ErrSeverity
        ,       ERROR_STATE()     AS ErrState
        ,       ERROR_PROCEDURE() AS ErrProcedure
        ,       ERROR_MESSAGE()   AS ErrMessage
        ;
    END CATCH;

IF @@TRANCOUNT >0
BEGIN
    PRINT 'COMMIT';
    COMMIT TRAN;
END

SELECT @xact_state AS TransactionState;

Het verwachte gedrag wordt nu waargenomen. De fout in de transactie wordt beheerd en de functies ERROR_* bieden waarden zoals verwacht.

Het enige dat is gewijzigd, is dat het terugdraaien van de transactie moest plaatsvinden voordat de foutinformatie in het CATCH-blok werd gelezen.

Error_Line() functie

It is also worth noting that dedicated SQL pool does not implement or support the ERROR_LINE() function. If you have this function in your code, you need to remove it to be compliant with dedicated SQL pool. Gebruik in plaats daarvan querylabels in uw code om equivalente functionaliteit te implementeren. For more information, see the LABEL article.

Use of THROW and RAISERROR

THROW is the more modern implementation for raising exceptions in dedicated SQL pool but RAISERROR is also supported. Er zijn echter enkele verschillen die de moeite waard zijn om aandacht te besteden.

  • User-defined error messages numbers can't be in the 100,000 - 150,000 range for THROW
  • RAISERROR-foutberichten zijn vastgezet op 50.000
  • Het gebruik van sys.messages wordt niet ondersteund

Beperkingen

Dedicated SQL pool does have a few other restrictions that relate to transactions. Ze zijn als volgt:

  • Geen gedistribueerde transacties
  • Geneste transacties zijn niet toegestaan
  • Er zijn geen opslagpunten toegestaan
  • Geen benoemde transacties
  • Geen gemarkeerde transacties
  • Geen ondersteuning voor DDL, zoals CREATE TABLE binnen een door de gebruiker gedefinieerde transactie

Volgende stappen

Voor meer informatie over het optimaliseren van transacties, zie Best practices voor transacties. Additional best practices guides are also provided for Dedicated SQL pool and serverless SQL pool.