Total rows: ' . mysqli_num_rows(mysqli_query($con, "SELECT * FROM floats;")) . '
';
print 'Selecting rows within 50 miles of Seattle...
';
flush();
print '';
$start = microtime(true);
$result = mysqli_query($con, "CALL float_coords_within_radius(47.6, -122.33, 50);"); // Within 50 miles of Seattle
$end = microtime(true);
print '- floats : procedure
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
mysqli_next_result($con);
flush();
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM floats WHERE haversine(lat, lon, 47.6, -122.33) < 50;"); // Within 50 miles of Seattle
$end = microtime(true);
print '- floats : function
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
mysqli_next_result($con);
flush();
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM floats WHERE haversine_alt(lat, lon, 47.6, -122.33) < 50;");
$end = microtime(true);
print '- floats : function alt
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
mysqli_next_result($con);
flush();
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM floats where acos(
cos(radians(lat)) *
cos(radians(47.6)) *
cos(radians(lon) - radians(-122.33)) +
sin(radians(lat)) *
sin(radians(47.6))
) * 3959 < 50;"); // Within 50 miles of Seattle
$end = microtime(true);
print '- floats : inlined
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
flush();
$results = [];
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM floats;");
while ($row = mysqli_fetch_assoc($result)) {
if (haversine($row['lat'], $row['lon'], 47.6, -122.33) < 50) { // Within 50 miles of Seattle
$results[] = $row;
}
}
$end = microtime(true);
print '- floats : php
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . count($results) . '
';
flush();
$start = microtime(true);
$result = mysqli_query($con, "CALL hybrid_coords_within_radius(47.6, -122.33, 80467);"); // Within 80,467 meters (50 miles) of Seattle
$end = microtime(true);
print '- hybrid : procedure
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
mysqli_next_result($con);
flush();
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM floats WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), POINT(lon, lat)) < 80467;"); // Within 80,467 meters (50 miles) of Seattle
$end = microtime(true);
print '- hybrid : inlined
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
flush();
$start = microtime(true);
$result = mysqli_query($con, "CALL point_coords_within_radius(POINT(-122.33, 47.6), 80467);"); // Within 80,467 meters (50 miles) of Seattle
$end = microtime(true);
print '- points : procedure
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
mysqli_next_result($con);
flush();
$start = microtime(true);
$result = mysqli_query($con, "SELECT * FROM points WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), coord) < 80467;"); // Within 80,467 meters (50 miles) of Seattle
$end = microtime(true);
print '- points : inlined
';
print '- Time taken: ' . ($end - $start) . '
';
print '- Results: ' . mysqli_num_rows($result) . '
';
print '
';
function haversine($latFrom, $lonFrom, $latTo, $lonTo)
{
// convert from degrees to radians
$latFrom = deg2rad($latFrom);
$lonFrom = deg2rad($lonFrom);
$latTo = deg2rad($latTo);
$lonTo = deg2rad($lonTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = sin($latDelta / 2) * sin($latDelta / 2) +
cos($latFrom) * cos($latTo) *
sin($lonDelta / 2) * sin($lonDelta / 2);
$chord = 2 * atan2(sqrt($angle), sqrt(1 - $angle));
return 3959 * $chord; // as miles
}