代码之家  ›  专栏  ›  技术社区  ›  Kent Boogaart

sqlite\ U stat1表格说明

  •  8
  • Kent Boogaart  · 技术社区  · 16 年前

    我试图诊断为什么特定的查询对SQLite的速度很慢。这方面的信息似乎很多 how the query optimizer works

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

    MyTable,ix_id,25112 1 1 1 1
    

    “251121”到底是什么意思?

    作为一个更广泛的问题,是否有人在诊断SQLite查询性能的最佳工具和技术方面有很好的资源?

    4 回复  |  直到 16 年前
        1
  •  5
  •   Peter Hizalev    16 年前

    /* 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.
    
        2
  •  2
  •   Eddie    10 年前

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

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

    1. 苹果,红色

    数据看起来像“21”。也就是说,索引中有两行。如果只使用索引的column1(Apple和Apple),将返回两行。以及使用column1+column2返回的唯一行(Apple+Red是Apple+Green中唯一的)

        3
  •  1
  •   HSchmale    10 年前

    另外,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)

    “测试”--->表名,“IDX”--->索引名,“10000 1 1000”,这里,10000是表TEST中的总行数,1表示,对于列C1,所有的值似乎都是不同的,这听起来像C1是IDs之类的,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不能。

        4
  •  0
  •   Curt Hu    15 年前

    只需运行explain QUERY PLAN+您的SQL语句,您就会发现语句中引用的表是否使用了您想要的索引,如果没有,请尝试重写SQL,如果是,请找出您想要使用的索引是否正确。更多信息请访问www.sqlite.org