gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

112 líneas
8.3KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE STATISTICS</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-createserver.html" title="CREATE SERVER" /><link rel="next" href="sql-createsubscription.html" title="CREATE SUBSCRIPTION" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE STATISTICS</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createserver.html" title="CREATE SERVER">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createsubscription.html" title="CREATE SUBSCRIPTION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATESTATISTICS"><div class="titlepage"></div><a id="id-1.9.3.83.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE STATISTICS</span></h2><p>CREATE STATISTICS — define extended statistics</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE STATISTICS [ IF NOT EXISTS ] <em class="replaceable"><code>statistics_name</code></em>
  4. [ ( <em class="replaceable"><code>statistics_kind</code></em> [, ... ] ) ]
  5. ON <em class="replaceable"><code>column_name</code></em>, <em class="replaceable"><code>column_name</code></em> [, ...]
  6. FROM <em class="replaceable"><code>table_name</code></em>
  7. </pre></div><div class="refsect1" id="SQL-CREATESTATISTICS-DESCRIPTION"><h2>Description</h2><p>
  8. <code class="command">CREATE STATISTICS</code> will create a new extended statistics
  9. object tracking data about the specified table, foreign table or
  10. materialized view. The statistics object will be created in the current
  11. database and will be owned by the user issuing the command.
  12. </p><p>
  13. If a schema name is given (for example, <code class="literal">CREATE STATISTICS
  14. myschema.mystat ...</code>) then the statistics object is created in the
  15. specified schema. Otherwise it is created in the current schema.
  16. The name of the statistics object must be distinct from the name of any
  17. other statistics object in the same schema.
  18. </p></div><div class="refsect1" id="id-1.9.3.83.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  19. Do not throw an error if a statistics object with the same name already
  20. exists. A notice is issued in this case. Note that only the name of
  21. the statistics object is considered here, not the details of its
  22. definition.
  23. </p></dd><dt><span class="term"><em class="replaceable"><code>statistics_name</code></em></span></dt><dd><p>
  24. The name (optionally schema-qualified) of the statistics object to be
  25. created.
  26. </p></dd><dt><span class="term"><em class="replaceable"><code>statistics_kind</code></em></span></dt><dd><p>
  27. A statistics kind to be computed in this statistics object.
  28. Currently supported kinds are
  29. <code class="literal">ndistinct</code>, which enables n-distinct statistics,
  30. <code class="literal">dependencies</code>, which enables functional
  31. dependency statistics, and <code class="literal">mcv</code> which enables
  32. most-common values lists.
  33. If this clause is omitted, all supported statistics kinds are
  34. included in the statistics object.
  35. For more information, see <a class="xref" href="planner-stats.html#PLANNER-STATS-EXTENDED" title="14.2.2. Extended Statistics">Section 14.2.2</a>
  36. and <a class="xref" href="multivariate-statistics-examples.html" title="70.2. Multivariate Statistics Examples">Section 70.2</a>.
  37. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  38. The name of a table column to be covered by the computed statistics.
  39. At least two column names must be given; the order of the column names
  40. is insignificant.
  41. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  42. The name (optionally schema-qualified) of the table containing the
  43. column(s) the statistics are computed on.
  44. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.83.7"><h2>Notes</h2><p>
  45. You must be the owner of a table to create a statistics object
  46. reading it. Once created, however, the ownership of the statistics
  47. object is independent of the underlying table(s).
  48. </p></div><div class="refsect1" id="SQL-CREATESTATISTICS-EXAMPLES"><h2>Examples</h2><p>
  49. Create table <code class="structname">t1</code> with two functionally dependent columns, i.e.
  50. knowledge of a value in the first column is sufficient for determining the
  51. value in the other column. Then functional dependency statistics are built
  52. on those columns:
  53. </p><pre class="programlisting">
  54. CREATE TABLE t1 (
  55. a int,
  56. b int
  57. );
  58. INSERT INTO t1 SELECT i/100, i/500
  59. FROM generate_series(1,1000000) s(i);
  60. ANALYZE t1;
  61. -- the number of matching rows will be drastically underestimated:
  62. EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
  63. CREATE STATISTICS s1 (dependencies) ON a, b FROM t1;
  64. ANALYZE t1;
  65. -- now the row count estimate is more accurate:
  66. EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
  67. </pre><p>
  68. Without functional-dependency statistics, the planner would assume
  69. that the two <code class="literal">WHERE</code> conditions are independent, and would
  70. multiply their selectivities together to arrive at a much-too-small
  71. row count estimate.
  72. With such statistics, the planner recognizes that the <code class="literal">WHERE</code>
  73. conditions are redundant and does not underestimate the row count.
  74. </p><p>
  75. Create table <code class="structname">t2</code> with two perfectly correlated columns
  76. (containing identical data), and a MCV list on those columns:
  77. </p><pre class="programlisting">
  78. CREATE TABLE t2 (
  79. a int,
  80. b int
  81. );
  82. INSERT INTO t2 SELECT mod(i,100), mod(i,100)
  83. FROM generate_series(1,1000000) s(i);
  84. CREATE STATISTICS s2 (mcv) ON a, b FROM t2;
  85. ANALYZE t2;
  86. -- valid combination (found in MCV)
  87. EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1);
  88. -- invalid combination (not found in MCV)
  89. EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 2);
  90. </pre><p>
  91. The MCV list gives the planner more detailed information about the
  92. specific values that commonly appear in the table, as well as an upper
  93. bound on the selectivities of combinations of values that do not appear
  94. in the table, allowing it to generate better estimates in both cases.
  95. </p></div><div class="refsect1" id="id-1.9.3.83.9"><h2>Compatibility</h2><p>
  96. There is no <code class="command">CREATE STATISTICS</code> command in the SQL standard.
  97. </p></div><div class="refsect1" id="id-1.9.3.83.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterstatistics.html" title="ALTER STATISTICS"><span class="refentrytitle">ALTER STATISTICS</span></a>, <a class="xref" href="sql-dropstatistics.html" title="DROP STATISTICS"><span class="refentrytitle">DROP STATISTICS</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createserver.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createsubscription.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE SERVER </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE SUBSCRIPTION</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1