2

My SQLite database has over 500,000 rows. Being new I have just started using additional indexes in my VB.net application with some startling speed improvements :).

These improvements are not replicated when I index the Date column of my table. I am going to include some code snippets details below and would appreciate any input regarding what I may be doing wrong.

    'When creating the date column in DB (amongst others) 
oMainQueryR.CommandText += "RDate DATE, "

'creating index
oMainQueryR.CommandText = "Create INDEX main.iRDate ON DatabaseRows(RDate)"   
      oMainQueryR.ExecuteNonQuery()

'Storing the Data
.Parameters.Add(":Rdate", DbType.Date)
cmdSQLite.Parameters(":Rdate").Value = CDate(TextSplit(1)).ToString("dd/MM/yyyy")



'SQL Call to retrieve data
 oMainQueryR.CommandText = "SELECT * FROM DatabaseRows " _
                            & "WHERE DATE([RDate]) BETWEEN DATE(:StartDate) AND DATE(:EndDate) " _

The Calls and everything is working OK and I get the correct functionality its just there is no improvement when I index the RDate column speed wise.

I should mention that the index appears to be created correctly

Would appreciate any help, thank you

4

2 回答 2

3

SQLite 不使用索引进行此查找,因为您查找的不是列值,而是函数调用的结果,并且它不是DATE(RDate)存储在索引中的值。

DATE从您的查询中删除该功能;除了阻止优化之外,它在您的查询中什么都不做:

... WHERE RDate BETWEEN :StartDate AND :EndDate

要检查 SQLite 是否在查询中实际使用您的索引,请使用EXPLAIN QUERY PLAN

请注意dd/MM/yyyy不是 SQLite支持的日期格式之一;您必须yyyy-MM-dd用于字符串比较才能正常工作。

(对于这样的查找,将日期存储为字符串还是数字并不重要。)

于 2013-09-17T16:42:56.990 回答
2

考虑将您的日期数据存储在INTEGER(SQLite 本质上不支持日期数据类型)。

这会将查询变成

SELECT * FROM DatabaseRows WHERE RDate BETWEEN (:StartDate) AND (:EndDate);

这将避免大量的DATE函数调用。

实际上,您的DATE函数调用完全禁用了INDEX改进。

此外,INTEGER比较比比较快得多TEXT

于 2013-09-17T10:19:57.497 回答