i'm designing a web application that has a rather complex query to search for points nearby the user's current location...along with quite a few other pieces of information. there are approximately 1.5 million rows in the main POI table, which is left joined to four other tables (the other smaller tables are for other data that is relevant to the main point of interest).
when running the query in the mysql console, it returns the data i'm looking for in 0.5 seconds or less (getting it to that point was enough of a PITA, but i finally figured out what indices i needed on the main POI table to make it happen)...but when i put the query into my app and have it run via the web (via jquery, ajax, and php pdo prepared statement), it takes upwards of 6-7 seconds to return any data...sometimes as long as 18-25 seconds.
what could i possibly be doing wrong that would cause the execute to take that long? is there something i need to make sure i'm doing that i might be forgetting somehow?
here's the relevant snippet of my php code (very simple) where the slowdown is actually happening:
$qry = "...";
$data = array(xxx); // user's lat/lon and other data we need
$sth = $this -> ci -> prepare($qry);
$sth -> execute($data);
for the record, running this as a non-prepared statement wasn't any better. in fact, it was actually a little bit slower...
please help me, i've been ripping my hair out over this all day. i thought once i finally got the query optimized it would solve the problem, but it turns out that's apparently not the case...
mysql> select
t.treasureID, t.buriedByUserID, t.lockLevel, t.currentGoldValue, t.initialSilverValue,
t.burySeen, t.initialGoldValue, t.prize1, t.buryPerkID, t.decoyOf,
t.unlockAttempts, t.unlockedByKeypad, t.unlockedByUserID, t.prizeID,
p.prizeDesc, p.validFrom, p.validUntil, p.sponsor, p.prizeName,
userB.displayName as bDisplayName, s.sponsorID, s.sponsorName, pb.perkName,
(DEGREES( ACOS( SIN( RADIANS( 40.6846 ) ) * SIN( RADIANS( t.latitude ) ) + COS( RADIANS( 40.6846 ) ) * COS( RADIANS( t.latitude ) ) * COS( RADIANS( -76.19613 - t.longitude ) ) ) ) * 60 * 1.1515 ) AS distance
from treasures t
left join prizes p on t.prizeID=p.prizeID
left join userInfo userB on userB.userID=t.buriedByUserID
left join sponsors s on p.sponsorID=s.sponsorID
left join perksB pb on t.buryPerkID=pb.perkID
where
t.unlockedByUserID=-1 and
t.buriedByUserID<>1011 and
t.isGlobal=0 and
t.latitude between 40.467351088803 and 40.901848911197 and
t.longitude between -76.483560028513 and -75.908699971487 and
((1361820374 > p.validFrom and 1361820374 < p.validUntil) or p.validUntil is null)
having distance < 15
order by distance asc
limit 0, 50;
+------------+----------------+-----------+------------------+--------------------+----------+------------------+--------+------------+---------+----------------+------------------+------------------+---------+---------------------------------------------------------------------------------------------------------------------------+------------+------------+---------------+-----------------+--------------+-----------+--------------+----------+---------------------+
| treasureID | buriedByUserID | lockLevel | currentGoldValue | initialSilverValue | burySeen | initialGoldValue | prize1 | buryPerkID | decoyOf | unlockAttempts | unlockedByKeypad | unlockedByUserID | prizeID | prizeDesc | validFrom | validUntil | sponsor | prizeName | bDisplayName | sponsorID | sponsorName | perkName | distance |
+------------+----------------+-----------+------------------+--------------------+----------+------------------+--------+------------+---------+----------------+------------------+------------------+---------+---------------------------------------------------------------------------------------------------------------------------+------------+------------+---------------+-----------------+--------------+-----------+--------------+----------+---------------------+
+------------+----------------+-----------+------------------+--------------------+----------+------------------+--------+------------+---------+----------------+------------------+------------------+---------+---------------------------------------------------------------------------------------------------------------------------+------------+------------+---------------+-----------------+--------------+-----------+--------------+----------+---------------------+
50 rows in set (0.78 sec)