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 - 预订日期。
运行此查询需要 8 秒以上。我在预订表中有超过 500,000 条记录,在用户表中有 40,000 条记录。
我没有对我的数据库表进行任何优化。什么都没有。仅限PK。如何优化此查询?有哪些选项可以提高此数据库的性能。
谢谢
订桌:
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
表用户:
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