内连接上的sqlite优化,表值约为18K

时间:2017-08-16 05:23:40

标签: sqlite

我有两个表工具,tool_attribute。 工具有12列,tool_attribute有5列。

表格中我需要的信息:

  1. 工具 - refid,serial,type,id
  2. tool_attribute - key,value,id(将有多个条目)
  3. 现在我在工具中有大约18264,在tool_attribute中有255696

    当前查询:

    select
        tool.refid,
        tool.serial,
        tool_attribute.value,
    tool.type
    from tool
    inner join tool_attribute
        on tool.id = tool_attribute.id
    where
        (tool_attribute.val LIKE '%t00%' or
         tool.serial LIKE '%t00%')
    group by tool.refid
    order by tool.serial asc;
    

    这需要大约750毫秒,这是相当快,但我想让它更快。我在低内存Windows 6.0设备上运行此代码,因此需要花费太多时间。 有什么方法可以让它更快吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试将索引添加到联接中涉及的列:

CREATE INDEX idx_tool ON tool (id);
CREATE INDEX idx_tool_attr ON tool_attribute (id);

我认为LIKE子句中的WHERE条件会妨碍在所涉及的列上使用索引的任何机会。原因是LIKE形式的%something表达式消除了搜索B树的机会,B树使用从左到右的后缀来查找内容。如果您可以使用与WHERE类似的内容重新定义LIKE 'something%'逻辑,那么也可以在那里使用索引。