Performace in native query

3

Good morning, I'm on a project where I have to build charts based on queries made in a Postgres database.

This bank has a BI framework and the tables have an average 40 million lines .

The queries I'm supposed to make at the bank are being done through native querys by JPA , these queries take up to 5 minutes to return the data, but when executed in the PgAdmin or DBeaver (general id for banks based on the eclipse ide), the return time is 20 ms.

The application is in EJB , using JPA and Hibernate , we use JBoss EAP 6.3 as middleware, the connection to the database is being managed by the JBoss , could it be some configuration of it or is the problem the same?

Inquiry

select date_part('day', dt.dta) dia,    
    sum(distinct(fp.valorvendido)) venda,   
    sum(distinct(fp.valortotalcustodia)) custo,     
    sum(distinct(fp.valorlucrototal)) lucro,    
    round(((sum(fp.valorlucrototal) / sum(fp.valortotalcustodia)) * 100),2) margem,     
    sum(distinct(dp.qtdvendido)) qtd_vendido 
from ft_produto fp  
    inner join dmn_tempo dt         
        on dt.dta = ft.dta 
where fp.nrodivisao in (1, 2, 3)    
    and fp.nroempresa = 9   
    and fp.nrosegmento in (1, 2, 3, 4, 7, 8, 9, 10)     
    and dt.anomes = 201604  
    and fp.seqproduto = 20615 
group by dt.dta order by dia

Execution plan generated by PgAdmin

"Sort  (cost=444.86..444.86 rows=1 width=34)"
"  Sort Key: (date_part('day'::text, dt.dta))"
"  ->  GroupAggregate  (cost=444.27..444.85 rows=1 width=34)"
"        ->  Merge Join  (cost=444.27..444.81 rows=1 width=34)"
"              Merge Cond: (fp.dta = dt.dta)"
"              ->  Sort  (cost=434.74..434.99 rows=100 width=34)"
"                    Sort Key: fp.dta"
"                    ->  Bitmap Heap Scan on ft_produto dp  (cost=27.15..431.41 rows=100 width=34)"
"                          Recheck Cond: ((nroempresa = 9) AND (nrodivisao = ANY ('{1,2,3}'::bigint[])) AND (seqproduto = 20615))"
"                          Filter: (nrosegmento = ANY ('{1,2,3,4,7,8,9,10}'::integer[]))"
"                          ->  Bitmap Index Scan on ind_dmn_produto15  (cost=0.00..27.13 rows=101 width=0)"
"                                Index Cond: ((nroempresa = 9) AND (nrodivisao = ANY ('{1,2,3}'::bigint[])) AND (seqproduto = 20615))"
"              ->  Sort  (cost=9.51..9.59 rows=30 width=8)"
"                    Sort Key: dt.dta"
"                    ->  Index Scan using ind_dmn_tempo01 on dmn_tempo dt  (cost=0.00..8.78 rows=30 width=8)"
"                          Index Cond: (anomes = 201604)"
    
asked by anonymous 01.08.2016 / 15:16

1 answer

2

Take a look at the query generated by Hibernate and how this query gets to the database.

Probably the passed parameters may not be matching the correct type of columns involved in the table in the database and thus not using the index of the columns in the tables and generating this time greater in the return of the results.

In SQL Server, for example, I've already done a query per text field that expected the passed parameter to arrive as VARCHAR:

SELECT * from Pessoa WHERE nome = 'Paulo'

But the jTDS driver would put an N in front, converting it to NVARCHAR.

SELECT * from Pessoa WHERE nome = N'Paulo'

Not using the database index and damaging query performance.

    
01.08.2016 / 20:31