0
I have a query that when it runs it’s taking a little while to bring results. And I don’t know where to start to improve this query. If anyone can help me I’ll be grateful.
The query copies record that are in a reference table that has two columns (Name of all banks and all tables of my bank) and then joins the result of this table "REFERENCE" to power insert into another table "CONSULTATION" to list all the banks, tables, and other columns referring to those two columns which I mentioned before the table "REFERENCE". Here is the query:
/*INDICANDO QUE QUERO USAR O BANCO DADOS(OBRIGATÓRIO) */
use Dados
/*DECLARANDO VARIAVEIS PARA MINHA TABELA RESULTADO QUE NÃO É FÍSICA*/
DECLARE @Resultado TABLE
(
Tabelas nvarchar(max),
data_sincronizacao date,
rodovia nvarchar(max),
elemento nvarchar(max),
n_foto int
)
/*DECLARANDO VARIAVEIS PARA MINHA PROCEDURE */
DECLARE @Table_cursor CURSOR
DECLARE @rodovia nvarchar(MAX)
DECLARE @elemento nvarchar(MAX)
DECLARE @table_name nvarchar(MAX)
DECLARE @database_name nvarchar(MAX)
DECLARE @sql_insert nvarchar(MAX)
/*CHAMANDO MEU CURSOR,APONTADANDO ELE PERCORER A TABELA DE REFERENCIA A ONDE ESTÁ ARMAZENADO OS BANCOS E AS TEBALES COM TERMINO "ATTACH" */
SET @Table_cursor = CURSOR FORWARD_ONLY FOR
SELECT Banco, Tabela, Rodovia, Elemento from Dados.dbo.Referencia
/*LIMPANDO A TABELA "CONSULTA" PARA EVITAR DUPLICAÇÃO */
DELETE FROM Dados.dbo.Consulta
/*ABRINDO O CURSOR PARA PERCORER TODOS OS DADOS DA TABELA REFERENCIA TRAZENDO DADOS DAS RESPECTIVAS COLUNAS */
OPEN @Table_cursor
FETCH NEXT FROM @Table_cursor INTO @database_name, @table_name, @rodovia, @elemento
WHILE @@FETCH_STATUS = 0
BEGIN
--PRINT 'Inserindo dados do banco ' + @database_name + ' e tabela ' + @table_name
SET @sql_insert = CONCAT('INSERT INTO Consulta (Banco, Tabela, Rodovia, Elemento, n_foto, data_sincronizacao) SELECT ''', @database_name, ''', ''', @table_name, ''', ''', @rodovia, ''', ''', @elemento, ''', count(*) as n_foto, FORMAT (GDB_FROM_DATE, ''dd-MM-yyyy'') FROM ', @database_name, '.dbo.' + @table_name, ' GROUP BY FORMAT(GDB_FROM_DATE, ''dd-MM-yyyy'');')
EXECUTE sp_executesql @sql_insert
FETCH NEXT FROM @Table_cursor INTO @database_name, @table_name, @rodovia, @elemento
END
CLOSE @Table_cursor;
DEALLOCATE @Table_cursor;
You can try using the Display Estimated Execution Plan (Ctrl+L) and check if there is any point that is consuming more resource. Another detail is to check the Indexes tables, as well as set an index in the return table @Upshot.
– Leandro Paixão
@Leandropaixão I will give a check. thanks for the tip
– Gladiador