Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite_stat1表说明_Sql_Sqlite_Indexing_Performance - Fatal编程技术网

sqlite_stat1表说明

sqlite_stat1表说明,sql,sqlite,indexing,performance,Sql,Sqlite,Indexing,Performance,我试图诊断为什么特定查询对SQLite的速度很慢。关于问题的信息似乎很多,但关于如何实际诊断问题的信息却很少 特别是,当我分析数据库时,我得到了预期的sqlite_stat1表,但我不知道stat列告诉了我什么。一个示例行是: MyTable,ix_id,25112 1 1 1 1 “251121”实际上是什么意思 作为一个更广泛的问题,是否有人拥有关于诊断SQLite查询性能的最佳工具和技术的良好资源 感谢来自analyze.c: /* Store the results. ** **

我试图诊断为什么特定查询对SQLite的速度很慢。关于问题的信息似乎很多,但关于如何实际诊断问题的信息却很少

特别是,当我分析数据库时,我得到了预期的sqlite_stat1表,但我不知道stat列告诉了我什么。一个示例行是:

MyTable,ix_id,25112 1 1 1 1
“251121”实际上是什么意思

作为一个更广泛的问题,是否有人拥有关于诊断SQLite查询性能的最佳工具和技术的良好资源

感谢来自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.

只需运行explain查询计划+您的SQL语句,您将发现语句中引用的表是否使用了您想要的索引,如果没有,请尝试重写SQL,如果是,请确定您想要使用的索引是否正确。更多信息请参考www.sqlite.org

另外,I=(K+D-1)/D表示:K是假定的总行数,D是每列的不同值, 因此,如果您使用
创建表TEST(c1int,c2text,c3int,c4int)创建表
您可以创建索引,比如
在测试(C1,C2)时创建索引IDX

然后,您可以手动插入或让sqlite自动更新sqlite_stat1表,如下所示: “TEST”-->TABLE NAME,“IDX”-->INDEX NAME,“10000 1 1000”,这里,10000是表TEST中的行总数,1表示,对于列C1,所有值似乎都是不同的,这听起来像C1类似于IDs或其他什么,1000表示C2的不同值较小,如您所知,值越高,索引引用的特定列的不太明显的值

您可以运行
ANALYZE
或手动更新表。(最好先做)

那么这个值用于什么呢?SQLite将使用这些统计信息,以找到他们想要使用的最佳索引,可以考虑<代码> >创建索引IDX2(C2),并且STAT1表中的值是“10000 1 ”和“代码>创建索引IDX1在测试(C1)上,值为“10000 100”;<代码> 假设我们没有之前定义的索引IDX,当您发布
SELECT*formtest,其中C1=?和C2=?
,sqlite将选择IDX2,而不是IDX1,为什么?这很简单,因为IDX2可以最小化查询结果,但IDX1不能


清楚吗?

请记住,索引可以由一个表的多个列组成。因此,在“251121”的情况下,这将被描述为一个由一个表的4列组成的复合索引。数字的含义如下:

  • 25112是索引中行总数的估计值
  • 第二个整数(第一个“1”)是对索引第一列中具有相同值的行数的估计
  • 第三个整数(第二个“1”)是对索引前两列中具有相同值的行数的估计。它不是第2列的“差异性”
  • 第四个整数(第三个“1”)是对索引前三列具有相同值的行数的估计
  • 最后一个整数的逻辑相同

最后一个整数应该总是一个。考虑一个表,该表有两行,两列由Calnn+Culn2组成的复合索引。数据表是:

  • 苹果,红色
  • 苹果,绿色

  • 统计数据看起来像“2 2 1”。这意味着索引中有2行。如果只使用索引的column1(Apple和Apple),将返回两行。使用column1+column2(Apple+红色与Apple+绿色不同)将返回一行唯一的数据

    刚发现我的性能问题是由于连接表上缺少索引造成的,但这比诊断更直观。我真的很想更多地了解诊断SQLite索引使用的过程。