Si possible regrouper les COUNT sur une même table dans la même requête via un CASE, ce qui vous évitera de lire plusieurs fois la table en FULL.
Exemple
SQL> CREATE TABLE t1 AS
2 SELECT ora_hash(ROWNUM,1000) PRICE
3 FROM ALL_OBJECTS
4 WHERE ROWNUM < 1000;
Table created.
SQL> SELECT COUNT (*) FROM t1
2 WHERE price < 100 ;
COUNT(*)
----------
104
SQL> SELECT COUNT (*) FROM t1
2 WHERE price BETWEEN 100 AND 900 ;
COUNT(*)
----------
799
SQL> SELECT COUNT (*) FROM t1
2 WHERE price > 900 ;
COUNT(*)
----------
96
SQL> SET AUTOTRACE ON EXPLAIN
SQL> SELECT
2 COUNT (CASE WHEN price < 100
3 THEN 1 ELSE null END) count1,
4 COUNT (CASE WHEN price BETWEEN 100 AND 900
5 THEN 1 ELSE null END) count2,
6 COUNT (CASE WHEN price > 900
7 THEN 1 ELSE null END) count3
8 FROM t1;
COUNT1 COUNT2 COUNT3
---------- ---------- ----------
104 799 96
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3 Card=1 Bytes=13)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=3 Card=999 Byt
es=12987)
SQL> SET AUTOTRACE OFF
2 SELECT ora_hash(ROWNUM,1000) PRICE
3 FROM ALL_OBJECTS
4 WHERE ROWNUM < 1000;
Table created.
SQL> SELECT COUNT (*) FROM t1
2 WHERE price < 100 ;
COUNT(*)
----------
104
SQL> SELECT COUNT (*) FROM t1
2 WHERE price BETWEEN 100 AND 900 ;
COUNT(*)
----------
799
SQL> SELECT COUNT (*) FROM t1
2 WHERE price > 900 ;
COUNT(*)
----------
96
SQL> SET AUTOTRACE ON EXPLAIN
SQL> SELECT
2 COUNT (CASE WHEN price < 100
3 THEN 1 ELSE null END) count1,
4 COUNT (CASE WHEN price BETWEEN 100 AND 900
5 THEN 1 ELSE null END) count2,
6 COUNT (CASE WHEN price > 900
7 THEN 1 ELSE null END) count3
8 FROM t1;
COUNT1 COUNT2 COUNT3
---------- ---------- ----------
104 799 96
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3 Card=1 Bytes=13)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=3 Card=999 Byt
es=12987)
SQL> SET AUTOTRACE OFF
Aucun commentaire:
Enregistrer un commentaire