需要帮助优化数据库查询。对指数非常缺乏经验
我需要优化这个查询。教授建议使用索引,但我对如何使用索引感到非常困惑。如果我能得到一个例子来说明什么是好的索引、为什么以及所需的实际代码,那么我绝对可以自己完成其余的工作。任何帮助都会很棒。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为除了查看索引之外,还可以优化 SQL 本身以提高性能。在 WHERE 子句中使用这些 IN 子句可能会导致优化器执行全表扫描。因此,如果您可以将这些移动到
FROM
部分中的表,您将获得更好的性能。此外,在SELECT
语句中包含COUNT(DISTINCT ...)
子句似乎有问题。如果您可以进行更改,因此 DISTINCT 子句是必要的,并且只需使用 COUNT 聚合函数,您可能会更好。在进行左连接之前,请考虑在
FROM
子句中使用 SQL 语句 - 类似于这样的结构:我知道这不会为您提供解决方案,但希望它能帮助您思考问题的其他方面并探索解决绩效的其他方法。
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 theFROM
section you would have better performance. Also, having theCOUNT(DISTINCT ...)
clauses in in theSELECT
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: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.