You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/sgml/aqo.sgml
+85-39Lines changed: 85 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,8 @@
9
9
10
10
<para>
11
11
The <filename>aqo</filename> module is a <productname>&productname;</productname> extension for cost-based query
12
-
optimization. Using machine learning methods, in particular, the
13
-
k-NN algorithm, <filename>aqo</filename> improves cardinality estimation, which can speed
14
-
up query execution. In the learning modes, <filename>aqo</filename> collects statistics on all the executed
15
-
queries and classifies it according to the query type. If the
16
-
queries differ in their constants only, they belong to the same
17
-
type. For each type, <filename>aqo</filename> stores the cardinality quality, planning
18
-
time, and execution time. Based on this data, <filename>aqo</filename> builds the new query
19
-
plan and uses it for the next query of the same type.
12
+
optimization. Using machine learning methods, more precisely, a modification of the
13
+
k-NN algorithm, <filename>aqo</filename> improves cardinality estimation, which can improve execution plans and, consequently, speed up query execution.
20
14
</para>
21
15
<para>
22
16
With <filename>aqo</filename>, you can:
@@ -29,11 +23,15 @@
29
23
</listitem>
30
24
<listitem>
31
25
<para>
32
-
Experiment with plans for complex query types
26
+
Experiment with execution plans for complex queries
33
27
</para>
34
28
</listitem>
35
29
</itemizedlist>
36
-
<para>
30
+
<para>In the learning modes, <filename>aqo</filename> collects statistics on all the executed
31
+
queries and classifies it according to the query type. If the
32
+
queries differ in their constants only, they belong to the same
33
+
type. For each type, <filename>aqo</filename> stores the cardinality quality, planning
34
+
time, execution time, and execution statistics for machine learning. Based on this data, <filename>aqo</filename> builds the new query plan and uses it for the next query of the same type.
37
35
Experimental evaluation shows that <filename>aqo</filename> can significantly improve
To stop using <filename>aqo</filename> for query optimization, run:
81
79
</para>
82
80
<programlisting>
83
-
DROP EXTENSION aqo
81
+
DROP EXTENSION aqo;
84
82
</programlisting>
85
83
</sect2>
86
84
<sect2 id="aqo-usage">
@@ -125,28 +123,33 @@ DROP EXTENSION aqo
125
123
To dynamically change the <filename>aqo</filename> settings in your current session,
126
124
run the following command:
127
125
<programlisting>
128
-
set aqo.mode = <replaceable>'mode'</>
126
+
SET aqo.mode = <replaceable>'mode'</>;
129
127
</programlisting>
130
128
where <replaceable>mode</> is the name of the optimization mode to use.
131
129
</para>
132
130
<blockquote>
133
131
<para>
134
132
<emphasis role="strong">Important:</emphasis> The <filename>aqo</filename>
135
-
extension may not work well for queries with dynamically
133
+
extension will not work well for queries with dynamically
136
134
generated views. If such queries appear in your workload,
137
135
reset the optimization mode to <literal>aqo.mode='manual'</literal>.
138
136
</para>
139
137
</blockquote>
140
138
<para>
141
-
If you often run queries of the same type, or a specific query
142
-
type takes too long, you can use the intelligent mode to
143
-
optimize planning for such queries. In the intelligent mode, <filename>aqo</filename>
144
-
analyzes previous query execution and stores statistics in the
145
-
<literal>aqo_queries</literal> table. Statistics on queries of
139
+
If you often run queries of the same type and some query types takes too long because of non-optimal query execution plans, you can use the intelligent mode to
140
+
improve planning for such queries. In the intelligent mode, <filename>aqo</filename>
141
+
analyzes previous query execution and stores statistics. Statistics on queries of
146
142
different types is stored separately. If performance is not
147
143
improved after 50 iterations, the <filename>aqo</filename> extension falls back to
148
144
the default query planner.
149
145
</para>
146
+
<blockquote>
147
+
<para>
148
+
<emphasis role="strong">Note:</emphasis> You can view the
149
+
current query plan using the standard PostgreSQL <command>EXPLAIN</> command with the
150
+
<command>ANALYZE</> option. For details, see the <link linkend="using-explain">Using EXPLAIN</link> section.
151
+
</para>
152
+
</blockquote>
150
153
<para>
151
154
Since the intelligent mode tries to learn separately for
152
155
different query types, <filename>aqo</filename> may fail to optimize queries with
@@ -164,28 +167,17 @@ where <replaceable>mode</> is the name of the optimization mode to use.
164
167
manual mode.
165
168
</para>
166
169
<para>
167
-
When you have reached the required optimization level, you can
168
-
stop performance tuning and use the new plan for a
169
-
particular query type. To disable further performance tuning,
170
-
switch <filename>aqo</filename> to the manual mode. This can help you avoid tuning
171
-
overhead if you run multiple simple queries of the same type:
170
+
When you have reached the required optimization level in the intelligent mode, you can stop performance tuning by switching <filename>aqo</filename> to the manual mode:
172
171
</para>
173
172
<programlisting>
174
-
set aqo.mode = manual
173
+
SET aqo.mode = 'manual';
175
174
</programlisting>
176
175
<para>
177
176
In the manual mode, <filename>aqo</filename> does not collect statistics for new
178
177
query types, so they will not be optimized. For known query
179
178
types, <filename>aqo</filename> will continue using the optimized planning
180
179
algorithms.
181
180
</para>
182
-
<blockquote>
183
-
<para>
184
-
<emphasis role="strong">Note:</emphasis> You can view the
185
-
current query plan using the standard PostgreSQL <command>EXPLAIN</> command with the
186
-
<command>ANALYZE</> option. For details, see the <link linkend="using-explain">Using EXPLAIN</link> section.
187
-
</para>
188
-
</blockquote>
189
181
</sect3>
190
182
<sect3 id="aqo-advanced-query-tuning">
191
183
<title>Advanced Query Tuning</title>
@@ -204,15 +196,15 @@ set aqo.mode = manual
204
196
the <literal>aqo_query_texts</literal> table:
205
197
</para>
206
198
<programlisting>
207
-
select * from aqo_query_texts;
199
+
SELECT * FROM aqo_query_texts;
208
200
</programlisting>
209
201
<para>
210
202
Each query type has its own optimization settings. To review the
211
203
current settings for each type, view the
212
204
<literal>aqo_queries</literal> table:
213
205
</para>
214
206
<programlisting>
215
-
select * from aqo_queries;
207
+
SELECT * FROM aqo_queries;
216
208
</programlisting>
217
209
<para>
218
210
For each query type, the following settings are available:
@@ -258,17 +250,32 @@ select * from aqo_queries;
258
250
for a particular query type. For example:
259
251
</para>
260
252
<programlisting>
253
+
-- Add a new query type into the aqo_queries table:
254
+
261
255
SET aqo.mode='intelligent';
262
256
SELECT * FROM a, b WHERE a.id=b.id;
263
257
SET aqo.mode='manual';
258
+
259
+
-- Disable auto_tuning, enable both learn_aqo and use_aqo for this query type:
260
+
264
261
UPDATE aqo_queries SET use_aqo=true, learn_aqo=true, auto_tuning=false
265
262
WHERE query_hash = (SELECT query_hash from aqo_query_texts
266
263
WHERE query_text LIKE 'SELECT * FROM a, b WHERE a.id=b.id;');
264
+
265
+
-- Run EXPLAIN ANALYZE until the plan changes:
266
+
267
+
EXPLAIN ANALYZE SELECT * FROM a, b WHERE a.id=b.id;
268
+
EXPLAIN ANALYZE SELECT * FROM a, b WHERE a.id=b.id;
269
+
270
+
-- Disable learning to stop statistics collection and use the optimized plan:
271
+
272
+
UPDATE aqo_queries SET learn_aqo=false
273
+
WHERE query_hash = (SELECT query_hash from aqo_query_texts
274
+
WHERE query_text LIKE 'SELECT * FROM a, b WHERE a.id=b.id;');
267
275
</programlisting>
268
276
269
277
<para>
270
-
If your data or query distributions are rapidly changing, <filename>aqo</filename> may
271
-
learn longer than expected. To speed up learning, reset the
278
+
If your data or query distribution is rapidly changing, obsolete statistics may affect AQO performance. After a while, <filename>aqo</filename> will learn the new statistics, but it may take longer than expected. To speed up learning, reset the
272
279
statistics. To remove all the collected machine learning
273
280
statistics, run the following command:
274
281
</para>
@@ -285,26 +292,65 @@ DELETE FROM aqo_data WHERE fspace_hash = (SELECT fspace_hash FROM aqo_queries
285
292
WHERE query_text LIKE 'SELECT * FROM a, b WHERE a.id=b.id;'));
286
293
</programlisting>
287
294
295
+
<para>
296
+
To stop intelligent tuning for a particular query type, disable the <literal>auto_tuning</literal> setting:
297
+
<programlisting>
298
+
UPDATE aqo_queries SET auto_tuning=false WHERE query_hash = '<replaceable>hash</>';
299
+
</programlisting>
300
+
where <replaceable>hash</> is the hash value for this query type. As a result, <filename>aqo</filename> disables automatic changing of the <literal>learn_aqo</literal> and <literal>use_aqo</literal> settings.
301
+
</para>
302
+
288
303
<para>
289
304
To disable further learning for a particular query type, use the
290
305
following command:
291
306
<programlisting>
292
-
UPDATE aqo_queries SET auto_tuning=false WHERE fspase_hash = '<replaceable>hash</>';
307
+
UPDATE aqo_queries SET learn_aqo=false WHERE query_hash = '<replaceable>hash</>';
293
308
</programlisting>
294
309
where <replaceable>hash</> is the hash value for this query type.
295
310
</para>
296
311
297
312
<para>
298
-
To disable <filename>aqo</filename> for all queries and use the default <productname>PostgreSQL</>
313
+
To fully disable <filename>aqo</filename> for all queries and use the default <productname>PostgreSQL</>
299
314
query planner, run:
300
315
</para>
301
316
<programlisting>
302
-
UPDATE aqo_queries SET use_aqo=false, learn_aqo=false, auto_tuning=false;
317
+
UPDATE aqo_queries SET use_aqo=false, learn_aqo=false, auto_tuning=false;s
303
318
</programlisting>
304
319
</sect3>
305
320
</sect2>
306
321
<sect2 id="aqo-reference">
307
322
<title>Reference</title>
323
+
<sect3 id="aqo-variables">
324
+
<title>Configuration Variables</title>
325
+
<para>The aqo extension provides the following configuration variables:
326
+
</para>
327
+
<sect4 id="aqo-mode">
328
+
<title>aqo.mode</title>
329
+
<para>Defines the <filename>aqo</filename> optimization modes.
330
+
</para>
331
+
<para><emphasis role="strong">Options:</emphasis>
332
+
</para>
333
+
<itemizedlist spacing="compact">
334
+
<listitem>
335
+
<para>
336
+
<literal>intelligent</literal> - auto-tunes your queries based on statistics collected per query type.
337
+
</para>
338
+
</listitem>
339
+
<listitem>
340
+
<para>
341
+
<literal>forced</literal> - optimizes all queries together, regardless of the query type.
342
+
</para>
343
+
</listitem>
344
+
<listitem>
345
+
<para>
346
+
<literal>manual</literal> (default) - uses the default planner for all new queries, but can reuse the collected statistics for already known query types, if any.
0 commit comments