彻底搞懂oracle的标量子查询
oracle标量子查询和自定义函数有时用起来比较方便,而且开发人员也经常使用,数据量小还无所谓,数据量大,往往存在性能问题。 以下测试帮助大家彻底搞懂标量子查询。 SQL create table a (id int,name varchar2(10)); Table created. SQL create table b (i
oracle标量子查询和自定义函数有时用起来比较方便,而且开发人员也经常使用,数据量小还无所谓,数据量大,往往存在性能问题。
以下测试帮助大家彻底搞懂标量子查询。
Table created.
SQL> create table b (id int,name varchar2(10));
Table created.
SQL> insert into a values (1,'a1');
1 row created.
SQL> insert into a values (2,'a2');
1 row created.
SQL> insert into b values (1,'b1');
1 row created.
SQL> insert into b values (2,'b2');
1 row created.
SQL> commit;
Commit complete.
SQL> @getlvall
Session altered.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 2 | 1 | 2 |00:00:00.01 | 14 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 2 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行2次,返回2行
SQL> insert into a values (3,'a3');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
3 a3
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 3 | 1 | 2 |00:00:00.01 | 21 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 3 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行3次,返回2行
SQL> insert into a values (4,'a4');
1 row created.
SQL> insert into a values (5,'a5');
1 row created.
SQL> insert into a values (6,'a6');
1 row created.
SQL> insert into a values (7,'a7');
1 row created.
SQL> insert into a values (8,'a8');
1 row created.
SQL> insert into a values (9,'a9');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
3 a3
4 a4
5 a5
6 a6
7 a7
8 a8
9 a9
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 2 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行9次,返回2行
SQL> update b set name='b1';
2 rows updated.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b1
3 a3
4 a4
5 a5
6 a6
7 a7
8 a8
9 a9
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 2 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行2次,返回2行
SQL> insert into b values (3,'b1');
1 row created.
SQL> insert into b values (4,'b1');
1 row created.
SQL> insert into b values (5,'b1');
1 row created.
insert into b values (6,'b1');b1');
1 row created.
SQL> insert into b values (7,'b1');
1 row created.
SQL> insert into b values (8,'b1');
1 row created.
SQL> insert into b values (9,'b1');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b1
3 a3 b1
4 a4 b1
5 a5 b1
6 a6 b1
7 a7 b1
8 a8 b1
9 a9 b1
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 9 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
b.name字段全部为‘b1’,由上面的执行计划可以知道,b表执行9次,返回9行
SQL> update a set id=1;
9 rows updated.
SQL> commit;
Commit complete.
SQL> select * from a;
ID NAME
---------- --------------------
1 a1
1 a2
1 a3
1 a4
1 a5
1 a6
1 a7
1 a8
1 a9
9 rows selected.
SQL> select * from b;
ID NAME
---------- --------------------
1 b1
2 b1
3 b1
4 b1
5 b1
6 b1
7 b1
8 b1
9 b1
9 rows selected.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
1 a2 b1
1 a3 b1
1 a4 b1
1 a5 b1
1 a6 b1
1 a7 b1
1 a8 b1
1 a9 b1
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 1 | 1 | 1 |00:00:00.01 | 7 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
SQL>
关联字段a.id全部为1,a表有9行,标量子查询相当于执行9次select name from b where b.id=1 ,oracle也不傻,starts=1,说明只执行了1次。
总结:
理想状态下,a.id为主键,没有重复值,那么a表返回多少行,b表就要被执行多少次。
特殊情况下,a.id的distinct值只有n个,那么b表只执行n次。

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen



Um die Oracle -Tablespace -Größe abzufragen, führen Sie die folgenden Schritte aus: Bestimmen Sie den Namen Tablespace, indem Sie die Abfrage ausführen: Wählen Sie Tablespace_Name aus dba_tablespaces. Abfragen Sie die Tablespace -Größe durch Ausführen der Abfrage: Summe (Bytes) als Total_Size, sum (bytes_free) als verfügbare_space, sum

Mit der Verschlüsselung von Oracle View können Sie Daten in der Ansicht verschlüsseln und so die Sicherheit sensibler Informationen verbessern. Die Schritte umfassen: 1) Erstellen des Master -Verschlüsselungsschlüssels (MEK); 2) Erstellen einer verschlüsselten Ansicht, der Ansicht und der Verschlüsselung der Ansicht und der MEK angeben; 3) Benutzern Sie die Benutzer, auf die verschlüsselte Ansicht zuzugreifen. Wie verschlüsselte Ansichten funktionieren: Wenn ein Benutzer nach einer verschlüsselten Ansicht nachgefragt wird, verwendet Oracle MEK, um Daten zu entschlüsseln und sicherzustellen, dass nur autorisierte Benutzer auf lesbare Daten zugreifen können.

Das Erstellen einer Oracle -Tabelle umfasst die folgenden Schritte: Verwenden Sie die Tabellensyntax erstellen, um Tabellennamen, Spaltennamen, Datentypen, Einschränkungen und Standardwerte anzugeben. Der Tabellenname sollte präzise und beschreibend sein und 30 Zeichen nicht überschreiten. Der Spaltenname sollte beschreibend sein und der Datentyp gibt den in der Spalte gespeicherten Datentyp an. Die Nicht -Null -Einschränkung stellt sicher, dass Nullwerte in der Spalte nicht zulässig sind, und die Standardklausel gibt die Standardwerte für die Spalte an. Primärschlüsseleinschränkungen zur Ermittlung des eindeutigen Datensatzes der Tabelle. Die Einschränkung der Fremdschlüssel gibt an, dass sich die Spalte in der Tabelle auf den Primärschlüssel in einer anderen Tabelle bezieht. Siehe die Erstellung der Schülertabellenstudenten, die Primärschlüssel, eindeutige Einschränkungen und Standardwerte enthält.

Es gibt drei Möglichkeiten, Instanznamen in Oracle anzuzeigen: Verwenden Sie den "SQLPLUS" und "Instance_name aus v $ Instance". Befehle in der Befehlszeile. Verwenden Sie den "show Instance_name;" Befehl in SQL*Plus. Überprüfen Sie die Umgebungsvariablen (Oracle_sid unter Linux) über den Task -Manager des Betriebssystems, den Oracle Enterprise Manager oder über das Betriebssystem.

Datenimportmethode: 1. Verwenden Sie das SQLLoader -Dienstprogramm: Datendateien vorbereiten, Steuerdateien erstellen und SQLLOADER ausführen. 2. Verwenden Sie das IMP/Exp -Tool: Daten exportieren, Daten importieren. Tipp: 1. Empfohlener SQL*Loader für Big -Data -Sets; 2. Die Zieltabelle sollte vorhanden und die Spaltendefinition entspricht. 3. Nach dem Import muss die Datenintegrität überprüft werden.

Deinstallieren Sie die Methode für Oracle -Installationsfehler: Schließen Sie den Oracle -Service, löschen Sie Oracle -Programmdateien und Registrierungsschlüssel, Deinstallieren Sie Oracle -Umgebungsvariablen und starten Sie den Computer neu. Wenn das Deinstall fehlschlägt, können Sie das Oracle Universal Deinstall -Tool manuell deinstallieren.

Es gibt die folgenden Methoden, um Zeit in Oracle zu bekommen: current_timestamp: Gibt die aktuelle Systemzeit genau auf Sekunden zurück. SYSTIMESTAMP: genauer als Current_Timestamp, für Nanosekunden. SYSDATE: Gibt das aktuelle Systemdatum mit Ausnahme des Zeitteils zurück. To_char (sysdate, 'yyy-mm-dd hh24: mi: ss'): konvertiert das aktuelle Systemdatum und die Uhrzeit in ein bestimmtes Format. Extrakt: Extrahiert einen bestimmten Teil aus einem Zeitwert wie einem Jahr, Monat oder Stunde.

Ein AWR -Bericht ist ein Bericht, in dem die Datenbankleistung und Aktivitätsschnappschüsse angezeigt werden. Zu den Interpretationsschritten gehören: Identifizierung des Datums und der Uhrzeit des Aktivitäts -Snapshots. Sehen Sie sich einen Überblick über Aktivitäten und Ressourcenverbrauch an. Analysieren Sie Sitzungsaktivitäten, um Sitzungsarten, Ressourcenverbrauch und Warteeignisse zu finden. Finden Sie potenzielle Leistungsengpässe wie langsame SQL -Aussagen, Ressourcenbeständigkeit und I/A -Probleme. Wartenereignisse anzeigen, identifizieren und beheben Sie sie für die Leistung. Analysieren Sie die Muster für Latch- und Speichernutzung, um Speicherprobleme zu identifizieren, die Leistungsprobleme verursachen.
