我正在尝试诊断为什么针对SQLite的特定查询速度很慢。关于how the query optimizer works的信息似乎很多,但关于如何实际诊断问题的信息却很少。
特别是,当我分析数据库时,我得到了预期的sqlite_stat1表,但我不知道stat列告诉我什么。一个示例行是:
MyTable,ix_id,25112 1 1 1 1"25112 1 11“究竟是甚麽意思呢?
作为一个更广泛的问题,有没有人有关于诊断SQLite查询性能的最佳工具和技术的好资源?
谢谢
发布于 2010-03-24 06:37:05
来自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.发布于 2016-07-12 09:15:38
请记住,索引可以由表的多个列组成。因此,在"25112 1111“的情况下,这将被描述为由表的4列组成的复合索引。这些数字的含义如下:
最后一个整数应始终为1。考虑一个具有两行两列的表,该表具有由column1+column2组成的复合索引。表中的数据是:
统计数据看起来像"2 2 1“。也就是说,索引中有2行。如果只使用索引的column1 (Apple和Apple),将返回两行。和1个使用column1+column2返回的唯一行(Apple+Red在Apple+Green中是唯一的)
发布于 2011-03-07 14:42:23
此外,I= (K+D-1)/D意味着:K是假设的总行数,D是每列的不同值,因此如果您使用CREATE TABLE TEST (C1 INT, C2 TEXT, C3 INT, C4 INT);创建表,并且您创建了像CREATE INDEX IDX on TEST(C1, C2)这样的索引
然后您可以手动插入或让sqlite自动更新sqlite_stat1表:" TEST "-->表名,"IDX"-->索引名,"10000 11000“,这里,10000是表TEST中的总行数,1表示,对于列C1,所有的值似乎都是不同的,这听起来像是C1是in之类的,1000表示C2的值不太明显,正如您所知道的,值越高,索引引用特定列的值就越不明显。
您可以运行ANALYZE或手动更新表。(最好先做第一件事)。
那么这个值用来做什么呢?sqlite将使用这些统计信息,以找到他们想要使用的最佳索引,您可以考虑CREATE INDEX IDX2 ON TEST(C2)" AND the value in stat1 table is "10000 1,CREATE INDEX IDX1 ON TEST(C1)" with value "10000 100";假设我们没有前面定义的索引IDX,当您发出SELECT * FORM TEST WHERE C1=? AND C2=?时,sqlite将选择IDX2,而不是IDX1,为什么?这很简单,因为IDX2可以最小化查询结果,而IDX1不能。
明白了吗?
https://stackoverflow.com/questions/2456215
复制相似问题