UPDATE SQL optimization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.