sqlite3expert.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ** 2017 April 07
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. */
  13. #if !defined(SQLITEEXPERT_H)
  14. #define SQLITEEXPERT_H 1
  15. #include "sqlite3.h"
  16. typedef struct sqlite3expert sqlite3expert;
  17. /*
  18. ** Create a new sqlite3expert object.
  19. **
  20. ** If successful, a pointer to the new object is returned and (*pzErr) set
  21. ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
  22. ** an English-language error message. In this case it is the responsibility
  23. ** of the caller to eventually free the error message buffer using
  24. ** sqlite3_free().
  25. */
  26. sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
  27. /*
  28. ** Configure an sqlite3expert object.
  29. **
  30. ** EXPERT_CONFIG_SAMPLE:
  31. ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
  32. ** each candidate index. This involves scanning and sorting the entire
  33. ** contents of each user database table once for each candidate index
  34. ** associated with the table. For large databases, this can be
  35. ** prohibitively slow. This option allows the sqlite3expert object to
  36. ** be configured so that sqlite_stat1 data is instead generated based on a
  37. ** subset of each table, or so that no sqlite_stat1 data is used at all.
  38. **
  39. ** A single integer argument is passed to this option. If the value is less
  40. ** than or equal to zero, then no sqlite_stat1 data is generated or used by
  41. ** the analysis - indexes are recommended based on the database schema only.
  42. ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
  43. ** generated for each candidate index (this is the default). Finally, if the
  44. ** value falls between 0 and 100, then it represents the percentage of user
  45. ** table rows that should be considered when generating sqlite_stat1 data.
  46. **
  47. ** Examples:
  48. **
  49. ** // Do not generate any sqlite_stat1 data
  50. ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
  51. **
  52. ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
  53. ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
  54. */
  55. int sqlite3_expert_config(sqlite3expert *p, int op, ...);
  56. #define EXPERT_CONFIG_SAMPLE 1 /* int */
  57. /*
  58. ** Specify zero or more SQL statements to be included in the analysis.
  59. **
  60. ** Buffer zSql must contain zero or more complete SQL statements. This
  61. ** function parses all statements contained in the buffer and adds them
  62. ** to the internal list of statements to analyze. If successful, SQLITE_OK
  63. ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
  64. ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
  65. ** may be set to point to an English language error message. In this case
  66. ** the caller is responsible for eventually freeing the error message buffer
  67. ** using sqlite3_free().
  68. **
  69. ** If an error does occur while processing one of the statements in the
  70. ** buffer passed as the second argument, none of the statements in the
  71. ** buffer are added to the analysis.
  72. **
  73. ** This function must be called before sqlite3_expert_analyze(). If a call
  74. ** to this function is made on an sqlite3expert object that has already
  75. ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
  76. ** immediately and no statements are added to the analysis.
  77. */
  78. int sqlite3_expert_sql(
  79. sqlite3expert *p, /* From a successful sqlite3_expert_new() */
  80. const char *zSql, /* SQL statement(s) to add */
  81. char **pzErr /* OUT: Error message (if any) */
  82. );
  83. /*
  84. ** This function is called after the sqlite3expert object has been configured
  85. ** with all SQL statements using sqlite3_expert_sql() to actually perform
  86. ** the analysis. Once this function has been called, it is not possible to
  87. ** add further SQL statements to the analysis.
  88. **
  89. ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
  90. ** an error occurs, an SQLite error code is returned and (*pzErr) set to
  91. ** point to a buffer containing an English language error message. In this
  92. ** case it is the responsibility of the caller to eventually free the buffer
  93. ** using sqlite3_free().
  94. **
  95. ** If an error does occur within this function, the sqlite3expert object
  96. ** is no longer useful for any purpose. At that point it is no longer
  97. ** possible to add further SQL statements to the object or to re-attempt
  98. ** the analysis. The sqlite3expert object must still be freed using a call
  99. ** sqlite3_expert_destroy().
  100. */
  101. int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
  102. /*
  103. ** Return the total number of statements loaded using sqlite3_expert_sql().
  104. ** The total number of SQL statements may be different from the total number
  105. ** to calls to sqlite3_expert_sql().
  106. */
  107. int sqlite3_expert_count(sqlite3expert*);
  108. /*
  109. ** Return a component of the report.
  110. **
  111. ** This function is called after sqlite3_expert_analyze() to extract the
  112. ** results of the analysis. Each call to this function returns either a
  113. ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
  114. ** The value passed as the third argument must be one of the EXPERT_REPORT_*
  115. ** #define constants defined below.
  116. **
  117. ** For some EXPERT_REPORT_* parameters, the buffer returned contains
  118. ** information relating to a specific SQL statement. In these cases that
  119. ** SQL statement is identified by the value passed as the second argument.
  120. ** SQL statements are numbered from 0 in the order in which they are parsed.
  121. ** If an out-of-range value (less than zero or equal to or greater than the
  122. ** value returned by sqlite3_expert_count()) is passed as the second argument
  123. ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
  124. **
  125. ** EXPERT_REPORT_SQL:
  126. ** Return the text of SQL statement iStmt.
  127. **
  128. ** EXPERT_REPORT_INDEXES:
  129. ** Return a buffer containing the CREATE INDEX statements for all recommended
  130. ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
  131. ** is returned.
  132. **
  133. ** EXPERT_REPORT_PLAN:
  134. ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
  135. ** iStmt after the proposed indexes have been added to the database schema.
  136. **
  137. ** EXPERT_REPORT_CANDIDATES:
  138. ** Return a pointer to a buffer containing the CREATE INDEX statements
  139. ** for all indexes that were tested (for all SQL statements). The iStmt
  140. ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
  141. */
  142. const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
  143. /*
  144. ** Values for the third argument passed to sqlite3_expert_report().
  145. */
  146. #define EXPERT_REPORT_SQL 1
  147. #define EXPERT_REPORT_INDEXES 2
  148. #define EXPERT_REPORT_PLAN 3
  149. #define EXPERT_REPORT_CANDIDATES 4
  150. /*
  151. ** Free an (sqlite3expert*) handle and all associated resources. There
  152. ** should be one call to this function for each successful call to
  153. ** sqlite3-expert_new().
  154. */
  155. void sqlite3_expert_destroy(sqlite3expert*);
  156. #endif /* !defined(SQLITEEXPERT_H) */