I have an issue with a search function, it works correctly but is taking between 14-18 second to load and that is causing me great pain.
This is the search function I am using:
$sql="select *,
( 3959 * acos( cos( radians( $lat ) ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians( $lng ) ) + sin( radians( $lat ) ) * sin( radians( `lat` ) ) ) ) AS distance
FROM postcodes,
details
WHERE `postcode`=postcodes.outcode
HAVING " . implode(" AND ", $searches)."
AND isactive='y'
ORDER BY `distance`";
If anyone has any suggestions they would all be welcomed
I have spent more time working on it, and understand exactly what the query is doing before I added any more information.
This is now my updated query, I tried using a left join but I couldn't join the postcode table onto the cardetails table, and when I reversed it the cardetails columns where all showing up null.
SELECT "column names",
( 3959 * acos( cos( radians( $lat ) ) * cos( radians( `lat` ) ) * cos( radians(
`lng` ) - radians( $lng ) ) + sin( radians( $lat ) ) * sin( radians( `lat` ) ) ))
AS distance
FROM details
inner JOIN car_postcodes ON details.postcode = postcodes.outcode AND
distance < 100 AND payment='y' AND expired='n' ORDER BY
details.price
That has drastically reduced the time taken to perform the query (down to around 6seconds) so I still need to improve that.
The problem I am having now is that I am not able to use the column alias 'distance' to measure the distance between the user and the field within the table. As I cant use column alias to within the where clause.
The answer I seem to have read on many other posts is a sub select,