⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optoverview.tcl

📁 sqlite-3.4.1,嵌入式数据库.是一个功能强大的开源数据库,给学习和研发以及小型公司的发展带来了全所未有的好处.
💻 TCL
📖 第 1 页 / 共 2 页
字号:
PARAGRAPH {  The GLOB operator is always case sensitive.  The column on the left side  of the GLOB operator must always use the built-in BINARY collating sequence  or no attempt will be made to optimize that operator with indices.}PARAGRAPH {  The right-hand side of the GLOB or LIKE operator must be a literal string  value that does not begin with a wildcard.  If the right-hand side is a  parameter that is bound to a string, then no optimization is attempted.  If the right-hand side begins with a wildcard character then no   optimization is attempted.}PARAGRAPH {  Suppose the initial sequence of non-wildcard characters on the right-hand  side of the LIKE or GLOB operator is <i>x</i>.  We are using a single   character to denote this non-wildcard prefix but the reader should  understand that the prefix can consist of more than 1 character.  Let <i>y</i> the smallest string that is the same length as /x/ but which  compares greater than <i>x</i>.  For example, if <i>x</i> is *hello* then  <i>y</i> would be *hellp*.  The LIKE and GLOB optimizations consist of adding two virtual terms  like this:}SYNTAX {  /column/ >= /x/ AND /column/ < /y/}PARAGRAPH {  Under most circumstances, the original LIKE or GLOB operator is still  tested against each input row even if the virtual terms are used to  constrain an index.  This is because we do not know what additional  constraints may be imposed by characters to the right  of the <i>x</i> prefix.  However, if there is only a single global wildcard  to the right of <i>x</i>, then the original LIKE or GLOB test is disabled.  In other words, if the pattern is like this:}SYNTAX {  /column/ LIKE /x/%  /column/ GLOB /x/*}PARAGRAPH {  Then the original LIKE or GLOB tests are disabled when the virtual  terms constrain an index because in that case we know that all of the  rows selected by the index will pass the LIKE or GLOB test.}HEADING 1 {Joins} joinsPARAGRAPH {  The current implementation of   SQLite uses only loop joins.  That is to say, joins are implemented as  nested loops.}PARAGRAPH {  The default order of the nested loops in a join is for the left-most  table in the FROM clause to form the outer loop and the right-most  table to form the inner loop.  However, SQLite will nest the loops in a different order if doing so  will help it to select better indices.}PARAGRAPH {  Inner joins can be freely reordered.  However a left outer join is  neither commutative nor associative and hence will not be reordered.  Inner joins to the left and right of the outer join might be reordered  if the optimizer thinks that is advantageous but the outer joins are  always evaluated in the order in which they occur.}PARAGRAPH {  When selecting the order of tables in a join, SQLite uses a greedy  algorithm that runs in polynomial time.}PARAGRAPH {  The ON and USING clauses of a join are converted into additional  terms of the WHERE clause prior to WHERE clause analysis described  above in paragraph 1.0.  Thus  with SQLite, there is no advantage to use the newer SQL92 join syntax  over the older SQL89 comma-join syntax.  They both end up accomplishing  exactly the same thing.}PARAGRAPH {  Join reordering is automatic and usually works well enough that  programmer do not have to think about it.  But occasionally some  hints from the programmer are needed.  For a description of when  hints might be necessary and how to provide those hints, see the  <a href="http://www.sqlite.org/cvstrac/wiki?p=QueryPlans">QueryPlans</a>  page in the Wiki.}HEADING 1 {Choosing between multiple indices} multi_indexPARAGRAPH {  Each table in the FROM clause of a query can use at most one index,  and SQLite strives to use at least one index on each table.  Sometimes,  two or more indices might be candidates for use on a single table.  For example:}CODE {  CREATE TABLE ex2(x,y,z);  CREATE INDEX ex2i1 ON ex2(x);  CREATE INDEX ex2i2 ON ex2(y);  SELECT z FROM ex2 WHERE x=5 AND y=6;}PARAGRAPH {  For the SELECT statement above, the optimizer can use the ex2i1 index  to lookup rows of ex2 that contain x=5 and then test each row against  the y=6 term.  Or it can use the ex2i2 index to lookup rows  of ex2 that contain y=6 then test each of those rows against the  x=5 term.}PARAGRAPH {  When faced with a choice of two or more indices, SQLite tries to estimate  the total amount of work needed to perform the query using each option.  It then selects the option that gives the least estimated work.}PARAGRAPH {  To help the optimizer get a more accurate estimate of the work involved  in using various indices, the user may optional run the ANALYZE command.  The ANALYZE command scans all indices of database where there might  be a choice between two or more indices and gathers statistics on the  selectiveness of those indices.  The results of this scan are stored  in the sqlite_stat1 table.  The contents of the sqlite_stat1 table are not updated as the database  changes so after making significant changes it might be prudent to  rerun ANALYZE.  The results of an ANALYZE command are only available to database connections  that are opened after the ANALYZE command completes.}PARAGRAPH {  Once created, the sqlite_stat1 table cannot be dropped.  But its  content can be viewed, modified, or erased.  Erasing the entire content  of the sqlite_stat1 table has the effect of undoing the ANALYZE command.  Changing the content of the sqlite_stat1 table can get the optimizer  deeply confused and cause it to make silly index choices.  Making  updates to the sqlite_stat1 table (except by running ANALYZE) is  not recommended.}PARAGRAPH {  Terms of the WHERE clause can be manually disqualified for use with  indices by prepending a unary *+* operator to the column name.  The  unary *+* is a no-op and will not slow down the evaluation of the test  specified by the term.  But it will prevent the term from constraining an index.  So, in the example above, if the query were rewritten as:}CODE {  SELECT z FROM ex2 WHERE +x=5 AND y=6;}PARAGRAPH {  The *+* operator on the *x* column would prevent that term from   constraining an index.  This would force the use of the ex2i2 index.}HEADING 1 {Avoidance of table lookups} index_onlyPARAGRAPH {  When doing an indexed lookup of a row, the usual procedure is to  do a binary search on the index to find the index entry, then extract  the rowid from the index and use that rowid to do a binary search on  the original table.  Thus a typical indexed lookup involves two  binary searches.  If, however, all columns that were to be fetched from the table are  already available in the index itself, SQLite will use the values  contained in the index and will never look up the original table  row.  This saves one binary search for each row and can make many  queries run twice as fast.}HEADING 1 {ORDER BY optimizations} order_byPARAGRAPH {  SQLite attempts to use an index to satisfy the ORDER BY clause of a  query when possible.  When faced with the choice of using an index to satisfy WHERE clause  constraints or satisfying an ORDER BY clause, SQLite does the same  work analysis described in section 6.0  and chooses the index that it believes will result in the fastest answer.}HEADING 1 {Subquery flattening} flatteningPARAGRAPH {  When a subquery occurs in the FROM clause of a SELECT, the default  behavior is to evaluate the subquery into a transient table, then run  the outer SELECT against the transient table.   This is problematic since the transient table will not have any indices  and the outer query (which is likely a join) will be forced to do a  full table scan on the transient table.}PARAGRAPH {  To overcome this problem, SQLite attempts to flatten subqueries in  the FROM clause of a SELECT.  This involves inserting the FROM clause of the subquery into the  FROM clause of the outer query and rewriting expressions in  the outer query that refer to the result set of the subquery.  For example:}CODE {  SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5}PARAGRAPH {  Would be rewritten using query flattening as:}CODE {  SELECT x+y AS a FROM t1 WHERE z<100 AND a>5}PARAGRAPH {  There is a long list of conditions that must all be met in order for  query flattening to occur.}PARAGRAPH {  <ol>  <li> The subquery and the outer query do not both use aggregates.</li>  <li> The subquery is not an aggregate or the outer query is not a join. </li>  <li> The subquery is not the right operand of a left outer join, or          the subquery is not itself a join. </li>  <li>  The subquery is not DISTINCT or the outer query is not a join. </li>  <li>  The subquery is not DISTINCT or the outer query does not use          aggregates. </li>  <li>  The subquery does not use aggregates or the outer query is not          DISTINCT. </li>  <li>  The subquery has a FROM clause. </li>  <li>  The subquery does not use LIMIT or the outer query is not a join. </li>  <li>  The subquery does not use LIMIT or the outer query does not use         aggregates. </li>  <li>  The subquery does not use aggregates or the outer query does not         use LIMIT. </li>  <li>  The subquery and the outer query do not both have ORDER BY clauses.</li>  <li>  The subquery is not the right term of a LEFT OUTER JOIN or the         subquery has no WHERE clause.  </li>  </ol>}PARAGRAPH {  The proof that query flattening may safely occur if all of the the  above conditions are met is left as an exercise to the reader.}PARAGRAPH {  Query flattening is an important optimization when views are used as  each use of a view is translated into a subquery.}HEADING 1 {The MIN/MAX optimization} minmaxPARAGRAPH {  Queries of the following forms will be optimized to run in logarithmic  time assuming appropriate indices exist:}CODE {  SELECT MIN(x) FROM table;  SELECT MAX(x) FROM table;}PARAGRAPH {  In order for these optimizations to occur, they must appear in exactly  the form shown above - changing only the name of the table and column.  It is not permissible to add a WHERE clause or do any arithmetic on the  result.  The result set must contain a single column.  The column in the MIN or MAX function must be an indexed column.}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -