gpt4 book ai didi

mysql - 如何减少这个查询(SQL)?

转载 作者:行者123 更新时间:2023-11-29 05:27:20 24 4
gpt4 key购买 nike

$sql = "SELECT min(id) FROM matchTrip where userTripId = :tripId AND 
matchStrength = (SELECT MIN(matchStrength) FROM matchTrip where userTripId = :tripId) ";

是否有可能减少此查询以便提高性能并减少执行此查询的时间?

我们将不胜感激。

最佳答案

您正在为 matchStrength 的最小值寻找 id 的最小值。因此,您可以:

SELECT 
id
FROM
matchTrip
WHERE
userTripId = :tripId
ORDER BY
matchStrength,
id
LIMIT 1

在任何情况下,您都需要通过 matchStrength 获得索引(我想您已经为 id 字段设置了索引)。至于性能 - 您必须采取措施,没有其他方法可以确保一件事在性能方面优于另一件事。

关于mysql - 如何减少这个查询(SQL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com