Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Server collegato Inserisci-Seleziona prestazioni

Il modo più veloce è estrarre i dati anziché inviarli. Quando le tabelle vengono inviate, ogni riga richiede una connessione, un inserimento e una disconnessione.

Se non riesci a estrarre i dati, perché hai una relazione di fiducia unidirezionale tra i server, la soluzione consiste nel costruire l'intera tabella come una gigantesca istruzione T-SQL ed eseguirla tutta in una volta.

DECLARE @xml XML

SET @xml = (
        SELECT 'insert Remote_Table values (' + '''' + isnull(first_col, 'NULL') + ''',' +
            -- repeat for each col
            '''' + isnull(last_col, 'NULL') + '''' + ');'
        FROM Local_Table
        FOR XML path('')
        ) --This concatenates all the rows into a single xml object, the empty path keeps it from having <colname> </colname> wrapped arround each value

DECLARE @sql AS VARCHAR(max)

SET @sql = 'set nocount on;' + cast(@xml AS VARCHAR(max)) + 'set nocount off;' --Converts XML back to a long string

EXEC ('use RemoteDb;' + @sql) AT RemoteServer