连接表时 SQL 查询速度慢
这个查询非常非常慢,我不确定我哪里出了问题导致它这么慢。
我猜这与 Flight_prices 表有关
因为如果我删除该连接,它就会从 16 秒缩短到不到 1 秒。
SELECT * FROM OPENQUERY(mybook,
'SELECT wb.booking_ref
FROM web_bookings wb
LEFT JOIN prod_info pi ON wb.location = pi.location
LEFT JOIN flight_prices fp ON fp.dest_date = pi.dest_airport + '' '' + wb.sort_date
WHERE fp.dest_cheapest = ''Y''
AND wb.inc_flights = ''Y''
AND wb.customer = ''12345'' ')
有什么想法可以加快这个加入速度吗?
This query is very very slow and i'm not sure where I'm going wrong to cause it to be so slow.
I'm guessing it's something to do with the flight_prices table
because if I remove that join it goes from 16 seconds to less than one.
SELECT * FROM OPENQUERY(mybook,
'SELECT wb.booking_ref
FROM web_bookings wb
LEFT JOIN prod_info pi ON wb.location = pi.location
LEFT JOIN flight_prices fp ON fp.dest_date = pi.dest_airport + '' '' + wb.sort_date
WHERE fp.dest_cheapest = ''Y''
AND wb.inc_flights = ''Y''
AND wb.customer = ''12345'' ')
Any ideas how I can speed up this join??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不太可能在要使用的 Flight_prices.dest_date 上获得任何索引,因为您实际上并未加入另一列,这对优化器来说很困难。
如果您可以更改架构,我会将 Flight_prices.dest_date 分为两列 dest_airport 和 dest_Date,因为它目前似乎是机场和日期的组合。如果您这样做了,那么您可以像这样加入
You're unlikely to get any indexing on flight_prices.dest_date to be used as you're not actually joining to another column which makes it hard for the optimiser.
If you can change the schema I'd make it so flight_prices.dest_date was split into two columns dest_airport and dest_Date as it appears to be currently a composite of airport and date. If you did that you could then join like this
尝试 EXPLAIN PLAN 并查看数据库返回的结果。
如果您看到 TABLE SCAN,则可能需要添加索引。
第二个 JOIN 对我来说看起来很奇怪。我想知道是否可以重写。
Try EXPLAIN PLAN and see what your database comes back with.
If you see TABLE SCAN, you might need to add indexes.
That second JOIN looks rather odd to me. I'd wonder if that could be rewritten.
您的声明重新格式化后给了我这个,
我将确保以下字段具有索引
Your statement reformatted gives me this
I would make sure that following fields have indexes