Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CUBRIDMAN-224] Manual for correlated subquery result cache #526

Merged
merged 71 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
eb9d411
add subquerycache related texts
xmilex-git Jun 13, 2024
1bc152f
add example
xmilex-git Jun 20, 2024
c691256
add english ver
xmilex-git Jun 20, 2024
e6f1925
vscode fix
xmilex-git Jun 20, 2024
6f9fe61
update english
xmilex-git Jun 20, 2024
ac47dd5
add blank line
xmilex-git Jun 20, 2024
2b350fa
move to alphabetical loc
xmilex-git Jun 20, 2024
65d5067
apply code review(1)
xmilex-git Jun 20, 2024
b40818b
Update ko/sql/tuning.rst
xmilex-git Jun 21, 2024
53ee79b
add examples
xmilex-git Jun 21, 2024
999b822
Update en/sql/tuning.rst
xmilex-git Jun 21, 2024
c419e5a
Update en/sql/tuning.rst
xmilex-git Jun 21, 2024
f9f62f5
Update en/sql/tuning.rst
xmilex-git Jun 21, 2024
b410a8c
apply code review (2)
xmilex-git Jun 21, 2024
5286bc7
apply code review (leading hint)
xmilex-git Jun 28, 2024
cfb8c2f
add connect by
xmilex-git Jul 18, 2024
38b4006
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
b1e29ef
Update ko/admin/config.rst
xmilex-git Aug 2, 2024
2174934
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
2619430
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
f3cf032
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
10d1fe0
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
0344207
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
7285854
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
4758dee
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
20b0ed4
Update ko/admin/config.rst
xmilex-git Aug 2, 2024
ad256ca
Update en/admin/config.rst
xmilex-git Aug 2, 2024
4f742e6
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
466297a
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
51e3b3e
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
740ffd7
Update en/admin/config.rst
xmilex-git Aug 2, 2024
a671d83
Update ko/admin/config.rst
xmilex-git Aug 2, 2024
2e98753
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
b85b56c
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
951ab7b
Update ko/sql/tuning.rst
xmilex-git Aug 2, 2024
30d7584
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
0f15d76
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
0c57ec6
Update en/sql/tuning.rst
xmilex-git Aug 2, 2024
9cb1bfb
applying code review
xmilex-git Aug 7, 2024
ac62556
단위 설명
xmilex-git Aug 7, 2024
2667a5e
문맥 조정
xmilex-git Aug 7, 2024
57d2642
순서 조정
xmilex-git Aug 7, 2024
722a825
apply code review
xmilex-git Aug 8, 2024
1f6c14a
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
20f38cb
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
c2f7c50
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
b555a30
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
09506ee
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
927f4b6
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
c6f5d3d
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
6db90ff
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
af28b8d
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
faca4dc
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
8edf01b
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
8d2c092
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
2c72cfd
apply code review (2)
xmilex-git Aug 8, 2024
058e25d
코드 리뷰 영문 적용
xmilex-git Aug 8, 2024
93e1634
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
61acafa
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
e56c955
Update ko/sql/tuning.rst
xmilex-git Aug 8, 2024
f397238
코드리뷰 영문적용
xmilex-git Aug 8, 2024
359594a
Merge branch 'CUBRID:develop' into CUBRIDMAN-224
xmilex-git Aug 8, 2024
74cd8cd
Update en/admin/config.rst
xmilex-git Aug 9, 2024
3164386
Update en/sql/tuning.rst
xmilex-git Aug 9, 2024
6d577e7
Update en/sql/tuning.rst
xmilex-git Aug 9, 2024
f6cddc3
Update ko/sql/tuning.rst
xmilex-git Aug 9, 2024
7f78de2
Apply suggestions from code review
xmilex-git Aug 9, 2024
33eadb8
Apply suggestions from code review
xmilex-git Aug 9, 2024
c6f0c49
질의 프로파일링 관련 링크 추가
xmilex-git Aug 9, 2024
c000bee
Apply suggestions from code review
xmilex-git Aug 9, 2024
fdd8714
Apply suggestions from code review
xmilex-git Aug 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions en/admin/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ On the below table, if "Applied" is "server parameter", that parameter affects t
| +-------------------------------------+-------------------------+---------+----------+--------------------------------+-----------------------+
| | max_hash_list_scan_size | server parameter | | byte | 8,388,608(8M) | |
| +-------------------------------------+-------------------------+---------+----------+--------------------------------+-----------------------+
| | max_subquery_cache_size | server parameter | | byte | 2,097,152(2M) | DBA only |
| +-------------------------------------+-------------------------+---------+----------+--------------------------------+-----------------------+
| | sort_buffer_size | server parameter | | byte | 128 * | |
| | | | | | :ref:`db_page_size <dpg>` | |
| +-------------------------------------+-------------------------+---------+----------+--------------------------------+-----------------------+
Expand Down Expand Up @@ -674,6 +676,8 @@ The following are parameters related to the memory used by the database server o
+--------------------------------+--------+---------------------------+---------------------------+---------------------------+
| max_hash_list_scan_size | byte | 8,388,608(8M) | 0 | 128MB |
+--------------------------------+--------+---------------------------+---------------------------+---------------------------+
| max_subquery_cache_size | byte | 2,097,152(2M) | 0 | 16,777,216(16M) |
+--------------------------------+--------+---------------------------+---------------------------+---------------------------+
| sort_buffer_size | byte | 128 * | 1 * | 2G(32bit), |
| | | :ref:`db_page_size <dpg>` | :ref:`db_page_size <dpg>` | INT_MAX * |
| | | | | :ref:`db_page_size <dpg>` |
Expand Down Expand Up @@ -714,6 +718,12 @@ The following are parameters related to the memory used by the database server o

If this parameter is set to 0 or If :ref:`NO_HASH_LIST_SCAN <no-hash-list-scan>` hint is specified, hash list scan will not be used.

**max_subquery_cache_size**

**max_subquery_cache_size** parameter is used to set the size of the subquery cache (correlated). You can set a unit as B, K or M, which stand for bytes, kilobytes (KB),and megabytes (MB) respectively. If you omit the unit, bytes will be applied. The default value is **2,097,152** (2M) bytes, the minimum value is **0**, and the maximum value is **16,777,216** (16M) bytes. The subquery cache is allocated for the number of subqueries in a query and is deallocated when the main query is completed.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

If max_subquery_cache_size is set to 0, the :ref:`NO_SUBQUERY_CACHE <correlated-subquery-cache>` hint is specified, or there is insufficient memory space, the subquery cache optimization is not enabled.

**sort_buffer_size**

**sort_buffer_size** is a parameter to configure the size of buffer to be used when a query is processing sorting. The server assigns one sort buffer for each client's sorting-request, and releases the assigned buffer memory when sorting is complete. A sorting query includes not only SELECT sorting query, but also index-creating query.
Expand Down
166 changes: 166 additions & 0 deletions en/sql/tuning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ Using hints can affect the performance of query execution. You can allow the que
NO_COVERING_IDX |
NO_MULTI_RANGE_OPT |
NO_SORT_LIMIT |
NO_SUBQUERY_CACHE |
NO_PUSH_PRED |
NO_MERGE |
NO_ELIMINATE_JOIN |
Expand Down Expand Up @@ -724,6 +725,7 @@ The following hints can be specified in **UPDATE**, **DELETE** and **SELECT** st
* **NO_COVERING_IDX**: This is a hint not to use the covering index. For details, see :ref:`covering-index`.
* **NO_MULTI_RANGE_OPT**: This is a hint not to use the multi-key range optimization. For details, see :ref:`multi-key-range-opt`.
* **NO_SORT_LIMIT**: This is a hint not to use the SORT-LIMIT optimization. For more details, see :ref:`sort-limit-optimization`.
* **NO_SUBQUERY_CACHE**: This is a hint not to use the SUBQUERY CACHE optimization. For more details, see :ref:`correlated-subquery-cache`.
* **NO_PUSH_PRED**: This is a hint not to use the PREDICATE-PUSH optimization.
* **NO_MERGE**: This is a hint not to use the VIEW-MERGE optimization.
* **NO_ELIMINATE_JOIN**: This is a hint not to use join elimination optimization. For more details, see :ref:`join-elimination-optimization`.
Expand Down Expand Up @@ -4189,3 +4191,167 @@ The user can check the query to be cached or not by putting the session command
}

The cached query is shown as **query_string** in the middle of the result screen. Each of the **n_entries** and **n_pages** represents the number of cached queries and the number of pages in the cached results. The **n_entries** is limited to the value of configuration parameter **max_query_cache_entries** and the **n_pages** is limited to the value of **query_cache_size_in_pages**. If the **n_entries** is overflown or the **n_pages** is overflown, some victims among the cache entries are selected and they are uncached. The number of victims is about 20% of **max_query_cache_entries** value and of the **query_cache_size_in_pages** value.

.. _correlated-subquery-cache:

SUBQUERY CACHE (correlated)
------------------------------------

Subquery cache optimization can enhance the performance of queries including correlated subqueries by caching each subquery's results in private memory space per subquery.
This optimization is enabled by default.
To disable it during query execution, use the NO_SUBQUERY_CACHE hint on the target subquery.

When the correlated subquery is in the SELECT clause, subquery cache is enabled.
The processing mechanism of the cache is as follows:
If the column values ​​of the search conditions of the re-executed correlated subquery are the same, the cached results are used instead of executing the subquery.
If the cached value cannot be found in the subquery cache, after executing the subquery, the retrieved column value and results are stored in the subquery cache.

When executing a query using :ref:`query profiling <query-profiling>`\, the profile results for the subquery cache are displayed as part of the profile for correlated subqueries.

The following example is displayed subquery cache profiling information ,when performing a correlated subquery.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

::

csql> SELECT /*+ RECOMPILE NO_MERGE */
(SELECT t1_pk FROM t1 b WHERE b.t1_pk = a.c3)
FROM t1 a
WHERE a.c2 >= 1;

Trace Statistics:
SELECT (time: 108, fetch: 103639, fetch_time: 13, ioread: 0)
SCAN (table: dba.t1), (heap time: 70, fetch: 100384, ioread: 0, readrows: 100000, rows: 99000)
SUBQUERY (correlated)
SELECT (time: 4, fetch: 2970, fetch_time: 0, ioread: 0)
SCAN (index: dba.t1.pk_t1), (btree time: 2, fetch: 1980, ioread: 0, readkeys: 990, filteredkeys: 0, rows: 990, covered: true)
SUBQUERY_CACHE (hit: 98010, miss: 990, size: 269384, status: enabled)

Descriptions for each item are as follows:

* **hit**: The number of retrieves from the cache without executing the query
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
* **miss**: The number of stored into the cache after executing the query. (miss in the cache)
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
* **size**: The memory size used by the subquery cache.
* **status**: The activation status of the subquery cache at the end of executing the query.

During query execution, if the **size** of the subquery cache exceeds the set value, or the **size** does not exceed the set value, but the **miss**/**hit** ratio exceeds 9 (the miss rate is determined to be high), the subquery cache is disabled, and the **status** of the profiling information is marked as disabled.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

The following example is a query that counts the results of a correlated subquery that is executed repeatedly to check performance improvement depending on whether subquery caching is used.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
The following query makes data to perform example queries.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

::

# Prepare data
csql> DROP TABLE IF EXISTS t1;

csql> CREATE TABLE t1 AS
SELECT
ROWNUM AS t1_pk,
MOD(ROWNUM, 10) AS c1,
MOD(ROWNUM, 100) AS c2,
MOD(ROWNUM, 1000) AS c3
FROM
db_class a, db_class b, db_class c, db_class d, db_class e, db_class f
LIMIT 100000;

csql> ALTER TABLE t1 ADD CONSTRAINT PRIMARY KEY pk_t1 (t1_pk);

csql> CREATE TABLE t2 AS
SELECT
ROWNUM as c1,
1 as c2,
TO_CHAR(ROWNUM * 1000, '0999') as code
FROM
db_class a, db_class b
LIMIT 10;

csql> update statistics on t1 with fullscan;

csql> ;trace on

Query #1 is executed using subquery cache optimization, while Query #2 is executed without the subquery cache optimization using the NO_SUBQUERY_CACHE hint.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
By comparing the results of the two queries, you can see subquery cache optimization improves response performance.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

+------------------------------------------------------------------------+------------------------------------------------------------------------------------------------+
| **Query #1** | **Query #2** |
+========================================================================+================================================================================================+
| :: | :: |
| | |
| csql> SELECT COUNT(*) | csql> SELECT COUNT(*) |
| FROM ( | FROM ( |
| SELECT /*+ RECOMPILE NO_MERGE */ | SELECT /*+ RECOMPILE NO_MERGE */ |
| (SELECT t1_pk FROM t1 b WHERE b.t1_pk = a.c3) | (SELECT /*+ NO_SUBQUERY_CACHE */ t1_pk FROM t1 b WHERE b.t1_pk = a.c3) |
| FROM t1 a | FROM t1 a |
| WHERE a.c2 >= 1 | WHERE a.c2 >= 1 |
| ); | ); |
| | |
| === <Result of SELECT Command in Line 2> === | === <Result of SELECT Command in Line 2> === |
| | |
| count(*) | count(*) |
| ====================== | ====================== |
| 99000 | 99000 |
| | |
| 1 row selected. (0.128251 sec) Committed. (0.000010 sec) | 1 row selected. (0.626199 sec) Committed. (0.000011 sec) |
| | |
| 1 command(s) successfully processed. | 1 command(s) successfully processed. |
+------------------------------------------------------------------------+------------------------------------------------------------------------------------------------+

Subquery cache optimization does not work in the following scenarios:

* When the correlated subquery contains another correlated subquery. (However, correlated subqueries not included other correlated subqueries is worked subquery cache optimization.)
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
* When the subquery is not in the SELECT clause.
* When the subquery includes CONNECT BY clause.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
* When the subquery includes OID-related features.
* When the subquery includes the **NO_SUBQUERY_CACHE** hint.
* When storing new results exceeds the set subquery cache size (default: 2MB).
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved
* When the subquery contains functions that change results with each execution, such as random() or sys_guid().
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

The following example shows the case where subquery caching is applied only to the lowest level correlated subquery, while the upper level correlated subquery does not utilize subquery caching.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

::

csql> SELECT /*+ recompile */
(
SELECT
(
SELECT c.code
FROM t2 c
WHERE c.c1 = b.c1
)
FROM t1 b
WHERE b.t1_pk = a.c1
) s
FROM t1 a
WHERE a.c3 = 1;

Trace Statistics:
SELECT (time: 56, fetch: 100785, fetch_time: 10, ioread: 0)
SCAN (table: dba.t1), (heap time: 55, fetch: 100384, ioread: 0, readrows: 100000, rows: 100)
SUBQUERY (correlated)
SELECT (time: 0, fetch: 401, fetch_time: 0, ioread: 0)
SCAN (index: dba.t1.pk_t1), (btree time: 0, fetch: 300, ioread: 0, readkeys: 100, filteredkeys: 0, rows: 100) (lookup time: 0, rows: 100)
SUBQUERY (correlated)
SELECT (time: 0, fetch: 1, fetch_time: 0, ioread: 0)
SCAN (table: dba.t2), (heap time: 0, fetch: 1, ioread: 0, readrows: 10, rows: 1)
SUBQUERY_CACHE (hit: 99, miss: 1, size: 150704, status: enabled)

The following example shows the deactivation of subquery caching when the correlated subquery includes functions like random(), which yield different results with each execution.
xmilex-git marked this conversation as resolved.
Show resolved Hide resolved

::

csql> WITH cte_1 AS
(SELECT
DISTINCT (SELECT random(1) FROM t2 b WHERE b.c1 = a.c1 AND b.c2 = 1) v
FROM t1 a
WHERE a.c2 = 1
) SELECT count(*) FROM cte_1;

Trace Statistics:
SELECT (time: 65, fetch: 101384, fetch_time: 9, ioread: 0)
SCAN (temp time: 0, fetch: 0, ioread: 0, readrows: 1000, rows: 1000)
SUBQUERY (uncorrelated)
CTE (non_recursive_part)
SELECT (time: 65, fetch: 101384, fetch_time: 9, ioread: 0)
SCAN (table: dba.t1), (heap time: 59, fetch: 100384, ioread: 0, readrows: 100000, rows: 1000)
ORDERBY (time: 0, sort: true, page: 0, ioread: 0)
SUBQUERY (correlated)
SELECT (time: 4, fetch: 1000, fetch_time: 0, ioread: 0)
SCAN (table: dba.t2), (heap time: 3, fetch: 1000, ioread: 0, readrows: 10000, rows: 1000)
Loading
Loading