Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

$sql = "SELECT id FROM matchTrip where userTripId = :tripId ORDER BY matchStrength, id LIMIT 1"; 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top