Debian Codegraph @

www/expridx.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Indexes On Expressions</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Indexes On Expressions
122 </div>
123 </div>
124
125
126
127
128
129 <p>
130 Normally, an SQL index references columns of a table. But an index
131 can also be formed on expressions involving table columns.
132
133 </p><p>
134 As an example, consider the following table that tracks
135 dollar-amount changes on various "accounts":
136
137 </p><div class="codeblock"><pre>CREATE TABLE account_change(
138 chng_id INTEGER PRIMARY KEY,
139 acct_no INTEGER REFERENCES account,
140 location INTEGER REFERENCES locations,
141 amt INTEGER, -- in cents
142 authority TEXT,
143 comment TEXT
144 );
145 CREATE INDEX acctchng_magnitude ON account_change(acct_no, abs(amt));
146 </pre></div>
147
148 <p>
149 Each entry in the account_change table records a deposit or a withdrawal
150 into an account. Deposits have a positive "amt" and withdrawals have
151 a negative "amt".
152
153 </p><p>
154 The acctchng_magnitude index is over the account number ("acct_no") and
155 on the absolute value of the amount. This index allows one to do
156 efficient queries over the magnitude of a change to the account.
157 For example, to list all changes to account number $xyz that are
158 more than $100.00, one can say:
159
160
161 </p><div class="codeblock"><pre>SELECT * FROM account_change WHERE acct_no=$xyz AND abs(amt)>=10000;
162 </pre></div>
163
164 <p>
165 Or, to list all changes to one particular account ($xyz) in order of
166 decreasing magnitude, one can write:
167
168 </p><div class="codeblock"><pre>SELECT * FROM account_change WHERE acct_no=$xyz
169 ORDER BY abs(amt) DESC;
170 </pre></div>
171
172 <p>
173 Both of the above example queries would work fine without the
174 acctchng_magnitude index.
175 The acctchng_magnitude index merely helps the queries to run
176 faster, especially on databases where there are many entries in
177 the table for each account.
178
179 </p><h1 id="how_to_use_indexes_on_expressions"><span>1. </span>How To Use Indexes On Expressions</h1>
180
181 <p>
182 Use a <a href="lang_createindex.html">CREATE INDEX</a> statement to create a new index on one or more
183 expressions just like you would to create an index on columns. The only
184 difference is that expressions are listed as the elements to be indexed
185 rather than column names.
186
187 </p><p>
188 The SQLite query planner will consider using an index on an expression
189 when the expression that is indexed appears in the WHERE clause or in
190 the ORDER BY clause of a query, <i>exactly</i> as it is written in the
191 CREATE INDEX statement. The query planner does not do algebra. In order
192 to match WHERE clause constraints and ORDER BY terms to indexes, SQLite
193 requires that the expressions be the same, except for minor syntactic
194 differences such as white-space changes. So if you have:
195
196 </p><div class="codeblock"><pre>CREATE TABLE t2(x,y,z);
197 CREATE INDEX t2xy ON t2(x+y);
198 </pre></div>
199
200 <p>
201 And then you run the query:
202
203 </p><div class="codeblock"><pre>SELECT * FROM t2 WHERE y+x=22;
204 </pre></div>
205
206 <p>
207 Then the index will <u>not</u> be used because
208 the expression on the CREATE INDEX
209 statement (x+y) is not the same as the expression as it appears in the
210 query (y+x). The two expressions might be mathematically equivalent, but
211 the SQLite query planner insists that they be the same, not merely
212 equivalent. Consider rewriting the query thusly:
213
214 </p><div class="codeblock"><pre>SELECT * FROM t2 WHERE x+y=22;
215 </pre></div>
216
217 <p>
218 This second query will likely use the index because now the expression
219 in the WHERE clause (x+y) matches the expression in the index exactly.
220
221
222 </p><h1 id="restrictions"><span>2. </span>Restrictions</h1>
223
224 <p>
225 There are certain reasonable restrictions on expressions that appear in
226 CREATE INDEX statements:
227
228 </p><ol>
229 <li><p>
230 Expressions in CREATE INDEX statements
231 may only refer to columns of the table being indexed, not to
232 columns in other tables.
233
234 </p></li><li><p>
235 Expressions in CREATE INDEX statements
236 may contain function calls, but only to functions whose output
237 is always determined completely by its input parameters (a.k.a.:
238 <a href="deterministic.html">deterministic functions</a>). Obviously, functions like <a href="lang_corefunc.html#random">random()</a> will not
239 work well in an index. But also functions like <a href="lang_corefunc.html#sqlite_version">sqlite_version()</a>, though
240 they are constant across any one database connection, are not constant
241 across the life of the underlying database file, and hence may not be
242 used in a CREATE INDEX statement.
243
244 </p><p>
245 Note that <a href="appfunc.html">application-defined SQL functions</a> are by default considered
246 non-deterministic and may not be used in a CREATE INDEX statement unless
247 the <a href="c3ref/c_deterministic.html#sqlitedeterministic">SQLITE_DETERMINISTIC</a> flag is used when the function is registered.
248
249 </p></li><li><p>
250 Expressions in CREATE INDEX statements may not use subqueries.
251
252 </p></li><li><p>
253 Expressions may only be used in CREATE INDEX statements, not within
254 <a href="lang_createtable.html#uniqueconst">UNIQUE</a> or <a href="lang_createtable.html#primkeyconst">PRIMARY KEY</a> constraints within the <a href="lang_createtable.html">CREATE TABLE</a> statement.
255 </p></li></ol>
256
257
258 <h1 id="compatibility"><span>3. </span>Compatibility</h1>
259
260 <p>
261 The ability to index expressions was added to SQLite with
262 <a href="releaselog/3_9_0.html">version 3.9.0</a> (2015-10-14). A database that uses an index on
263 expressions will not be usable by earlier versions of SQLite.
264 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/expridx.in?m=918a2f1493">2023-02-11 20:57:33</a> UTC </small></i></p>
265