Certain operations can be accomplished in more than one way. For
example, there is often a join equivalent to a subquery. Consider
factors such as readability and maintenance, but generally you will
choose the query that runs fastest. The SAS system option STIMER shows
you the cumulative time for an entire procedure. The PROC SQL STIMER
option shows you how fast the individual statements in a PROC SQL
step are running. This enables you to optimize your query.
Note: For the PROC SQL STIMER option
to work, the SAS system option STIMER must also be specified.
This example
compares the execution times of two queries. Both queries list the
names and populations of states in the UNITEDSTATES table that have
a larger population than Belgium. The first query does this with a
join; the second with a subquery.
Comparing Run Times of Two Queries shows the STIMER results from the SAS log.
libname sql 'SAS-library';
proc sql stimer ;
select us.name, us.population
from sql.unitedstates as us, sql.countries as w
where us.population gt w.population and
w.name = 'Belgium';
select Name, population
from sql.unitedstates
where population gt
(select population from sql.countries
where name = 'Belgium');
Comparing Run Times of Two Queries
4 proc sql stimer ;
NOTE: SQL Statement used:
real time 0.00 seconds
cpu time 0.01 seconds
5 select us.name, us.population
6 from sql.unitedstates as us, sql.countries as w
7 where us.population gt w.population and
8 w.name = 'Belgium';
NOTE: The execution of this query involves performing one or more Cartesian
product joins that can not be optimized.
NOTE: SQL Statement used:
real time 0.10 seconds
cpu time 0.05 seconds
9
10 select Name, population
11 from sql.unitedstates
12 where population gt
13 (select population from sql.countries
14 where name = 'Belgium');
NOTE: SQL Statement used:
real time 0.09 seconds
cpu time 0.09 seconds
Compare the CPU time
of the first query (that uses a join), 0.05 seconds, with 0.09 seconds
for the second query (that uses a subquery). Although there are many
factors that influence the run times of queries, generally a join
runs faster than an equivalent subquery.