我有两张桌子:
asset
- 带有id_asset,名称,自动收报机(60k行)quote_close
- 使用id_asset,refdate,quote_close(22MM行)我想在名称和代码中创建一个过滤器并返回:
我写了这个查询:
WITH tableAssetFiltered AS
(
SELECT
id_asset, ticker, name
FROM
asset
WHERE
ticker LIKE ('%VALE%') AND name LIKE ('%PUT%')
)
SELECT
ast.id_asset, ast.ticker, ast.name,
xx.quote_close as LastQuote, xx.MinDate,
xx.refdate as LastDate
FROM
tableAssetFiltered ast
LEFT JOIN
(SELECT
qc.id_asset, qc.refdate, qc.quote_close, tm.MinDate
FROM
quote_close qc
INNER JOIN
(SELECT
t.id_asset, max(t.refdate) as MaxDate, min(t.refdate) as MinDate
FROM
(SELECT
qc.id_asset, qc.refdate, qc.quote_close
FROM
quote_close qc
WHERE
qc.id_asset IN (SELECT id_asset
FROM tableAssetFiltered)
) t
GROUP BY
t.id_asset) tm ON qc.id_asset = tm.id_asset
AND qc.refdate = tm.MaxDate
) xx ON xx.id_asset = ast.id_asset
ORDER BY
ast.ticker
名称和代码中使用不同过滤器的结果为:
ticker like ('%VALE%') AND name like ('%PUT%')
00:02:28 并返回491行name like ('%PUT%')
时间为00:00:02并返回16697行ticker like ('%VALE%')
时间为00:00:02并返回1102行我无法理解的是查询
SELECT id_asset,ticker, name
FROM Viper.dbo.asset
WHERE ticker like ('%VALE%') AND name like ('%PUT%')
运行00:00:00。
为什么较小的表需要更长的时间才能运行?任何使其更快的解决方案?
答案 0 :(得分:0)
缓慢可能是由许多因素造成的,硬件,网络,缓存等。
为了加快查询速度, 1.确保自动收报机上有索引 2.在表格上运行update statistics 3.尝试找到一种方法来删除“'%'在字符串的开头。 这没关系:' VALE%' 这会降低您的查询速度:'%VALE'