Search code examples
azure-data-lakeu-sql

Does u-sql script executes in sequence?


I am supposed to do incremental load and using below structure. Do the statements execute in sequence i.e. TRUNCATE is never executed before first two statements which are getting data:

@newData = Extract ... (FROM FILE STREAM)

@existingData = SELECT * FROM dbo.TableA //this is ADLA table

@allData = SELECT * FROM @newData UNION ALL SELECT * FROM @existingData

TRUNCATE TABLE dbo.TableA;

INSERT INTO dbo.TableA SELECT * FROM @allData

Solution

  • To be very clear: U-SQL scripts are not executed statement by statement. Instead it groups the DDL/DML/OUTPUT statements in order and the query expressions are just subtrees to the inserts and outputs. But first it binds the data during compilation to their names, so your SELECT from TableA will be bound to the data (kind of like a light-weight snapshot), so even if the truncate is executed before the select, you should still be able to read the data from table A (note that permission changes may impact that).

    Also, if your script fails during the execution phase, you should have an atomic execution. That means if your INSERT fails, the TRUNCATE should be undone at the end.

    Having said that, why don't you use INSERT incrementally and use ALTER TABLE REBUILD periodically instead of doing the above pattern that reads the full table on every insertion?