Paul Man
Paul Man

Reputation: 151

When do you create a custom function in mysql

I'm new to MySQL. I have a database that is accessed by many servers. I need to create a custom MySQL function like this for example:

DELIMITER $$

create function calcDistance(lat float, lng float, pnt_lat float, pnt_lng float)

Returns float
BEGIN

Declare dist float;
SET dist =
  3959 * acos (
  cos ( radians(pnt_lat) )
  * cos( radians( lat ) )
  * cos( radians( lng ) - radians(pnt_lng) )
  + sin ( radians(pnt_lat) )
  * sin( radians( lat ) )
);

RETURN dist;

END

Since i want this function to be used by many servers, im wondering HOW and WHEN do i send this function to the database?

Since i have many servers that can crash and rebooted at anytime, do i resend this same function to MySQL everytime a server boots up, but before i start making queries? What happens if i send the same function more than once, since the servers wont know if the function was already sent to the database by another server.

I dont really know how it works...Does anybody know the answers to this?

Thanks

Upvotes: 0

Views: 42

Answers (1)

Barmar
Barmar

Reputation: 780688

You don't need to resend it. This is a stored function, it's saved on the database server just like tables are. Once it's created, any client can use it until you explicitly drop it.

Upvotes: 1

Related Questions