Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8828689

Browse files
committed
Make an editorial pass over the newly SGML-ified contrib documentation.
Fix lots of bad markup, bad English, bad explanations.Second round of commits. pgcrypto and pgstandby still to go...
1 parenta3102ce commit8828689

File tree

12 files changed

+2285
-1937
lines changed

12 files changed

+2285
-1937
lines changed

‎doc/src/sgml/intagg.sgml

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,145 @@
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/intagg.sgml,v 1.3 2007/12/10 05:32:51 tgl Exp $ -->
12

23
<sect1 id="intagg">
34
<title>intagg</title>
4-
5+
56
<indexterm zone="intagg">
67
<primary>intagg</primary>
78
</indexterm>
89

910
<para>
10-
This section describes the <literal>intagg</literal> module which provides an integer aggregator and an enumerator.
11+
The <filename>intagg</filename> module provides an integer aggregator and an
12+
enumerator.
13+
</para>
14+
15+
<sect2>
16+
<title>Functions</title>
17+
18+
<para>
19+
The aggregator is an aggregate function
20+
<function>int_array_aggregate(integer)</>
21+
that produces an integer array
22+
containing exactly the integers it is fed.
23+
Here is a not-tremendously-useful example:
1124
</para>
25+
26+
<programlisting>
27+
test=# select int_array_aggregate(i) from
28+
test-# generate_series(1,10,2) i;
29+
int_array_aggregate
30+
---------------------
31+
{1,3,5,7,9}
32+
(1 row)
33+
</programlisting>
34+
1235
<para>
13-
Many database systems have the notion of a one to many table. Such a table usually sits between two indexed tables, as:
36+
The enumerator is a function
37+
<function>int_array_enum(integer[])</>
38+
that returns <type>setof integer</>. It is essentially the reverse
39+
operation of the aggregator: given an array of integers, expand it
40+
into a set of rows. For example,
1441
</para>
42+
43+
<programlisting>
44+
test=# select * from int_array_enum(array[1,3,5,7,9]);
45+
int_array_enum
46+
----------------
47+
1
48+
3
49+
5
50+
7
51+
9
52+
(5 rows)
53+
</programlisting>
54+
55+
</sect2>
56+
57+
<sect2>
58+
<title>Sample Uses</title>
59+
60+
<para>
61+
Many database systems have the notion of a one to many table. Such a table
62+
usually sits between two indexed tables, for example:
63+
</para>
64+
1565
<programlisting>
16-
CREATE TABLE one_to_many(left INT, right INT) ;
66+
CREATE TABLE left (id INT PRIMARY KEY, ...);
67+
CREATE TABLE right (id INT PRIMARY KEY, ...);
68+
CREATE TABLE one_to_many(left INT REFERENCES left, right INT REFERENCES right);
1769
</programlisting>
1870

1971
<para>
20-
And it is used like this:
72+
It is typically used like this:
2173
</para>
2274

2375
<programlisting>
24-
SELECT right.* from right JOIN one_to_many ON (right.id = one_to_many.right)
25-
WHEREone_to_many.left = item;
76+
SELECT right.* from right JOIN one_to_many ON (right.id = one_to_many.right)
77+
WHERE one_to_many.left =<replaceable>item</>;
2678
</programlisting>
2779

2880
<para>
29-
This will return all the items in the right hand table for an entry
81+
This will return all the items in the right hand table for an entry
3082
in the left hand table. This is a very common construct in SQL.
3183
</para>
3284

3385
<para>
3486
Now, this methodology can be cumbersome with a very large number of
35-
entries in the one_to_many table.Depending on the order in which
36-
data was entered,a join like thiscould result in an index scan
87+
entries in the<structname>one_to_many</> table. Often,
88+
a join like thiswould result in an index scan
3789
and a fetch for each right hand entry in the table for a particular
38-
left hand entry. If you have a very dynamic system, there is not much you
90+
left hand entry. If you have a very dynamic system, there is not much you
3991
can do. However, if you have some data which is fairly static, you can
4092
create a summary table with the aggregator.
4193
</para>
4294

4395
<programlisting>
44-
CREATE TABLE summary as SELECT left, int_array_aggregate(right)
45-
AS right FROM one_to_many GROUP BY left;
96+
CREATE TABLE summary as
97+
SELECT left, int_array_aggregate(right) AS right
98+
FROM one_to_many
99+
GROUP BY left;
46100
</programlisting>
47101

48102
<para>
49103
This will create a table with one row per left item, and an array
50104
of right items. Now this is pretty useless without some way of using
51-
the array, thats why there is an array enumerator.
105+
the array; that's why there is an array enumerator. You can do
52106
</para>
107+
53108
<programlisting>
54-
SELECT left, int_array_enum(right) FROM summary WHERE left = item;
109+
SELECT left, int_array_enum(right) FROM summary WHERE left =<replaceable>item</>;
55110
</programlisting>
56111

57112
<para>
58-
The above query using int_array_enum, produces the same results as:
113+
The above query using <function>int_array_enum</> produces the same results
114+
as
59115
</para>
116+
60117
<programlisting>
61-
SELECT left, right FROM one_to_many WHERE left = item;
118+
SELECT left, right FROM one_to_many WHERE left =<replaceable>item</>;
62119
</programlisting>
63-
120+
64121
<para>
65122
The difference is that the query against the summary table has to get
66-
only one row from the table,where asthe query against "one_to_many"
67-
must index scan and fetch a row for each entry.
123+
only one row from the table,whereasthedirectquery against
124+
<structname>one_to_many</>must index scan and fetch a row for each entry.
68125
</para>
126+
69127
<para>
70-
On our system, an EXPLAIN shows a query with a cost of 8488 gets reduced
71-
to a cost of 329. The query is a join between the one_to_many table,
128+
On one system, an <command>EXPLAIN</> showed a query with a cost of 8488 was
129+
reduced to a cost of 329. The original query was a join involving the
130+
<structname>one_to_many</> table, which was replaced by:
72131
</para>
132+
73133
<programlisting>
74-
SELECT right, count(right) FROM
75-
(
76-
SELECT left, int_array_enum(right) AS right FROM summary JOIN
77-
(SELECT left FROM left_table WHERE left = item) AS lefts
78-
ON (summary.left = lefts.left )
79-
) AS list GROUP BY right ORDER BY count DESC ;
134+
SELECT right, count(right) FROM
135+
( SELECT left, int_array_enum(right) AS right
136+
FROM summary JOIN (SELECT left FROM left_table WHERE left = <replaceable>item</>) AS lefts
137+
ON (summary.left = lefts.left)
138+
) AS list
139+
GROUP BY right
140+
ORDER BY count DESC;
80141
</programlisting>
81-
</sect1>
82142

143+
</sect2>
144+
145+
</sect1>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp