需要帮助优化数据库查询。对指数非常缺乏经验

发布于 2024-11-04 19:40:44 字数 1324 浏览 1 评论 0原文

我需要优化这个查询。教授建议使用索引,但我对如何使用索引感到非常困惑。如果我能得到一个例子来说明什么是好的索引、为什么以及所需的实际代码,那么我绝对可以自己完成其余的工作。任何帮助都会很棒。 (PSQL 顺便说一句)

    SELECT 
      x.enteredBy
      , x.id
      , count(DISTINCT xr.id)
      , count(DISTINCT c.id)
      , 'l'
 FROM 
      ((locationsV x left outer join locationReviews xr on x.id = xr.lid)
       left outer join reviews r on r.id = xr.id) 
       left outer join comments c on xr.id = c.reviewId
 WHERE 
      x.vNo = 0  
      AND (r.enteredBy IS NULL OR 
            (r.enteredBy <> x.enteredBy 
             AND c.enteredBy <> x.enteredBy
             AND r.enteredBY NOT IN 
                       (SELECT requested FROM friends WHERE requester = x.enteredBY)
             AND r.enteredBY NOT IN 
                       (SELECT requester FROM friends WHERE requested = x.enteredBY)))
     AND (c.enteredBy IS NULL OR 
             (c.enteredBY NOT IN 
                       (SELECT requested FROM friends WHERE requester = x.enteredBY)
             AND c.enteredBY NOT IN 
                       (SELECT requester FROM friends WHERE requested = x.enteredBY)))
 GROUP BY 
     x.enteredBy
     , x.id

我尝试在开头添加类似的内容,但所花费的总时间没有改变。

CREATE INDEX friends1_idx ON friends(requested);
CREATE INDEX friends2_idx ON friends(requester);

I need to optimize this query. The professor recommends using indices, but i'm very confused about how. If I could get just one example of what a good index is and why, and the actual code needed, I could definitely do the rest by myself. Any help would be awesome. (PSQL btw)

    SELECT 
      x.enteredBy
      , x.id
      , count(DISTINCT xr.id)
      , count(DISTINCT c.id)
      , 'l'
 FROM 
      ((locationsV x left outer join locationReviews xr on x.id = xr.lid)
       left outer join reviews r on r.id = xr.id) 
       left outer join comments c on xr.id = c.reviewId
 WHERE 
      x.vNo = 0  
      AND (r.enteredBy IS NULL OR 
            (r.enteredBy <> x.enteredBy 
             AND c.enteredBy <> x.enteredBy
             AND r.enteredBY NOT IN 
                       (SELECT requested FROM friends WHERE requester = x.enteredBY)
             AND r.enteredBY NOT IN 
                       (SELECT requester FROM friends WHERE requested = x.enteredBY)))
     AND (c.enteredBy IS NULL OR 
             (c.enteredBY NOT IN 
                       (SELECT requested FROM friends WHERE requester = x.enteredBY)
             AND c.enteredBY NOT IN 
                       (SELECT requester FROM friends WHERE requested = x.enteredBY)))
 GROUP BY 
     x.enteredBy
     , x.id

I tried adding something like this to the beginning, but the overall time it took didn't change.

CREATE INDEX friends1_idx ON friends(requested);
CREATE INDEX friends2_idx ON friends(requester);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

☆獨立☆ 2024-11-11 19:40:44

我认为除了查看索引之外,还可以优化 SQL 本身以提高性能。在 WHERE 子句中使用这些 IN 子句可能会导致优化器执行全表扫描。因此,如果您可以将这些移动到 FROM 部分中的表,您将获得更好的性能。此外,在 SELECT 语句中包含 COUNT(DISTINCT ...) 子句似乎有问题。如果您可以进行更改,因此 DISTINCT 子句是必要的,并且只需使用 COUNT 聚合函数,您可能会更好。

在进行左连接之前,请考虑在 FROM 子句中使用 SQL 语句 - 类似于这样的结构:

SELECT ... 
FROM Table1 LEFT JOIN 
     (SELECT ... FROM Table2 INNER JOIN Table3 ON ...) AS Table4 ON
        Table1.somecolumn = Table4.somecolumn
...

我知道这不会为您提供解决方案,但希望它能帮助您思考问题的其他方面并探索解决绩效的其他方法。

I think the SQL itself could be optimized to improve performance in addition to looking at indexes. Having those IN clauses in the WHERE clause may cause the optimizer do full table scans. So if you could move those to be tables in the FROM section you would have better performance. Also, having the COUNT(DISTINCT ...) clauses in in the SELECT statement seems problematic. You would likely be better off if you could make changes so the DISTINCT clauses were necessary there and simply use the COUNT aggregate function.

Consider using a SQL statement in the FROM clause before you do the left join--a structure something like this:

SELECT ... 
FROM Table1 LEFT JOIN 
     (SELECT ... FROM Table2 INNER JOIN Table3 ON ...) AS Table4 ON
        Table1.somecolumn = Table4.somecolumn
...

I know this isn't giving you the solution, but hopefully it will help you to think about other aspects of the problem and to explore other ways to address performance.

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