sql改寫
建立表和儲存過程
點選( 此處 )摺疊或開啟
-
CREATE TABLE customers AS
-
SELECT * FROM sh . customers where country_id in ( 52778 , 52772 ) ;
-
-
CREATE OR REPLACE FUNCTION f_count_obj ( p_country_id VARCHAR2 )
-
RETURN NUMBER AS
-
v_count NUMBER ;
-
BEGIN
-
SELECT COUNT ( * )
-
INTO v_count
-
FROM customers e26856649_1
-
WHERE country_id = p_country_id ;
-
RETURN v_count ;
-
END ;
-
/
點選( 此處 )摺疊或開啟
-
11 : 01 : 18 SQL > SET timing ON
-
SET autotrace traceonly
-
SELECT country_id , cust_first_name , f_count_obj ( country_id ) AS cnt FROM customers ;
-
-
4049 rows selected .
-
-
Elapsed : 00 : 00 : 02 . 80
-
-
Execution Plan
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
Plan hash value : 2008213504
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
| Id | Operation | Name | Rows | Bytes | Cost ( % CPU ) | Time |
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
| 0 | SELECT STATEMENT | | 3936 | 98400 | 33 ( ) | 00 : 00 : 01 |
-
| 1 | TABLE ACCESS FULL | CUSTOMERS | 3936 | 98400 | 33 ( ) | 00 : 00 : 01 |
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
Note
-
- - - - -
-
- dynamic sampling used for this statement ( level = 2 )
-
-
-
Statistics
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
4078 recursive calls
-
0 db block gets
-
437813 consistent gets
-
0 physical reads
-
0 redo size
-
99781 bytes sent via SQL * Net to client
-
3478 bytes received via SQL * Net from client
-
271 SQL * Net roundtrips to/from client
-
0 sorts ( memory )
-
0 sorts ( disk )
-
4049 rows processed
點選( 此處 )摺疊或開啟
-
SQL > SELECT fetches , executions , sql_text
-
FROM v$sql
-
WHERE sql_text LIKE '%26856649 _1 %'
-
AND sql_text NOT LIKE '%v$sql%'
-
AND sql_text NOT LIKE '%OPT_DYN_SAMP%' ;
-
-
FETCHES EXECUTIONS SQL_TEXT
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
4049 4049 SELECT COUNT ( * ) FROM CUSTOMERS E26856649_1 WHERE COUNTRY_ID = : B1
我們看到被解析 執行了4049次
下面我們新增如下引數
點選( 此處 )摺疊或開啟
-
CREATE OR REPLACE FUNCTION f_count_obj2 ( p_country_id VARCHAR2 )
-
RETURN NUMBER DETERMINISTIC AS
-
v_count NUMBER ;
-
BEGIN
-
SELECT
-
COUNT ( * )
-
INTO v_count
-
FROM customers e26856649_2
-
WHERE country_id = p_country_id ;
-
RETURN v_count ;
-
END ;
-
/
這裡指定別名為_2是為了區分v$sql中的sql_text
點選( 此處 )摺疊或開啟
-
SELECT country_id , cust_first_name , f_count_obj2 ( country_id ) AS cnt FROM customers;
點選( 此處 )摺疊或開啟
-
11 : 47 : 07 SQL > SELECT fetches , executions , sql_text
-
FROM v$sql
-
WHERE sql_text LIKE '%26856649_2%'
-
AND sql_text NOT LIKE '%v$sql%'
-
AND sql_text NOT LIKE '%OPT_DYN_SAMP%' ;
-
-
FETCHES EXECUTIONS SQL_TEXT
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
540 540 SELECT COUNT ( * ) FROM CUSTOMERS E26856649_2 WHERE COUNTRY_ID = : B1
結果看到這個被執行解析 540次,時間從2點幾秒,變成1.幾秒了。。。 。。。
有的時候,說函式很慢讓你改,你又不會改,那麼在函式里加這個引數,哈哈。
點選( 此處 )摺疊或開啟
-
SELECT o . country_id ,
-
o . cust_first_name ,
-
( SELECT COUNT ( * ) FROM customers t WHERE t . country_id = o . country_id ) AS cnt
-
FROM customers o
改成標量子查詢,時間不到1s
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29990276/viewspace-1485366/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL改寫優化SQL優化
- Oracle case when改寫SQLOracleSQL
- DB2 SQL改寫DB2SQL
- 一條SQL的改寫SQL
- MySQL的SQL等價改寫MySql
- 改寫不走索引的SQL索引SQL
- oracle sql tunning 15 --常用改寫OracleSQL
- SQL 改寫系列七:謂詞移動SQL
- SQL改寫的方法,select group by sumSQL
- SQL 改寫系列六:謂詞推導SQL
- 一種提升SQL改寫效率的方法SQL
- MySQL效能優化之簡單sql改寫MySql優化
- 用分析函式改寫冗長的sql函式SQL
- SQLServer效能優化之改寫SQL語句SQLServer優化
- SQL KEEP 視窗函式等價改寫案例SQL函式
- SQL抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL 抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL 改寫系列十:半連線轉內連線SQL
- 異構資料庫遷移 sql等價改寫資料庫SQL
- 【中亦安圖】SQL優化之基於SQL特徵的改寫(9)SQL優化特徵
- sql改寫優化:簡單規則重組實現SQL優化
- 由Delphi程式改寫Sql(2000)的儲存過程SQL儲存過程
- 改寫一個要跑5小時的SQL成1分鐘SQL
- SQL增刪改查SQL
- 小米開源自研智慧SQL優化與改寫工具SOAR使用指南SQL優化
- Oracle Rownum分頁改寫Oracle
- SQL Server最佳化標量函式改寫內聯表值函式SQLServer函式
- sql指令,增,刪,查,改SQL
- Oracle到PostgreSQL等價改寫OracleSQL
- 用WITH…AS改寫標量子查詢
- 多屏判斷css改寫CSS
- SQL 基礎增、刪、改、查SQL
- sql 常見增刪改查SQL
- 從兩張表中取資料的 SQL 能改寫成 Laravel Eloquent 的形式嗎?SQLLaravel
- sql優化案例:改變表的寫法使代價和邏輯讀降下來SQL優化
- Oracle SQL寫法OracleSQL
- 書寫高效sqlSQL
- 分析函式改寫自關聯函式