1
the DBMS in question is SQL Server
In my system, we have a table that stores book releases. The table has a considerable amount of records, currently (around 1.2million).
Has an index clustered over a sequential numerator, which is the primary key of the table (no business rule, just a sequential number).
Has a Codclient column. Recently an index was created on it, to use in queries filtering by it.
However, with this, the consultation was extremely slow. Something "wrong" happened.. with the data already in memory, the query took 1 or 2 seconds dropping the Dice... and with the index created again, it takes several minutes!
The consultation was slow in 2 users I tested. I took a backup of one of them, and tested in my environment, the same behavior was identified (ie: should be the content really)
Detail: in this query has "Codcliente not in (subconsulta)..." Because it has the index, it may have changed so much because of this?
Can an Indice slow down a query in this case?
The insertion in the table, it is normal, the only problem was that in the delay of the query, ended up blocking the accounting table and gave timeout in many Insert in it - many same.
Consultation:
Select distinct CT.CodCliente,ct.codacesso FROM CT_Contabilidade CT (nolock)
Inner join CD_PlanoContas PC ON CT.CodAcesso = PC.Cod AND CT.CodPlano = PC.CodPlano
WHERE (CT.CodCliente NOT IN (
SELECT codcliente FROM ct_contabilidade SI (nolock)
WHERE SI.CodPlano = CT.CodPlano And SI.codcliente > 0 And SI.CodAcesso = CT.CodAcesso And Year(SI.Data) = 2016
AND month(SI.data) = 4 And ( SI.CodEmpresa = 1) )
)
And CT.CodPlano = 1 AND MONTH(CT.Data) < 4 AND YEAR(CT.Data) = 2016 AND PC.subgrupo = 'C'
And ( CT.CodEmpresa = 1 ) And ct.codCliente > 0
Below I will put the execution plan and the client statistics generated in the execution when THERE is the index: (I was going to put, but I could not put more than 2 links p/ images. It was only the customer statistics)
And in the sequence, the implementation plan and statistics generated by the execution of the query when DO NOT HAVE the index (much faster): (I was going to put, but I could not put more than 2 links p/ images. It was only the customer statistics)
Someone might give some light, what might have happened?
Try a few things. 1. Try replacing distinct with group by. 2. Try not to use filter functions like MONTH() - Try to do with date range. 3. See if there is any index with the fields used in Where. 4. Run a Database statistic.
– Marcelo