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 8420671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:55:48+00:00 2026-06-10T02:55:48+00:00

SELECT r.*, u.username FROM `reservation` AS r JOIN `users` AS u WHERE u.id =

  • 0
SELECT r.*, u.username 
FROM `reservation` AS r JOIN 
     `users` AS u 
WHERE u.id = r.user_id 
AND   DATE(r.bx_date) >= DATE('2012-08-22') 
AND   DATE(r.bx_date) <= DATE('2012-08-22') 
AND   r.status='1' 
ORDER BY r.id desc

bx_date – booked date.

It takes more than 8 secs to run this query. I have more than 500,000 records in reservation table and 40,000 records in users table.

I have not done any optimization to my database tables. Nothing at all. Just PKs only. How can I optimize this query ? What are the options to increase the performance of this database.

Thanks

Table reservation:

CREATE TABLE `reservation` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` int(10) unsigned NOT NULL COMMENT 'User ID',
 `tx_id` varchar(15) NOT NULL COMMENT 'Transaction ID',
 `tx_date` datetime NOT NULL COMMENT 'Transaction Date',
 `bx_date` date NOT NULL COMMENT 'Booking Date',
 `theater_id` int(10) unsigned NOT NULL COMMENT 'Theater ID',
 `movie_id` int(10) unsigned NOT NULL COMMENT 'Movie ID',
 `showtime_id` int(10) unsigned NOT NULL COMMENT 'Show Time ID',
 `category_id` int(10) unsigned NOT NULL COMMENT 'Category ID',
 `full_tickets` tinyint(2) unsigned NOT NULL COMMENT 'Number of Full Tickets',
 `half_tickets` tinyint(2) unsigned NOT NULL COMMENT 'Number of Half Tickets',
 `no_seats` tinyint(3) unsigned NOT NULL COMMENT 'No of Seats',
 `full_ticket_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT 'Full Ticket Price',
 `half_ticket_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT 'Half Ticket Price',
 `amount` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT 'Total Amount',
 `method` tinyint(1) unsigned NOT NULL COMMENT 'Payment Method 1=web 2=mobile 3=theater',
 `paymentgateway_id` int(10) unsigned NOT NULL COMMENT 'Payment Gateway ID',
 `payment_type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '0 = Cash 1=Credit Card',
 `status` tinyint(1) unsigned NOT NULL COMMENT 'Status 0=provisional 1=booked 2=canceled 3=auto canceld',
 `comment` text,
 `reservation_type` tinyint(1) unsigned NOT NULL COMMENT 'Reservation Type 0=complimentary 1=advamce booking 2=Mobile Booking, 3 - Theater Offline Booking',
 `complimentary_type` tinyint(1) unsigned NOT NULL COMMENT '0=none 1=loyalty rewards 2=marketing 3=promotional 4=show blocking 5=vip',
 `description` mediumtext NOT NULL COMMENT 'Complimentary Description',
 `title` varchar(10) DEFAULT NULL COMMENT 'Title',
 `fname` varchar(255) DEFAULT NULL COMMENT 'First Name',
 `lname` varchar(255) DEFAULT NULL COMMENT 'Last Name',
 `gender` tinyint(1) unsigned DEFAULT NULL COMMENT 'Gender 0=male 1=female',
 `dob` date DEFAULT '0000-00-00' COMMENT 'Date of Birth',
 `nic` varchar(10) DEFAULT NULL COMMENT 'NIC no',
 `address` text COMMENT 'Address Line 1',
 `city` varchar(255) DEFAULT NULL COMMENT 'City',
 `district` varchar(50) DEFAULT NULL COMMENT 'District ',
 `country` varchar(255) DEFAULT NULL COMMENT 'Country',
 `mobile` varchar(15) DEFAULT NULL COMMENT 'Mobile No',
 `contact_phone` varchar(15) DEFAULT NULL COMMENT 'Fixed Land Phone Number',
 `email` varchar(255) DEFAULT NULL COMMENT 'Email Address',
 `timer` int(4) unsigned NOT NULL,
 PRIMARY KEY (`id`),
 KEY `user_id_index` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=555706 DEFAULT CHARSET=utf8

Table users:

CREATE TABLE users ( id int(11) NOT NULL AUTO_INCREMENT,
 title varchar(100) NOT NULL,
 name varchar(255) NOT NULL DEFAULT '',
 last_name varchar(255) NOT NULL,
 username varchar(150) NOT NULL DEFAULT '',
 email varchar(100) NOT NULL DEFAULT '',
 password varchar(100) NOT NULL DEFAULT '',
 usertype varchar(25) NOT NULL DEFAULT '',
 block tinyint(4) NOT NULL DEFAULT '0',
 sendEmail tinyint(4) DEFAULT '0',
 registerDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 lastvisitDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 activation varchar(100) NOT NULL DEFAULT '',
 params text NOT NULL,
 gender varchar(6) NOT NULL,
 date_of_birth date NOT NULL,
 nic_no varchar(10) NOT NULL,
 address varchar(255) NOT NULL,
 city varchar(100) NOT NULL,
 district varchar(100) NOT NULL,
 mobile varchar(15) NOT NULL,
 subscribe_sms tinyint(1) NOT NULL,
 contact_phone varchar(15) NOT NULL,
 newsletter_subscribe tinyint(1) NOT NULL,
 PRIMARY KEY (id),
 UNIQUE KEY username (username),
 KEY usertype (usertype),
 KEY idx_name (name),
 KEY idx_block (block),
 KEY email (email)) 
 ENGINE=InnoDB AUTO_INCREMENT=34265 DEFAULT CHARSET=utf8
  • 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-06-10T02:55:49+00:00Added an answer on June 10, 2026 at 2:55 am

    First I would rewrite the query like this:

    SELECT r.*, u.username 
    FROM 
    `reservation` AS r 
    INNER JOIN `users` AS u ON u.id = r.user_id 
    WHERE 
    r.bx_date = '2012-08-22' /* or use BETWEEN when you really have a range */
    AND   r.status='1' 
    ORDER BY r.id desc
    

    The INNER JOIN is just a different syntax than yours, but it does the same. In my opinion this one is less error prone and more eye friendly, since you don’t have to separate in the WHERE clause what’s a join and what’s not.

    Since the bx_date column is of type DATE you don’t need the function DATE() to make a date out of it again. In general the use of a function on a column prevents the use of an index (if exists on that column).

    To keep things simple at the start, just start by adding indexes to columns which are frequently used for joins or used in WHERE clauses.

    In your case the columns used for the JOIN are already primary keys, so they implicitly already have an index.

    You can add an index like this:

    ALTER TABLE reservation ADD INDEX idx_reservation_bx_date (bx_date); /*if I remember correctly :) */
    

    For more info read here.

    You can also use compound indexes, this are indexes over multiple columns.

    UPDATE Thanks to ypercube’s suggest in comments:

    ALTER TABLE reservation ADD INDEX idx_reservation_bx_date_status (bx_date, status); /*if I remember correctly :) */
    

    You can then check if or what index is used by putting an EXPLAIN in front of your query. And of course you have to measure, measure and measure if an index is good or not. Overindexing is also not a good idea, since INSERTS and UPDATES become much slower. To see if an index fastens your query you have to make sure it’s not in cache somewhere, so just to test execute your query with SQL_NO_CACHE like this:

    SELECT SQL_NO_CACHE r.*, u.username 
    FROM 
    `reservation` AS r 
    INNER JOIN `users` AS u ON u.id = r.user_id 
    WHERE 
    r.bx_date = '2012-08-22' /* or use BETWEEN when you really have a range */
    AND   r.status='1' 
    ORDER BY r.id desc;
    

    —

    </UPDATE>
    

    Another rule is, that it’s not a good idea to index columns which have very few distinct values when the table has many many rows. Your status column in reservation only has 4 distinct values. Adding an index on this column renders the index useless. It can even get worse, since MySQL might use the index and then has to do an extra lookup to find the actual data corresponding to the index position.

    A very good read about it is Use-the-index-Luke

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

Sidebar

Related Questions

I currently am trying the following code: $sql = $db->query(select id,username,fullname from userss ORDER
I have a simple query in php on oracle: $query='select u.username,u.lastname,u.firstname,c.event,c.reason from users u,
I need to do the following: Select Usernames from table delivery where date>2012-06-01 00:00:00
Say i have a query select username from users. I want to output this
this is some of my script so far: $check = (SELECT username FROM users
OleDbDataAdapter dt = new OleDbDataAdapter(select UserName from UserInfo where User_ID = (select User_ID from
I want to build a query like SELECT username from users WHERE login_attempts >
Query: SELECT id, username, (SELECT username FROM users WHERE email = '{$email}' AND activate
Here's the code: <?php mysqlLogin(); $username = $_COOKIE['username']; $sql = mysql_query(SELECT username FROM `users`
If I issue SELECT username FROM Users I get this result: username -------- Paul

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.