连接表时 SQL 查询速度慢

发布于 2024-10-06 01:03:05 字数 529 浏览 3 评论 0原文

这个查询非常非常慢,我不确定我哪里出了问题导致它这么慢。

我猜这与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南冥有猫 2024-10-13 01:03:05

您不太可能在要使用的 Flight_prices.dest_date 上获得任何索引,因为您实际上并未加入另一列,这对优化器来说很困难。

如果您可以更改架构,我会将 Flight_prices.dest_date 分为两列 dest_airport 和 dest_Date,因为它目前似乎是机场和日期的组合。如果您这样做了,那么您可以像这样加入

fp.dest_date = wb.sort_date and fp.dest_airport = pi.dest_airport

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

fp.dest_date = wb.sort_date and fp.dest_airport = pi.dest_airport
热鲨 2024-10-13 01:03:05

尝试 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.

离不开的别离 2024-10-13 01:03:05

您的声明重新格式化后给了我这个,

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'

我将确保以下字段具有索引

  • dest_cheapest
  • dest_date
  • location
  • customer、inc_flights、booking_ref (覆盖索引)

Your statement reformatted gives me this

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'

I would make sure that following fields have indexes

  • dest_cheapest
  • dest_date
  • location
  • customer, inc_flights, booking_ref (covering index)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文