Total rows: ' . mysqli_num_rows(mysqli_query($con, "SELECT * FROM floats;")) . '

Selecting rows within 50 miles of Seattle...

'; flush(); test( $con, 'floats : procedure', "CALL float_coords_within_radius(47.6, -122.33, 50);" // Within 50 miles of Seattle ); test( $con, 'floats_indexed : procedure', "CALL float_indexed_coords_within_radius(47.6, -122.33, 50);" // Within 50 miles of Seattle ); test( $con, 'floats : function', "SELECT * FROM floats WHERE haversine(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle false // disabled ); test( $con, 'floats : function alt', "SELECT * FROM floats WHERE haversine_alt(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle false // disabled ); test( $con, 'floats_indexed : function', "SELECT * FROM floats_indexed WHERE haversine(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle false // disabled ); test( $con, 'floats_indexed : function alt', "SELECT * FROM floats_indexed WHERE haversine_alt(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle false // disabled ); test( $con, 'floats : inlined', "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 ); test( $con, 'floats_indexed : inlined', "SELECT * FROM floats_indexed 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 ); { // $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); // row('floats : php', $end - $start, 'SELECT * FROM floats;', count($results)); row('floats : php', 'disabled', 'SELECT * FROM floats;', 0, true); // disabled } test( $con, 'hybrid : procedure', 'CALL hybrid_coords_within_radius(47.6, -122.33, 80467);' // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'hybrid_point : procedure', 'CALL hybrid_coords_within_radius_point(POINT(-122.33, 47.6), 80467);' // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'hybrid_indexed : procedure', 'CALL hybrid_indexed_coords_within_radius(47.6, -122.33, 80467);' // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'hybrid_indexed_point : procedure', 'CALL hybrid_indexed_coords_within_radius_point(POINT(-122.33, 47.6), 80467);' // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'hybrid : inlined', "SELECT * FROM floats WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), POINT(lon, lat)) < 80467;", // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'hybrid_indexed : inlined', "SELECT * FROM floats_indexed WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), POINT(lon, lat)) < 80467;" // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'points : procedure', "CALL point_coords_within_radius(POINT(-122.33, 47.6), 80467);" // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'points_indexed : procedure', "CALL point_indexed_coords_within_radius(POINT(-122.33, 47.6), 80467);" // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'points : inlined', "SELECT * FROM points WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), coord) < 80467;" // Within 80,467 meters (50 miles) of Seattle ); test( $con, 'points_indexed : inlined', "SELECT * FROM points_indexed WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), coord) < 80467;" // Within 80,467 meters (50 miles) of Seattle ); print '
Test Time taken Query Results
'; 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 } function test($con, $name, $query, $enabled = true) { if (!$enabled) { row($name, 'disabled', $query, 0, true); return; } $start = microtime(true); $result = mysqli_query($con, $query); $end = microtime(true); row($name, $end - $start, $query, mysqli_num_rows($result)); try { mysqli_next_result($con); } catch (Exception $e) {} } function row($name, $duration, $query, $results, $disabled = false) { print ' ' . htmlspecialchars($name) . '
' . htmlspecialchars($query) . '
' . $results . ' '; flush(); }