08.01.2015 Views

phsjhxx

phsjhxx

phsjhxx

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.8. Patterns<br />

5.8.1 Checking the Number of Rows<br />

74. Never use SELECT COUNT(*) if you are only interested in the existence of a row.<br />

[CodeXpert 5804]<br />

Reason: If you do a SELECT count(*) all rows will be read according to the WHERE<br />

clause, even if only the availability of data is of interest. For this we have a<br />

big performance overhead. If we do a SELECT count(*) .. WHERE<br />

ROWNUM = 1 there is also a overhead as there will be two<br />

communications between the PL/SQL and the SQL engine. See the<br />

following example for a better solution.<br />

Example:<br />

-- Bad<br />

...<br />

BEGIN<br />

SELECT count(*)<br />

INTO l_count<br />

FROM cust_order<br />

WHERE …<br />

IF l_count > 0<br />

THEN<br />

SELECT p.part_nbr, p.name, p.unit_cost<br />

FROM part p<br />

WHERE …<br />

-- Good<br />

...<br />

BEGIN<br />

SELECT p.part_nbr, p.name, p.unit_cost<br />

FROM part p<br />

WHERE EXISTS (SELECT 1<br />

FROM cust_order co<br />

WHERE co.part_nbr = p.part_nbr)<br />

…<br />

PL/SQL Coding Guidelines 43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!