質問

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

Is it possible to reduce this query So that performance is increased and time to execute this query is reduced?

Any help will be appreciated.

役に立ちましたか?

解決

You're looking for minimum value of id for minimum value of matchStrength. Therefore, you can do:

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

in any case you need to have index via matchStrength (I think you already have it for id field). As for performance - you have to do measures, there's no certain other way to be sure that one thing is better than another in terms of performance.

他のヒント

$sql = "SELECT id FROM matchTrip where userTripId = :tripId ORDER BY matchStrength, id LIMIT 1"; 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top