0
0
mirror of https://github.com/sqlite/sqlite.git synced 2024-12-01 17:23:42 +01:00
sqlite/ext/expert
dan 79610f5d09 Fix memory leaks in the code on this branch. Make use of the
sqlite3_index_constraint.usage field. Do not try to handle ORDER BY terms with
explicit COLLATE clauses - they don't get passed to the vtab layer anyway.

FossilOrigin-Name: 0cd75a872c89958a7f418720a0e8c6f638f8284c488f666015c19136feae6be8
2017-04-15 14:16:04 +00:00
..
expert1.test Modify the code in ext/expert/ to use the vtab interface instead of 2017-04-14 19:41:37 +00:00
expert.c Fix a formatting issue in the output of the sqlite3_expert program. 2017-04-11 18:29:14 +00:00
README.md Add ext/expert/README.md. 2017-04-10 20:00:26 +00:00
sqlite3expert.c Fix memory leaks in the code on this branch. Make use of the 2017-04-15 14:16:04 +00:00
sqlite3expert.h Add header comments to the API functions in sqlite3expert.h. Include a list of 2017-04-11 17:43:12 +00:00
test_expert.c Changes to allow the code in sqlite3expert.c to be tested directly (via the 2017-04-10 16:13:20 +00:00

SQLite Expert Extension

This folder contains code for a simple system to propose useful indexes given a database and a set of SQL queries. It works as follows:

  1. The user database schema is copied to a temporary database.

  2. All SQL queries are prepared against the temporary database. The sqlite3_whereinfo_hook() API is used to record information regarding the WHERE and ORDER BY clauses attached to each query.

  3. The information gathered in step 2 is used to create (possibly a large number of) candidate indexes.

  4. The SQL queries are prepared a second time. If the planner uses any of the indexes created in step 3, they are recommended to the user.

No ANALYZE data is available to the planner in step 4 above. This can lead to sub-optimal results.

This extension requires that SQLite be built with the SQLITE_ENABLE_WHEREINFO_HOOK pre-processor symbol defined.

C API

The SQLite expert C API is defined in sqlite3expert.h. Most uses will proceed as follows:

  1. An sqlite3expert object is created by calling sqlite3_expert_new(). A database handle opened by the user is passed as an argument.

  2. The sqlite3expert object is configured with one or more SQL statements by making one or more calls to sqlite3_expert_sql(). Each call may specify a single SQL statement, or multiple statements separated by semi-colons.

  3. sqlite3_expert_analyze() is called to run the analysis.

  4. One or more calls are made to sqlite3_expert_report() to extract components of the results of the analysis.

  5. sqlite3_expert_destroy() is called to free all resources.

Refer to comments in sqlite3expert.h for further details.

sqlite3_expert application

The file "expert.c" contains the code for a command line application that uses the API described above. It can be compiled with (for example):

  gcc -O2 -DSQLITE_ENABLE_WHEREINFO_HOOK sqlite3.c expert.c sqlite3expert.c -o sqlite3_expert

Assuming the database is "test.db", it can then be run to analyze a single query:

  ./sqlite3_expert -sql <sql-query> test.db

Or an entire text file worth of queries with:

  ./sqlite3_expert -file <text-file> test.db