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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- DB2 SQL改寫DB2SQL
- Oracle case when改寫SQLOracleSQL
- MySQL的SQL等價改寫MySql
- SQL改寫的方法,select group by sumSQL
- 一種提升SQL改寫效率的方法SQL
- SQL 改寫系列七:謂詞移動SQL
- SQL 改寫系列六:謂詞推導SQL
- MySQL效能優化之簡單sql改寫MySql優化
- SQL KEEP 視窗函式等價改寫案例SQL函式
- SQL 抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL 改寫系列十:半連線轉內連線SQL
- 異構資料庫遷移 sql等價改寫資料庫SQL
- sql改寫優化:簡單規則重組實現SQL優化
- SQL增刪改查SQL
- 小米開源自研智慧SQL優化與改寫工具SOAR使用指南SQL優化
- sql指令,增,刪,查,改SQL
- SQL Server最佳化標量函式改寫內聯表值函式SQLServer函式
- SQL 基礎增、刪、改、查SQL
- sql devloper 用法的和SQL 編寫SQLdev
- Oracle Rownum分頁改寫Oracle
- 從兩張表中取資料的 SQL 能改寫成 Laravel Eloquent 的形式嗎?SQLLaravel
- 基本 SQL 之增刪改查(二)SQL
- SQL改寫系列九:外連線轉內連線的常見場景與錯誤SQL
- Oracle到PostgreSQL等價改寫OracleSQL
- [20210428]改進pr.sql指令碼.txtSQL指令碼
- SQL改寫系列九:外連線轉內連線的常見場景與錯誤-2SQL
- SQL書寫規範(通用)SQL
- 分析函式改寫自關聯函式
- 前端工具Rome將用Rust改寫前端Rust
- SQL 層功能改進 - lookupJoin 的最佳化SQL
- 常用SQL語句1-增刪改查SQL
- MySQL 常用 SQL 增刪改查操作詳解MySql
- 5. SQL 編寫規範SQL
- sql語句抄寫作業SQL
- [20190430]注意sql hint寫法.txtSQL
- 工作日常-SQL不能亂寫SQL
- 複雜SQL分析和編寫SQL
- 如何寫出高效能SQLSQL