jpql delete query is active since long time - java

jpql delete query is active for last 4 hours. When I tried to execute the same query directly on the database console it took around 30seconds to execute.The total data to delete is maximum 100000. I have index on id. I am unable to understand. Any suggestions would be appreciated.
Thanks
#Modifying
#Query("DELETE FROM IoEntity WHERE Id = :id")
void deleteAllById(#Param("id") UUID id);

Whenever you run a DML in your DB using SQL clients like Toad, SQL Developer etc, make sure you commit it unless auto commit is turned on in the client.
If you are doing the delete via JPA, commit will be taken care by Spring when you define #Transactional in your service method.

Related

Log how many items have been impacted in a Spring/Hibernate transaction

I have this method that is called from another service:
#Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void execute(String sql) {
Query query = entityManager.createNativeQuery(sql);
query.executeUpdate();
}
Basically the client loads multiple sql files and run each sql file in a new transaction, in order to not impact other files execution.
For example this is an example of an sql file, that is cleaning up some data:
begin;
delete from table t where t.created_at < current_date - interval '2 month';
commit;
What I'm trying to do is to log, the outcome of each transaction. For example here, I want to display how many records were deleted. How can I do that from Spring ? I know that you can log something more specific with:
logging.level.org.springframework.transaction=TRACE
, but still I cannot see any outcome. This reveals information about sql that will run and when transaction started/ended.
Second solution was to check the result of:
int count = query.executeUpdate();
, but count is 0, even though the sql code got executed and deletes hundreds of rows.
Thanks upfront for the suggestions !
The problem is as #XtremeBaumer correctly pointed out your script. If you just run executeUpdate with a delete statement it will return the number of affected rows.
But that is not what you are doing. You are executing a code block delimited by begin and end. There might be a way for such a code block to return a value, but that would need to be coded into the code block and is probably highly database specific.

How to get postgres schema from runinng query in Java

assume you do query on pg_stat_activity table and you get example result:
datid
datname
pid
usesysid
usename
application_name
client_addr
client_hostname
client_port
backend_start
xact_start
query_start
state_change
wait_event_type
wait_event
state
backend_xid
backend_xmin
query
backend_type
7198
10
rdsadmin
7195
16384
rdsadmin
32375
10
rdsadmin
PostgreSQL JDBC Driver
16409
c-t-s
21143
16410
c-t-s
c-t-s
10.10.3.1
48037
2021-01-18 13:19:03
2021-01-18 13:31:23
2021-01-18 13:31:23
Client
ClientRead
idle
COMMIT
client backend
I would like to know on which schema the query COMMIT was executed?
My case is i have schema-based multitenancy and i would like to distinguish between schemas (tenants). We always make a single-schema queries, so we dont mix them. To achieve that we set search_path on each getConnection method invocation. Code is developed in java and we dont use schema names in queries, as it is always dynamic -- taken from current request context and set in getConnection method.
With current result I dont know which tenant (schema) is causing slow / long queries.
I have tried to select from pg_class by ids taken from pg_stat_activity but without luck.
So far the comments did not answer my problem, is that possible at all?

Will entityManager.createQuery().executeUpdate() execute immediately on DB?

I'm using hibernate jpa. I'm trying to execute an update query and want to tell if the update actually happen or not based on the return value because I may have multiple processes doing the same updates and I only want to continue with the process that successfully update the record. (Relying on mysql return 1 iff the update happen successfully, 0 if nothing changed.)
int update = entityManager.createQuery("update Employee set name = \'xxxx\' where id=1").executeUpdate();
My question is that with default flush mode (AUTO), will this query being executed on the db? (if not, does the return value still make sense?) What about flush mode = COMMIT? Do I need to do entityManager.flush()? (I feel like the query execution will bypass the entityManager so flush doesn't do anything?)
What if I use JPA repository instead of the native query?
#Modifying
#Query("UPDATE Employee SET name = :name WHERE id = :id)
int update(long id, String name)
Will the return value reflects the rows changed in DB or just the persistent context?

no result after transforming postgres query to jpql in spring data jpa

I am trying to write a query to fetch list as this query is native sql query, all I need is to transform to spring jpql in which I am failing badly. if there is any link related to this please let me know
I am supposed to get list from this query. as this query is working fine with postgres console but when I even tried this with spring jpa as native query
it is showing results in console but not fetching in service layer [edit:] I mean not calculating any result set.
I am sure I am missing some important/small thing here.
below is the native postgres query
selcet t.id,count(*), count(*) filter (where t.status = 'DONE') from table t where t.id in ([list]) group by t.id
what Im trying is
SELECT t.id, count(t), count(t.id) where staus = 'DONE' from Table t where t.id in ([list]) group by t.id
edit: with constructor based query this is not even working
I am not even sure how to start this query
while being new to this I am not even able to start how to solve this.
Any hint, insight will be useful

Native Hibernate query works very slow from the application but very fast when I run it directly from MySQL workbench

In my application I use native query to fetch data:
SELECT
time_month,
CCC.closing_strike_value AS closing_strike_value,
CC.opening_strike_value AS opening_strike_value,
C.closing_time AS closing_time,
C.max_ask AS max_ask,
C.max_bid AS max_bid,
C.max_point_value AS max_point_value,
C.max_strike_value AS max_strike_value,
C.min_ask AS min_ask,
C.min_bid AS min_bid,
C.min_point_value AS min_point_value,
C.min_strike_value AS min_strike_value,
C.opening_time AS opening_time,
C.option_name AS option_name,
C.opening_time AS id
FROM
(SELECT
DATE(DATE_FORMAT(FROM_UNIXTIME(opening_time / 1000), '%Y-%m-01')) AS time_month,
MAX(closing_time) AS closing_time,
MAX(max_ask) AS max_ask,
MAX(max_bid) AS max_bid,
MAX(max_point_value) AS max_point_value,
MAX(max_strike_value) AS max_strike_value,
MIN(min_ask) AS min_ask,
MIN(min_bid) AS min_bid,
MIN(min_point_value) AS min_point_value,
MIN(min_strike_value) AS min_strike_value,
MIN(opening_time) AS opening_time,
option_name
FROM
candle_option
WHERE
option_name LIKE CONCAT('%', :optionName, '%')
AND opening_time BETWEEN :from AND :to
GROUP BY DATE(DATE_FORMAT(FROM_UNIXTIME(opening_time / 1000), '%Y-%m-01'))) C
JOIN
candle_option CC ON CC.opening_time = C.opening_time
AND CC.option_name = C.option_name
JOIN
candle_option CCC ON CCC.closing_time = C.closing_time
AND CCC.option_name = C.option_name
ORDER BY C.opening_time
Every column listed in the first select statement corresponds to a field within the Entity which I retrieve.
This native query works fine and returns valid results. However, there is one crucial problem - it fetches results dramatically slow even on small amounts of data when used by hibernate. However, when I run this query directly (I use MySQL Workbench) results are returned very fast. For instance, if this query is executed by hibernate, It takes about about 1 minute to get results; if I run this query directly from MySQL Workbench, then it takes only 100 milliseconds.
I wonder why is this happening and would appreciate any hint or help.
Thanks!

Categories