How to optimize the query below?

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 is the registered date.

It takes more than 8 seconds to run this request. I have over 500,000 entries in the reservation table and 40,000 entries in the user table.

I did not do any optimization for my database tables. Nothing. PC only. How can I optimize this query? What are the options for improving the performance of this database.

thanks

Booking in the table:

 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 
+6
source share
1 answer

First, I would rewrite the query as follows:

 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 

INNER JOIN is just a different syntax than yours, but it does the same. In my opinion, it is less error prone and more eye friendly, since you do not need to highlight the connection in the WHERE clause and what is not.

Since the bx_date column is of type DATE , you do not need the DATE() function to make a date from it again. In general, using a function in a column prevents the use of an index (if exists in this column).

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

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

You can add an index like this:

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

Read more here .

You can also use composite indexes, these are indexes across multiple columns.

UPDATE Thanks to ypercube in the comments:

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

You can then check if or which index is being used by placing EXPLAIN in front of your request. And of course, you have to measure, measure and measure whether the index is good or not. Overindexing is also not a good idea, since INSERTS and UPDATES are becoming much slower. To make sure the index simplifies your query, you need to make sure that it is not in the cache somewhere, so just to check the query execution using SQL_NO_CACHE as follows:

 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 is not recommended to index columns that have very few different values ​​when the table has many rows. There are only 4 different values ​​in the status column on the reservation. Adding an index to this column makes the index useless. This can even get worse, since MySQL can use the index and then have to do an additional search to find the actual data corresponding to the index position.

Very good read about this Use-the-index-Luke

+3
source

Source: https://habr.com/ru/post/923461/


All Articles