我创建了一个查询,它在前 100 名中执行大约 2 秒。如果我创建这个精确查询的存储过程,它需要 12-13 秒才能运行。
为什么会这样?
- 元素表计数 = 2309015(指定用户 ID = 326969)
- 匹配表计数 = 1290(指定用户 ID = 498)
- 站点表计数 = 71(指定用户 ID = 9)
代码
with search (elementid, siteid, title, description, site, link, addeddate)
as
(
select top(@top)
elementid,
elements.siteid, title, elements.description,
site =
case sites.description
when '' then sites.name
when null then sites.name
else sites.name + ' (' + sites.description + ')'
end,
elements.link,
elements.addeddate
from elements
left join sites on elements.siteid = sites.siteid
where title like @search and sites.userid = @userid
order by addeddate desc
)
select search.*, isnull(matches.elementid,0) as ismatch
from search
left join matches on matches.elementid = search.elementid