Java EE web app, optimize queries - oracle

I'm building a Java EE web application using Servlets.
In a servlet I need to perform a series (30) of queries to an Oracle Database.
The queries are all of this type: count(*) from TABLE where CONDITIONS.
Each query takes 1 second, not too much, but the total amount of time is 30-35 seconds: that's too much!
Have you got any idea how I could improve the performances of my queries?
Is there a way to run them in parallel?

You could try to make one query of the 30:
select
(select count(*) from tbl_table1 where ...) as table1_cnt,
(select count(*) from tbl_table2 where ...) as table2_cnt,
...
(select count(*) from tbl_table30 where ...) as table30_cnt
from dual;
If this doesn't help and you cannot alter the db, one thing remained. Cache your query results at Java side, in the cache keep track of your insert/delete operations made by the Java application, and time-by-time refresh your cache from db in the background using a scheduler.

Related

query ORACLE database during springboot application no response

A sql like "SELECT COUNT(1) FROM tablename..." was executed by my springboot application with mybatis,but no response during a long time.
But when I executed the same sql directly in ORACLE,it worked and returned the response.
With executing these sql:
select event,count(*) from v$session_wait group by event order by 2 desc;
I found the 'latch:cache buffers chains' event.
I asked my frined and he told me that I can execute analyze table tablename compute statistics.
After executed the sql,the problem resolved,the select count sql executed by springboot application worked !
I feel puzzled,why the analyze sql worked?
Thank you.

Is optimizer_use_sql_plan_baselines and resource_manager_cpu_allocation oracle system parameter have impact on sql query performance

Is optimizer_use_sql_plan_baselines and resource_manager_cpu_allocation oracle system parameter have impact on sql query performance.
We have two envt suppose A and B. On A Envt query is running fine but in Envt. B its tacking time. I have compared system parameter and found difference in values in optimizer_use_sql_plan_baselines and resource_manager_cpu_allocation .
SQL plan baselines and the resource manager certainly could have a huge impact on performance, and you should use the below two queries or confirm or deny that those parameters are related to your problem.
GV$SQL stores which SQL plan baseline is associated with each SQL statement. Compare the SQL_PLAN_BASELINE column in the below query, and if they are equal then your problem is not related to baselines:
select sql_plan_baseline, round(elapsed_time/1000000) elapsed_seconds, gv$sql.*
from gv$sql
order by elapsed_time desc;
The Active Session History (ASH) views can tell you if the resource manager is an issue. If your queries are being throttled then you will see an event
named "resmgr:cpu quantum" in the below query. (But pay attention to the counts - don't troubleshoot a wait event if it only happens a small number of times.)
select nvl(event, 'CPU') event, count(*)
from gv$active_session_history
group by event
order by count(*) desc;
Resource manager can have other potentially negative affects. If you're in a data warehouse, and using parallel queries, it's possible that resource manager has downgraded the queries on one system. If you're using parallel queries, try comparing the SQL monitoring reports from both systems:
select dbms_sqltune.report_sql_monitor(sql_id => '&YOUR_SQL_ID') from dual;
However, I have a feeling that you're using the wrong approach for your problem. There are generally two approaches to Oracle database performance - database tuning and query tuning. If you're only interested in a single query, then you should probably focus on things like the execution plan and the wait events for the operations of that specific query.

How can rebuild index in IOT(Index organized table)?

Dear all experts.
I have IOT having 7 million records in oracle database, eventually iot use for fast access primary key but in my case, when i select primary key column it takes 5-4 seconds for select single column.
My query is:
Select Emp_Refno from Emp_master where Rownum =1 order
by Emp_Refno asc;
I have also used Sql Tunning Advisor for optimize it and also get index suggest ion from SQL Tunning Advisor and also applied it, But in explain plan not seen this index and it takes same time after it.
I'm curious if the following query has the same execution time:
select * from (select Emp_Refno from Emp_master order by Emp_Refno asc) where rownum = 1
This is how I usually write top-n queries for Oracle.

Hibernate generated query is slow in application but fast when executed manually

I'm using Spring DATA JPA with Hibernate + PostgreSQL, and I generate q query like this:
SELECT A.field1 AS field1_10_,
A.field2 AS field2_10_,
A.field3 AS field3_10_,
...
A.field10 as field10_10_
FROM mytable A
WHERE (A.field1 BETWEEN 1 AND 2)
AND ((cast(A.field1 AS varchar(255))||cast(A.field2 AS varchar(255))) IN
(SELECT (cast(B.field1 AS varchar(255))||cast(max(B.field2) AS varchar(255)))
FROM mytable B
WHERE B.field1 BETWEEN 1 AND 2
GROUP BY B.field2))
LIMIT 20
All A and B fields are numeric.
If I copy that query and execute it directly in Postgres (using Pgadmin), the result comes in less than a second. But in my application, it takes about a minute for Hibernate to retrieve the result. I have Hibernate statistics activated and the gross of the time is running JDBC statements.
I'm using Spring pagination, what generates a count query using the same specification. This query runs fast (less that a second):
SELECT count(*) AS col_0_0_
FROM mytable A
WHERE (A.field1 BETWEEN 1 AND 2)
AND ((cast(A.field1 AS varchar(255))||cast(A.field2 AS varchar(255))) IN
(SELECT (cast(B.field1 AS varchar(255))||cast(max(B.field2) AS varchar(255)))
FROM mytable B
WHERE B.field1 BETWEEN 1 AND 2
GROUP BY B.field2))
Any idea?
Update: I've just found out that the generatedstatement executed by PostgreSQL is the following:
SELECT A.field1 AS field1_10_,
A.field2 AS field2_10_,
A.field3 AS field3_10_,
...
A.field10 as field10_10_
FROM mytable A
WHERE (A.field1 BETWEEN ? AND ?)
AND ((cast(A.field1 AS varchar(255))||cast(A.field2 AS varchar(255))) IN
(SELECT (cast(B.field1 AS varchar(255))||cast(max(B.field2) AS varchar(255)))
FROM mytable B
WHERE B.field1 BETWEEN ? AND ?
GROUP BY B.field2))
LIMIT $1
Please note that the replacement character in the BETWEEN conditions is the question mark ? but in the limit is the dollar along with the parameter order $1. The limit is automatically added by Spring pagination, if I remove it the query is fast.
Update 2: I've just found out that the problem happens only when specifying a limit of 1, with bigger values all works as expected. Weird.
There are multiple possible causes.
JPA will fetch all rows before returning you the result. Make sure PGAdmin does the same by scrolling to the very end of the result before stopping your timer.
JPA (and in some cases Spring) will convert the ResultSet you get from the database into entities or some other Java objects. If you do non-trivial stuff in constructors or getters, this might cause a performance penalty. Also if your session is really big this might as well cause performance problems. These should become obvious, once you attach a profiler.
The query has to get created from a Specification (in your case), this might take some time as well. Again: attach a profiler to identify this.

Tuning the below Sql query

I have a database of Old version Oracle 8.1.7 there I have been running the below Union query
select c_ordine_es
,c_ordine_salesnet
,v_oyov
,v_annuale
,v_oneoff
,v_canone
,c_operatore_tam
from v_ordine_cliente_easysell
where d_ultima_modifica>DataRif
union
select c_ordine_es
,c_ordine_salesnet
,v_oyov
,v_annuale
,v_oneoff
,v_canone
,c_operatore_tam
from v_ordine_cliente_easysell v
,scarti_interfaccia_easysell_o s
where s.c_codice_es=v.c_ordine_es
and s.t_tabella_es=pkType.K_SCARTO_ORDINE_CLIENTE;
Every time I run these query the SQL Client(I am using Toad) hangs. Here I must mention data in v_ordine_cliente_easysell and scarti_interfaccia_easysell_o these two views/synonyms are fetched using DB Link(To another SIEBEL DB). I guess the problem is happening at the time of fetching data via DB_LINK, as the SIEBEL DB is alwas very busy. Would you please suggest me how could I tune the above query?
The Explain plan goes like below
OPERATION OPTIONS OBJECT_NODE POSITION COST CARDINALITY BYTES
SELECT STATEMENT 28 28 478912 61779648
HASH JOIN 1 28 478912 61779648
INDEX FAST FULL SCAN 1 2 11354 102186
REMOTE SIEB.WORLD 2 23 4218 506160
Make a local copy of table v_ordine_cliente_easysell.
Make a local copy of table scarti_interfaccia_easysell_o filtered with
t_tabella_es=pkType.K_SCARTO_ORDINE_CLIENTE.
Run the query on local copies, but using UNION ALL

Resources