alias - MySQL query extreme slow filtering on CASE WHEN formula -


using mysql 5.6 encounter big performance problem when filtering on calculated formula using conditions case when else end syntax.

this sql formula mapped hibernate. around 6000 rows in database.
table foo has indices on columns product , barcode

1. slow 2-16 s

select      count(*)      foo f      (         case when f.product not null 1         else (             case when f.barcode null 0             else  (                 select exists(                     select 1                     product p                     p.barcode = f.barcode                     limit 1                 )             ) end         ) end     ) = 0  

explain results:

+----+--------------------+-------+------+-------------------------------+-----+---------+-----+-------+---------------------------------------------------+ | id |    select_type     | table | type |         possible_keys         | key | key_len | ref | rows  |                                             | +----+--------------------+-------+------+-------------------------------+-----+---------+-----+-------+---------------------------------------------------+ |  1 | primary            | f     |  |                               |     |         |     |   700 | using                                       | |  3 | dependent subquery | p     |  | uq_product,ix_product_barcode |     |         |     |  3134 | range checked each record (index map: 0x2008) | +----+--------------------+-------+------+-------------------------------+-----+---------+-----+-------+---------------------------------------------------+ 

2. fast ~ 0.4 s

select      *      foo f      (case ... end) = 0 

explain results identical count query.

first of should try @ output of explain garner more information.

but either way, let's try cleaning query bit , see if can't make use of few indexes. biggest smell thecase xxx = 0; wonder if query parser having trouble making efficient plan , calculating value per row , comparing result 0.

so let's rewrite as:

where f.product null , (     f.barcode null      or exists (select 1 product p p.barcode = f.barcode) ) 

if not fix problem, try updating index statistics analyze table.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -