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

  • Home
  • SEARCH
  • 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 6150507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:37:26+00:00 2026-05-23T19:37:26+00:00

I have a MySQL query below that I’m using to get events for my

  • 0

I have a MySQL query below that I’m using to get events for my event listings page. The problem is, it’s taking ~35 seconds to run with the limit 10, and another ~35 seconds to do the COUNT for pagination. 70+ seconds page-load time just won’t cut it, as you can imagine. And this is only with 740 event results! I’m scared to think how this will run when we get 2000+.

We’ve tried indexing (to the best of our lacking index-knowledge), and that had literally zero effect.

Explanation of table associations:
An Event can be held at either a restaurant or a venue. The City of that event is determined by the city_id of the Restaurant or Venue it’s being held at. It’s also getting Uploads (photos in this case).

The somewhat confusing part is the Schedule/Date – a Schedule(s) holds the start/end/repeat information for an event. The Date records are created based on the Schedule’s information and holds an individual record for every day the event is being held (start = datetime, end = datetime)

I’m using CakePHP to create this query, and have listed my associations at the bottom:

SELECT
`Event`.*, `Venue`.`id`, `Venue`.`slug`, `Venue`.`name`, `Venue`.`GPS_Lon`,
`Venue`.`GPS_Lat`, `Venue`.`city_id`, `VenueCity`.`name`, `VenueCity`.`slug`,
`Restaurant`.`id`, `Restaurant`.`slug`, `Restaurant`.`name`, `Restaurant`.`GPS_Lat`,
`Restaurant`.`GPS_Lon`, `Restaurant`.`city_id`, `RestaurantCity`.`name`, 
`RestaurantCity`.`slug`, GROUP_CONCAT(Date.start, "|", Date.end

ORDER BY Date.start ASC SEPARATOR "||") AS EventDates
FROM `events` AS `Event`
LEFT JOIN restaurants AS `Restaurant` ON (`Restaurant`.`id` = `Event`.`restaurant_id`)
LEFT JOIN venues AS `Venue` ON (`Venue`.`id` = `Event`.`venue_id`)
LEFT JOIN cities AS `VenueCity` ON (`Venue`.`city_id` = `VenueCity`.`id`)
LEFT JOIN cities AS `RestaurantCity` ON (`Restaurant`.`city_id` = `RestaurantCity`.`id`)
INNER JOIN schedules AS `Schedule` ON (`Schedule`.`event_id` = `Event`.`id`)
INNER JOIN dates AS `Date` ON (`Date`.`schedule_id` = `Schedule`.`id`)
LEFT JOIN uploads AS `Upload` ON (`Upload`.`event_id` = `Event`.`id`)
WHERE `Event`.`approval_status_id` = 1 AND `Date`.`start` >= '2011-07-11 12:38:54'
GROUP BY `Event`.`id`
ORDER BY `Date`.`start` ASC LIMIT 10

CakePHP associations:

Event belongsTo Venue
Venue hasMany Event

Event belongsTo Restaurant
Restaurant hasmany Event

Event hasMany Upload
Upload belongsTo Event

City hasMany Restaurant
City hasMany Venue
Restaurant belongsTo City
Venue belongsTo City

Event hasMany Schedule
Schedule belongsTo Event
Schedule hasMany Date
Date belongsTo Schedule

UPDATE (per @Zoredache request):

This is what I get from adding EXPLAIN before the select:

id  select_type  table          type  possible_keys            key              key_len   ref                             rows  Extra
1   SIMPLE       Event          ref   PRIMARY,approval status  approval status  5         const                           946   Using where; Using temporary; Using filesort
1   SIMPLE       Restaurant     ref   PRIMARY,id               id               4         medut_ent.Event.restaurant_id   1 
1   SIMPLE       Venue          ref   PRIMARY,id               id               4         medut_ent.Event.venue_id        1 
1   SIMPLE       VenueCity      ref   PRIMARY,id               id               4         medut_ent.Venue.city_id         1 
1   SIMPLE       RestaurantCity ref   PRIMARY,id               id               4         medut_ent.Restaurant.city_id    1 
1   SIMPLE       Schedule       ref   PRIMARY,index            index            5         medut_ent.Event.id              1     Using where; Using index
1   SIMPLE       Date           ref   all cols,start...        all cols         5         medut_ent.Schedule.id           8     Using where; Using index
1   SIMPLE       Upload         ALL                                                                                       4240  
  • 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-23T19:37:27+00:00Added an answer on May 23, 2026 at 7:37 pm

    Turns out, the Upload table had no index. Simply adding an index to that made it run incredibly fast (142ms instead of 75000+ms).

    I found the issue via EXPLAIN SELECT …(thanks to @Zoredache) – details of the EXPLAIN in “UPDATE” of answer.

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

Sidebar

Related Questions

Using the MySQL query below, I have created a pivot table which is pretty
I have a MySQL query that returns a result with a single column of
I have a MySQL query that I use to retrieve random rows from a
I have been using the MySql Query Browser and [opinion]it has to be the
I have a MySQL (v 5, MyISAM) query that returns different rows depending on
I have a dataset in MySQL where using limit is already an expensive query,
I have a mysql query that targets a single column in a single row
The code below works great. I have a MySQL database that contains book titles
I have a MySQL query called $sqlStr5 that ranks rows by a metric called
The query below is not inserting the variables into MySQL. I know that the

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.