Jump to content

Slow on first few queries, then really fast


mortenj

Recommended Posts

I am a Mysql novice.

 

After deploying the PHP/Mysql code on my host, Site5, a strange pattern emerged.

 

When I'm using my web service, all queries are fast. But then, if no users have been using it for a few hours (I guess), the first query after that period is extremely slow - so slow it ends in a timeout.

 

The database is rather large. Here's the table in question.

 

Table B (madsted)
Format  	fixed
Collation 	utf8_general_ci
Rows 	49,393
Row length ø 	4,518
Row size ø 	4,565 B

 

And the sql query:

 

$sql="SELECT navn1, lat, lon, postnr, virksomhedstype, branchekode, adresse1, 
degrees(acos(
sin( radians(madsted.lat) )
* sin( radians(".$lat."))
+ cos( radians(madsted.lat))
* cos( radians(".$lat."))
* cos( radians(madsted.lon - ".$lon.") ) 
) ) * 69.09 * 1.609344 * 1000 as meterdistance
FROM madsted
WHERE degrees(acos(
sin( radians(madsted.lat) )
* sin( radians(".$lat."))
+ cos( radians(madsted.lat))
* cos( radians(".$lat."))     	
* cos( radians(madsted.lon - ".$lon.") ) 
) ) * 69.09 < (10 * 1.609344)   
AND (
	virksomhedstype = 'Detail'
	)       
AND (
	branchekode <> '5552B'
	and branchekode <> '5551A'
	)	
order by meterdistance limit 20;";   

 

 

Link to comment
Share on other sites

Can anyone explain how I keep the indexes loaded?

 

I've done an explain for each query. 

 

EXPLAIN SELECT navn1, lat, lon, postnr, virksomhedstype, branchekode, adresse1, degrees( acos( sin( radians( madsted.lat ) ) * sin( radians( 55.66809314904700300985 ) ) + cos( radians( madsted.lat ) ) * cos( radians( 55.66809314904700300985 ) ) * cos( radians( madsted.lon - 12.54388122614099998486 ) ) ) ) * 69.09 * 1.609344 *1000 AS meterdistance
FROM madsted
WHERE degrees( acos( sin( radians( madsted.lat ) ) * sin( radians( 55.66809314904700300985 ) ) + cos( radians( madsted.lat ) ) * cos( radians( 55.66809314904700300985 ) ) * cos( radians( madsted.lon - 12.54388122614099998486 ) ) ) ) * 69.09 < ( 10 * 1.609344 )
AND (
virksomhedstype = 'Detail'
)
AND (
branchekode <> '5552B'
AND branchekode <> '5551A'
)
ORDER BY meterdistance
LIMIT 20 ;

 

And the output

 

1  	SIMPLE  	madsted  	range  	branchekodeindex  	branchekodeindex  	61  	NULL  	41719  	Using where; Using filesort

 

 

Next query

 


EXPLAIN SELECT *
FROM madsted
WHERE navn1 LIKE '%sim%'
AND (
lat IS NOT NULL
)
LIMIT 0 , 100

 

and its explain output

 

1  	SIMPLE  	madsted  	range  	latindex  	latindex  	24  	NULL  	43766  	Using where

 

 

Link to comment
Share on other sites

Could you fill the table with the cos() and sin() values for each row? I am willing to bet that it would cut out ALOT of work.

From what I see in the query it has to compute sin, cos and rad->degree values for each row and that must take up quite a bit of query time. If you could pre-compute the values AFAIK it would remove alot of computation and you could index those columns, at the moment no indexes are being used.

Link to comment
Share on other sites

I think you're right. But the calculations are done with varying variables.

 

Anyway, the strange thing is that this query is slow, too - when performed first - despite its simplicity.

 

SELECT navn1, lat, lon
FROM madsted
WHERE navn1 LIKE '%sim%'
AND (
lat IS NOT NULL
)
LIMIT 0 , 100

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.