Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which SQL has a better performance?

I have 2 sql statements as below. They can retrieve the same dataset. As I tested, it seems SQL2 always has a better performance. And I explained both statements. They have the same execution plan.

Can anyone help to explain if it is performance impacting when using subquery here? And how can we use subquery to do sql tuning?

Thanks very much!

--SQL 1
SELECT ADDRRF.ENTITYKEYID,
    ADDRESS1,
    ADDRESS2
FROM WODS_STG.BLIS_ADDRESSREFERENCE ADDRRF
LEFT JOIN WODS_STG.BLIS_ADDRESS ADDR
    ON ADDRRF.ADDRESSID = ADDR.ADDRESSID
WHERE ADDRRF.ADDRESSTYPEID = 10
    AND ADDRRF.ENTITYID = 3;

--SQL 2
SELECT ADDRRF.ENTITYKEYID,
    ADDRESS1,
    ADDRESS2
FROM (
    SELECT *
    FROM WODS_STG.BLIS_ADDRESSREFERENCE
    WHERE ADDRESSTYPEID = 10
        AND ENTITYID = 3
    ) ADDRRF
LEFT JOIN WODS_STG.BLIS_ADDRESS ADDR
    ON ADDRRF.ADDRESSID = ADDR.ADDRESSID;
like image 814
Yinan Xu Avatar asked Sep 19 '26 13:09

Yinan Xu


1 Answers

The second query can be faster because it reads data from the SGA area that was cached by the first query. In different words, the first query use hard parse and the second use soft parse, because they have the same execution plan. Try to run several times both of them to find out if the second query is still faster.

like image 199
neshkeev Avatar answered Sep 22 '26 03:09

neshkeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!