我有两个表工具,tool_attribute。工具有12列,tool_attribute有5列。
表中需要的信息:
现在我有大约18264的工具和255696的tool_attribute
当前查询:
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毫秒左右,这是相当快,但我想让它更快。我在低内存的Windows6.0设备上运行这段代码,所以时间太长了。有什么办法能让我快点吗?
发布于 2017-08-16 05:33:37
可以尝试将索引添加到联接所涉及的列中:
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%'逻辑,那么也可以在那里使用索引。
https://stackoverflow.com/questions/45705649
复制相似问题