Question

$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.

Was it helpful?

Solution

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.

OTHER TIPS

$sql = "SELECT id FROM matchTrip where userTripId = :tripId ORDER BY matchStrength, id LIMIT 1"; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top