OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / indexes-expressional.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Indexes on Expressions</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Indexes"
16 HREF="indexes.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Unique Indexes"
19 HREF="indexes-unique.html"><LINK
20 REL="NEXT"
21 TITLE="Operator Classes"
22 HREF="indexes-opclass.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="SECT1"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="indexes-unique.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="indexes.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 11. Indexes</TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="indexes.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="indexes-opclass.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="INDEXES-EXPRESSIONAL"
96 >11.5. Indexes on Expressions</A
97 ></H1
98 ><A
99 NAME="AEN13255"
100 ></A
101 ><P
102 >   An index column need not be just a column of the underlying table,
103    but can be a function or scalar expression computed from one or
104    more columns of the table.  This feature is useful to obtain fast
105    access to tables based on the results of computations.
106   </P
107 ><P
108 >   For example, a common way to do case-insensitive comparisons is to
109    use the <CODE
110 CLASS="FUNCTION"
111 >lower</CODE
112 > function:
113 </P><PRE
114 CLASS="PROGRAMLISTING"
115 >SELECT * FROM test1 WHERE lower(col1) = 'value';</PRE
116 ><P>
117    This query can use an index, if one has been
118    defined on the result of the <TT
119 CLASS="LITERAL"
120 >lower(col1)</TT
121 >
122    operation:
123 </P><PRE
124 CLASS="PROGRAMLISTING"
125 >CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));</PRE
126 ><P>
127   </P
128 ><P
129 >   If we were to declare this index <TT
130 CLASS="LITERAL"
131 >UNIQUE</TT
132 >, it would prevent
133    creation of rows whose <TT
134 CLASS="LITERAL"
135 >col1</TT
136 > values differ only in case,
137    as well as rows whose <TT
138 CLASS="LITERAL"
139 >col1</TT
140 > values are actually identical.
141    Thus, indexes on expressions can be used to enforce constraints that
142    are not definable as simple unique constraints.
143   </P
144 ><P
145 >   As another example, if one often does queries like this:
146 </P><PRE
147 CLASS="PROGRAMLISTING"
148 >SELECT * FROM people WHERE (first_name || ' ' || last_name) = 'John Smith';</PRE
149 ><P>
150    then it might be worth creating an index like this:
151 </P><PRE
152 CLASS="PROGRAMLISTING"
153 >CREATE INDEX people_names ON people ((first_name || ' ' || last_name));</PRE
154 ><P>
155   </P
156 ><P
157 >   The syntax of the <TT
158 CLASS="COMMAND"
159 >CREATE INDEX</TT
160 > command normally requires
161    writing parentheses around index expressions, as shown in the second
162    example.  The parentheses may be omitted when the expression is just
163    a function call, as in the first example.
164   </P
165 ><P
166 >   Index expressions are relatively expensive to maintain, since the
167    derived expression(s) must be computed for each row upon insertion
168    or whenever it is updated.  Therefore they should be used only when
169    queries that can use the index are very frequent.
170   </P
171 ></DIV
172 ><DIV
173 CLASS="NAVFOOTER"
174 ><HR
175 ALIGN="LEFT"
176 WIDTH="100%"><TABLE
177 SUMMARY="Footer navigation table"
178 WIDTH="100%"
179 BORDER="0"
180 CELLPADDING="0"
181 CELLSPACING="0"
182 ><TR
183 ><TD
184 WIDTH="33%"
185 ALIGN="left"
186 VALIGN="top"
187 ><A
188 HREF="indexes-unique.html"
189 ACCESSKEY="P"
190 >Prev</A
191 ></TD
192 ><TD
193 WIDTH="34%"
194 ALIGN="center"
195 VALIGN="top"
196 ><A
197 HREF="index.html"
198 ACCESSKEY="H"
199 >Home</A
200 ></TD
201 ><TD
202 WIDTH="33%"
203 ALIGN="right"
204 VALIGN="top"
205 ><A
206 HREF="indexes-opclass.html"
207 ACCESSKEY="N"
208 >Next</A
209 ></TD
210 ></TR
211 ><TR
212 ><TD
213 WIDTH="33%"
214 ALIGN="left"
215 VALIGN="top"
216 >Unique Indexes</TD
217 ><TD
218 WIDTH="34%"
219 ALIGN="center"
220 VALIGN="top"
221 ><A
222 HREF="indexes.html"
223 ACCESSKEY="U"
224 >Up</A
225 ></TD
226 ><TD
227 WIDTH="33%"
228 ALIGN="right"
229 VALIGN="top"
230 >Operator Classes</TD
231 ></TR
232 ></TABLE
233 ></DIV
234 ></BODY
235 ></HTML
236 >