简体   繁体   English

为什么SQLite索引不会加快我的查询速度

[英]Why does an SQLite index not speed up my query

I have 2 tables: 我有2张桌子:

tblValidItems - | tblValidItems - | - tblItems - tblItems

validID        itemID
-------        ------
3              1
5              2
6              3
...            4
~ 8 K items    5
               ..... 
               ~ 20 K items

My query is to select certain things in tblItems that are also in tblValidItems : 我的查询是选择tblItems中同样位于tblValidItems某些东西:

SELECT tblItems.itemID FROM tblItems 
JOIN tblValidItems ON tblItems.itemID = tblValidItems.validID

I tried the query with and without an index on both tables, but the results varied vary little: 我在两个表上都尝试了带有和没有索引的查询,但结果变化很小:

  • With a indexes on both tables - 127ms 两个表上都有索引 - 127ms
  • With no index on either table - 132ms 两个表都没有索引 - 132ms

This surprised me because I thought an index would dramatically effect the speed of this query. 这让我感到惊讶,因为我认为索引会显着影响此查询的速度。 Why dons't it? 为什么不呢?

I am guessing that the query is dominated by the time to return the 8,000 values and not the time to find the rows. 我猜测查询主要是返回8,000个值而不是查找行的时间。

Indexes are most useful when you are reducing the size of the data you are working with. 当您减小正在使用的数据的大小时,索引最有用。 The reduction from 20k rows to 8k is not particularly signficant. 从20k行减少到8k并不是特别重要。

Sqlite primary keys are indexed by default. 默认情况下会为Sqlite主键编制索引。 You are joining on an indexed primary key anyway. 无论如何,您正在加入索引的主键。

Whenever you have doubts on how sqlite will work with your query, use EXPLAIN QUERY PLAN 每当您对sqlite如何处理查询有疑问时,请使用EXPLAIN QUERY PLAN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM