Different performance using limit less than 100 and greater than 100

Asked

Viewed 42 times

-2

I have a subselect that filters 16 records and there is a limit of 100.

This consultation results in a time usually of 1:20 ~ 1:40 minutes. But when I change the limit to a number greater than 100, be it 200, 300 or 1k the result is returned in less than 1 second. The time is lower also when using a limit less than or equal to the amount of records available.

I performed a EXPLAIN of this select and there really is a change in the way the system performs the search.

I wonder if there is how to optimize this search while keeping a low limit. Mysql has some limit-related configuration to cause this big difference?

select consultas.id       
  from consultas                          
 where find_in_set(status,'Ok')
   and tipo_doc like '%qualquer%'
   and id_empresas in (6226,6416)
   and cnpj != '09966999000100'
   and case utilizado 
       when "S" then "Importado"  
       when "N" then "Pendente"
        end like '%Pendente%'  
 order by consultas.id desc                         
 limit 1000(ou 100)

Explain example with limit = 100 Exemplo Explain com limit = 100

Explain example with limit = 1k Exemplo Explain com limit = 1k

  • How this table is indexed consultas?

  • I have the primary key in id, foreign in id_empresas and an index in cnpj. Looking at the documentation I saw that it may be something related to limit along with order by, but I didn’t understand what is the rule that governs which search it will use. When I add an order by document number (More than one order) the problem is solved, in explain I can see that the search rule changes and the limit of 100 becomes the limit of 1k.

1 answer

-2

I didn’t understand why use the case in this context, find_in_set(status,'Ok') instead of status like '%Ok%', changing the query as below improves performance?

select consultas.id       
  from consultas                          
 where status like '%Ok%'
   and tipo_doc like '%qualquer%'
   and id_empresas in (6226,6416)
   and cnpj != '09966999000100'
   and utilizado = 'N'
 order by consultas.id desc                         
 limit 1000(ou 100)
  • We had to adapt Select to insert here in the S.O. But this is because it receives more values, it is a very dynamic SQL to filter documents. Answering your question; no, it does not improve, I did tests using the equal operator with the absolute value and also does not solve. The case is a bad adaptation for other systems that send the "Pending" or "Imported" integer value and the status may contain other 5 types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.