Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of sqlite_stat1 table

I'm trying to diagnose why a particular query is slow against SQLite. There seems to be plenty of information on how the query optimizer works, but scant information on how to actually diagnose issues.

In particular, when I analyze the database I get the expected sqlite_stat1 table, but I don't know what the stat column is telling me. An example row is:

MyTable,ix_id,25112 1 1 1 1

What does the "25112 1 1 1 1" actually mean?

As a wider question, does anyone have any good resources on the best tools and techniques for diagnosing SQLite query performance?

Thanks

like image 485
Kent Boogaart Avatar asked Mar 16 '10 16:03

Kent Boogaart


People also ask

What is Sqlite_sequence table?

sqlite_sequence. Lists the last sequence number used for the AUTOINCREMENT column in a table. The sqlite_sequence table will only be created once an AUTOINCREMENT column has been defined in the database and at least one sequence number value has been generated and used in the database.

What does ANALYZE do in SQL?

The ANALYZE command gathers statistics about tables and indices and stores the collected information in internal tables of the database where the query optimizer can access the information and use it to help make better query planning choices. If no arguments are given, all attached databases are analyzed.

What is SQLite master?

sqlite_master is an internal table that is present in all SQLite databases. The content of this table describes the database's schema. The structure of sqlite_master can be displayed with the SQLite shell command .

How fast is SQLite?

SQLite only supports one writer at a time per database file. But in most cases, a write transaction only takes milliseconds and so multiple writers can simply take turns.


2 Answers

from analyze.c:

/* Store the results.  
**
** The result is a single row of the sqlite_stmt1 table.  The first
** two columns are the names of the table and index.  The third column
** is a string composed of a list of integer statistics about the
** index.  The first integer in the list is the total number of entires
** in the index.  There is one additional integer in the list for each
** column of the table.  This additional integer is a guess of how many
** rows of the table the index will select.  If D is the count of distinct
** values and K is the total number of rows, then the integer is computed
** as:
**
**        I = (K+D-1)/D
**
** If K==0 then no entry is made into the sqlite_stat1 table.  
** If K>0 then it is always the case the D>0 so division by zero
** is never possible.
like image 64
Peter Hizalev Avatar answered Sep 22 '22 21:09

Peter Hizalev


Remember that an index can be comprised of more than one column of a table. So, in the case of "25112 1 1 1 1", this would be described as a composite index that is made up of 4 columns of a table. The numbers mean as follows:

  • 25112 is an estimate of the total number of rows in the index
  • The second integer (the first "1") is an estimate of the number of rows that have the same value in the 1st column on the index.
  • The third integer (the second "1") is an estimate of the number of rows that have the same value for the first TWO columns of the index. It is NOT the "distinctness" of column 2.
  • The forth integer (the third "1") is an estimate of the number of rows that have the same values for the first THREE columns on the index.
  • Same logic for the last integer..

The last integer should always be one. Consider a table that has two rows and two columns with a composite index made up of column1+column2. The data is the table is:

  1. Apple,Red
  2. Apple,Green

The stats would look like "2 2 1". Meaning, there are 2 rows in the index. There are two rows that would be returned if only using column1 of the index (Apple and Apple). And 1 unique row that would be returned using column1+column2 (Apple+Red is unique from Apple+Green)

like image 43
Eddie Avatar answered Sep 24 '22 21:09

Eddie