A netizen said his storage process has an update SQL, running for 15 minutes has not produced results, need to optimize
He sent me the SQL.
UPDATE tb_result R SET R.VOTE_COUNT=NVL (
SELECT Temp_. Vote_count
From (
SELECT result_id,
COUNT (rv_id) as Vote_count
From Tb_result_vote
GROUP by result_id
) Temp_
WHERE Temp_. result_id = r.result_id),
R.vote_count
);
Looking at this update SQL, split him into two parts, the red color part is the key to optimization
SELECT Temp_. Vote_count
From (
SELECT result_id,
COUNT (rv_id) as Vote_count
From Tb_result_vote
GROUP by result_id
) Temp_
WHERE Temp_. result_id = r.result_id
This is a very simple SQL, if there is a problem that basically appears in the subquery after the From
And it's interesting. The table in the self-query does not have a filter condition, which is equivalent to a full table scan
He said the watch Tb_result_vote a 90w.
I asked him to send out the execution plan, found all NL, and the execution plan was wrong.
This SQL optimization idea is tb_result with the temp table to go hash connection, temp table since is full table scan, need to cure and to parallel loading to improve speed
This will require you to rewrite the SQL
With Temp_ as (select/*+ materialize parallel (tb_result_vote,6) */result_id,count (rv_id) as Vote_count from Tb_result_vo Te Group by result_id)
Select/*+ Use_hash (temp_,r) */temp_.vote_count
From Temp_,tb_result R
where temp_.result_id=r.result_id;
I let him run this sql, less than 1s out of the results
My QQ is 343548233, and I hope to communicate with you about the optimization of SQL, as well as oracle aspects of Things
UPDATE SQL optimization