Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:55:35+00:00 2026-05-16T21:55:35+00:00

I have a database call that i am not sure if i am doing

  • 0

I have a database call that i am not sure if i am doing it the most efficient way. Basically the call queries a table of events with zip codes and then joins a zip code database that gives the lat/lon of that events zip. Then it joins the logged in user to the query and that user has a lat/lon of upon logging in. So the whole query pulls events from within so many miles of of the users lat/lon.

My question, is there a better way to do it then calling this query each time the page is loaded? would a stored procedure be faster? I dont have any experience with them. I am using MySQL.

    $this->db->select('*');
$this->db->from('events');
$this->db->join('zipcodes', 'zipcodes.zipcode = courses.courseZip');
$this->db->join('eventTypes', 'eventTypes.eventTypeID = events.eventType');
$this->db->where('eventApproved', 1);
$this->db->select('(DEGREES(ACOS(SIN(RADIANS('.$this->user['userLat'].'))
     * SIN(RADIANS(latitude))
     + COS(RADIANS('.$this->user['userLat'].'))
     * COS(RADIANS(latitude))
     * COS(RADIANS('.$this->user['userLon'].' - longitude))))) * 69.09 AS distance');

$this->db->having('distance <', 100);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:55:36+00:00Added an answer on May 16, 2026 at 9:55 pm

    Yes it will help to have a stored procedure here.
    Reasons are
    1. it makes your Database layer more managable
    2. SP are precompiled. When you run then first them the engine will create a execution plan and save the plan. Next time when it runs it will reuse the plan. So you get some performance benifit. In your case you might get lot of benifit if the underlined table is not changing (updated/deleted) too much after SP is made. If it is then you can RECOMPILE the sp(Running it WITH RECOMPILE OPTION) and it will create and save a new plan.

    How you do it .
    Well it is quite easy. If you are using HeidiSQL for MySQL Front End or the query browser of MYSQL enterprise 5.0 you might be able to generate the SP graphically . But even if you want to code it from scratch it is easy.

    http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html

    Sp also are advisible from Security point of view because they can stop SQL Injection attacks.

    Once you have the SP you can tune the table to make the SP faster.
    1. Create a Index (nonclustered ) on the columns in where clause
    2. Include the column that you bringing in SELECT in this Index.

    In Microsoft SQL Server you can do this usign covering index. I am not sure if you can do it in MYSQL or not. But even if you can you should try to create a index covering as many columns as you can in either clustered or non clustered index.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a ServiceCall database table that records down all the service calls
I have a database with two tables - let's call them Foo and Bar.
I have enabled search in my MonoTouch.Dialog. After each keystroke I call the database,
i have database table like this +-------+--------------+----------+ | id | ip | date |
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
I am not sure what you call it in other technologies, on the IBM
I have a before_save method that I call that renames an uploaded image. before_save
Assume I have a database table with many names. I'd like to flex match
I have a multidimensional array from a a MySQL database call. It looks something
I have a Preference class (module) that's used across several different apps. Basically it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.