gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

49 行
4.6KB

  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>11.7. Indexes on Expressions</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="indexes-unique.html" title="11.6. Unique Indexes" /><link rel="next" href="indexes-partial.html" title="11.8. Partial Indexes" /></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">11.7. Indexes on Expressions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-unique.html" title="11.6. Unique Indexes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><th width="60%" align="center">Chapter 11. Indexes</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="indexes-partial.html" title="11.8. Partial Indexes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEXES-EXPRESSIONAL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.7. Indexes on Expressions</h2></div></div></div><a id="id-1.5.10.10.2" class="indexterm"></a><p>
  3. An index column need not be just a column of the underlying table,
  4. but can be a function or scalar expression computed from one or
  5. more columns of the table. This feature is useful to obtain fast
  6. access to tables based on the results of computations.
  7. </p><p>
  8. For example, a common way to do case-insensitive comparisons is to
  9. use the <code class="function">lower</code> function:
  10. </p><pre class="programlisting">
  11. SELECT * FROM test1 WHERE lower(col1) = 'value';
  12. </pre><p>
  13. This query can use an index if one has been
  14. defined on the result of the <code class="literal">lower(col1)</code>
  15. function:
  16. </p><pre class="programlisting">
  17. CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
  18. </pre><p>
  19. </p><p>
  20. If we were to declare this index <code class="literal">UNIQUE</code>, it would prevent
  21. creation of rows whose <code class="literal">col1</code> values differ only in case,
  22. as well as rows whose <code class="literal">col1</code> values are actually identical.
  23. Thus, indexes on expressions can be used to enforce constraints that
  24. are not definable as simple unique constraints.
  25. </p><p>
  26. As another example, if one often does queries like:
  27. </p><pre class="programlisting">
  28. SELECT * FROM people WHERE (first_name || ' ' || last_name) = 'John Smith';
  29. </pre><p>
  30. then it might be worth creating an index like this:
  31. </p><pre class="programlisting">
  32. CREATE INDEX people_names ON people ((first_name || ' ' || last_name));
  33. </pre><p>
  34. </p><p>
  35. The syntax of the <code class="command">CREATE INDEX</code> command normally requires
  36. writing parentheses around index expressions, as shown in the second
  37. example. The parentheses can be omitted when the expression is just
  38. a function call, as in the first example.
  39. </p><p>
  40. Index expressions are relatively expensive to maintain, because the
  41. derived expression(s) must be computed for each row upon insertion
  42. and whenever it is updated. However, the index expressions are
  43. <span class="emphasis"><em>not</em></span> recomputed during an indexed search, since they are
  44. already stored in the index. In both examples above, the system
  45. sees the query as just <code class="literal">WHERE indexedcolumn = 'constant'</code>
  46. and so the speed of the search is equivalent to any other simple index
  47. query. Thus, indexes on expressions are useful when retrieval speed
  48. is more important than insertion and update speed.
  49. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-unique.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexes.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="indexes-partial.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.6. Unique Indexes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 11.8. Partial Indexes</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1