"1062 Duplicate entry" on SELECT - mysql

I've got a SELECT query that's returning the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_id-name email#address.com--2018-11-04 01:24:38' for key ''
(I replaced the user_id, name and email for privacy)
SELECT users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created,
MAX(cards.card_id) AS latest_card_id, MAX(cards.date_created) AS latest_date_card, MAX(punches.date_created) AS date_lastvisited
FROM users
INNER JOIN cards ON cards.user_id = users.user_id
INNER JOIN punches ON punches.card_id = cards.card_id
WHERE punches.business_id=1626
GROUP BY users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created
ORDER BY users.fullname;
Here are the table definitions for cards, punches and users:
CREATE TABLE `cards` (
`card_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`printjob_id` int(11) DEFAULT NULL,
`temp` varchar(256) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`whitelabel_id` int(11) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`card_id`),
KEY `printjob_id` (`printjob_id`),
KEY `active` (`active`),
KEY `user_id` (`user_id`),
KEY `business_id` (`business_id`)
) ENGINE=InnoDB AUTO_INCREMENT=576475 DEFAULT CHARSET=latin1;
CREATE TABLE `punches` (
`punch_id` int(11) NOT NULL AUTO_INCREMENT,
`punch_count` int(11) unsigned NOT NULL DEFAULT '1',
`employeebonus` tinyint(1) NOT NULL DEFAULT '0',
`details` text,
`card_id` int(11) DEFAULT NULL,
`behaviour` set('basicpunch','employeebonus','visitsregularly','broughtfriend','mytreat','firstcustomer','dailyrepeater','visitslocations','facebookshare','mostfrequent','longabsence','registerer','employeepromoter','luckiest','enroll','opportunist') NOT NULL DEFAULT 'basicpunch',
`location_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`employee_id` int(11) DEFAULT NULL,
`v1` float DEFAULT NULL,
`eventcounter` smallint(5) unsigned NOT NULL DEFAULT '0',
`date_local` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`punch_id`),
KEY `business_id` (`business_id`),
KEY `employee_id` (`employee_id`),
KEY `card_id` (`card_id`),
KEY `date_created` (`date_created`),
KEY `behaviour_id` (`behaviour`),
KEY `location_id` (`location_id`),
KEY `employeebonus` (`employeebonus`),
KEY `punch_count` (`punch_count`)
) ENGINE=InnoDB AUTO_INCREMENT=807402 DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`fullname` varchar(60) DEFAULT NULL,
`email` varchar(50) NOT NULL,
`undeliverable` tinyint(4) DEFAULT '0',
`phone_formatted` varchar(25) DEFAULT NULL,
`phone_unformatted` varchar(15) DEFAULT NULL,
`password_hash` char(64) DEFAULT NULL,
`password_salt` char(6) DEFAULT NULL,
`reset_hash` binary(32) DEFAULT NULL,
`date_reset` datetime DEFAULT NULL,
`stripecustomer_id` int(11) DEFAULT NULL,
`stripe_customer_id` varchar(40) DEFAULT NULL,
`stripe_customer_access_id` varchar(40) DEFAULT NULL,
`problemwithpayment` tinyint(1) NOT NULL DEFAULT '0',
`enterprisemanager_whitelabel_id` int(11) NOT NULL DEFAULT '0',
`god` tinyint(1) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `email` (`email`),
KEY `phone_unformatted` (`phone_unformatted`),
KEY `date_reset` (`date_reset`),
KEY `undeliverable` (`undeliverable`)
) ENGINE=InnoDB AUTO_INCREMENT=16809 DEFAULT CHARSET=latin1;
I don't understand how I can get this error with a SELECT query. When I remove the MAX() functions, I no longer get the error.

Related

Does MySQL different engine types (InnoDB and Aria) affect on query with large amount of LEFT JOIN?

I have one heavy MySQL query with 12 LEFT JOIN and noticed that some of tables have InnoDB engine and some Aria.
I've attached EXPLAIN of this query and pointed engine types at image with corresponding letters I and A.
Does fact of different engines affects on query speed and how seriously? Does changing of engines types helps to optimize query execution?
What else I could do to optimize this query?
Query (if needed)
SELECT user.id, user.first_name, user.last_name, user.birthday, user.email, user.phone, user.address_id,
user.alt_address_id, user.type, user.level_id, user_level.level, user.consecutive_orders, user.orders_count,
user.code_id, user.period, user.preferences, user.is_pick_up, user.pickup_address_id, user.is_active,
user.first_delivery_date, user.change_delivery_date, user.eway_id, user.date_created,
user.rest_referral_discount as `rest_referral_discount`, user.rest_code_discount as `rest_code_discount`,
user.rest_code_box_discount as `rest_code_box_discount`, user.rest_code_mp_discount as `rest_code_mp_discount`,
user.rest_code_tup_discount as `rest_code_tup_discount`, user.nf_pantry_list, user.nf_next_weeks_menu,
user.nf_paused_reminder, user.nf_welcome, user.nf_expected_delivery_time,
user.nf_delivery, address_pickup.address as `pickup_address`, address.address, address.unit,
address.instructions, location.id location_id, location.postcode, location.suburb,
location_state.code state_code, delivery_area.delivery_area_window_id delivery_area_window_id,
delivery_area_by_location.fee delivery_fee_value, delivery_area_window.day delivery_day,
delivery_area_window.day_name_full delivery_day_name_full, delivery_area_window.window delivery_window,
user.delivery_area_id, user.alt_delivery_area_id, if (user.is_pick_up, 0, delivery_area.fee) delivery_fee,
if (user.type = 1 || user.type = 2, 5, 0) ondemand_fee, code.code code_name, code.amount code_amount,
code.value code_value, code.is_permanent is_code_permanent, code.repeat code_repeat, code.times code_times,
code.apply_to code_apply_to, code.valid_from code_valid_from, code.valid_until code_valid_until,
if (code_used.count > 0, 1, 0) is_code_used, if (code_used.count > 0, code_used.count, 0) code_used_count,
if (user.is_pick_up, IFNULL(delivery_area_by_location.topup_available, 0), IFNULL(delivery_area.topup_available, 0)) topup_available,
(SELECT `order`.`id` FROM `order` LEFT JOIN `gift` ON gift.order_id = order.id
WHERE (`order`.`delivery_date` BETWEEN '2021-02-07' and '2021-02-13') and (user.id = order.user_id) and (`gift`.`id` IS NULL)
and (`order`.`status` = 10) ORDER BY `order`.`date_created` DESC LIMIT 1) as `order_id`,
(SELECT `order`.`delivery_date` FROM `order`
LEFT JOIN `gift`
ON gift.order_id = order.id
WHERE (user.id = order.user_id) and (`gift`.`id` IS NULL) and (`order`.`status` = 10) ORDER BY `order`.`date_created` DESC LIMIT 1) as `last_order_delivery_date`
FROM `user`
LEFT JOIN `address`
ON user.address_id = address.id
LEFT JOIN `address_pickup`
ON user.pickup_address_id = address_pickup.id
LEFT JOIN `delivery_area`
ON user.delivery_area_id = delivery_area.id
LEFT JOIN `delivery_area_window`
ON delivery_area_window.id = delivery_area.delivery_area_window_id
LEFT JOIN `user_level`
ON user.level_id = user_level.id
LEFT JOIN `location`
ON address.location_id = location.id
LEFT JOIN `location_state`
ON location.state_id = location_state.id
LEFT JOIN `delivery_area` `delivery_area_by_location` ON delivery_area_by_location.location_id = location.id
LEFT JOIN (SELECT count(*) count, `user_id`, `code_id` FROM `order` WHERE (code_id > 0) and (`order`.`status` = 10) GROUP BY `user_id`, `code_id`) `code_used`
ON ((user.id = code_used.user_id) and (code_used.code_id = user.code_id))
LEFT JOIN `code`
ON user.code_id = code.id WHERE (`user`.`type` = 0) and (`user`.`status` = 10)
GROUP BY `user`.`id` ORDER BY `last_name` LIMIT 15
UPD: CREATE TABLE
CREATE TABLE `address` (
`id` int(11) UNSIGNED NOT NULL,
`location_id` int(11) UNSIGNED NOT NULL,
`address` text NOT NULL,
`unit` char(4) NOT NULL,
`instructions` text NOT NULL,
`regular` tinyint(1) NOT NULL DEFAULT '1',
`topup` tinyint(1) NOT NULL DEFAULT '1',
`date_from` date DEFAULT NULL,
`date_to` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `address_pickup` (
`id` tinyint(3) UNSIGNED NOT NULL,
`address` text NOT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `code` (
`id` smallint(6) UNSIGNED NOT NULL,
`code` varchar(255) NOT NULL,
`amount` decimal(7,2) NOT NULL,
`value` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 - number; 1 - percent',
`is_mswa` tinyint(4) DEFAULT '0',
`is_permanent` tinyint(4) DEFAULT '0',
`repeat` tinyint(4) DEFAULT '0',
`apply_to` tinyint(4) DEFAULT '0' COMMENT '0 - both; 1 - box; 2 - mp;',
`times` int(11) NOT NULL DEFAULT '0',
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`valid_from` date NOT NULL,
`valid_until` date DEFAULT NULL,
`author` varchar(255) NOT NULL,
`checkout_text` text NOT NULL,
`status` smallint(6) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `delivery_area` (
`id` int(10) UNSIGNED NOT NULL,
`location_id` int(11) NOT NULL,
`fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`delivery_area_window_id` tinyint(4) UNSIGNED NOT NULL DEFAULT '1',
`topup_available` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `delivery_area_window` (
`id` tinyint(4) UNSIGNED NOT NULL,
`day` tinyint(3) UNSIGNED NOT NULL,
`day_name` varchar(255) NOT NULL,
`day_name_full` varchar(255) NOT NULL,
`window` varchar(255) NOT NULL DEFAULT '',
`pickup` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `gift` (
`id` int(11) UNSIGNED NOT NULL,
`user_id` int(11) UNSIGNED NOT NULL,
`recipient_id` int(11) UNSIGNED NOT NULL,
`type` tinyint(1) NOT NULL COMMENT '0 - card; 1 - box;',
`message` text NOT NULL,
`token` char(24) DEFAULT NULL,
`order_id` int(11) UNSIGNED NOT NULL,
`is_redeemed` tinyint(1) NOT NULL,
`date_exp` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `location` (
`id` int(10) UNSIGNED NOT NULL,
`postcode` int(4) UNSIGNED NOT NULL,
`suburb` varchar(45) NOT NULL,
`dc` varchar(45) NOT NULL,
`state_id` tinyint(3) UNSIGNED NOT NULL,
`type_id` tinyint(4) UNSIGNED NOT NULL,
`lat` double DEFAULT NULL,
`lon` double DEFAULT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `location_state` (
`id` tinyint(4) UNSIGNED NOT NULL,
`code` char(3) NOT NULL,
`state` varchar(255) NOT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `order` (
`id` int(11) UNSIGNED NOT NULL,
`user_id` int(11) UNSIGNED NOT NULL,
`shipping_address_id` int(11) UNSIGNED NOT NULL,
`topup_address_id` int(10) UNSIGNED NOT NULL,
`pickup_address_id` tinyint(3) UNSIGNED DEFAULT NULL,
`preferences` int(11) UNSIGNED NOT NULL,
`referral_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`code_id` int(11) UNSIGNED NOT NULL,
`code_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`pickup_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`admin_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`level_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`delivery_fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`topup_delivery_fee` decimal(7,2) NOT NULL,
`ondemand_fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`amount` decimal(7,2) NOT NULL DEFAULT '0.00',
`retry_payment` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '1-wait answer; 2-recharge with sms; 3-recharge without sms',
`retry_payment_sms` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 - processed; >0 - sms id',
`retry_payment_email` int(10) UNSIGNED NOT NULL DEFAULT '0',
`eway_errors` text NOT NULL,
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`delivery_date` date DEFAULT NULL,
`status` smallint(6) UNSIGNED NOT NULL DEFAULT '10',
`processing_status` smallint(6) UNSIGNED NOT NULL,
`processing_status_edited` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`has_topup` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`is_topup_only` tinyint(1) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
CREATE TABLE `user` (
`id` int(11) UNSIGNED NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`birthday` date DEFAULT NULL,
`auth_key` varchar(32) NOT NULL,
`password_hash` varchar(255) NOT NULL,
`password_reset_token` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(255) DEFAULT NULL,
`custom_ref_code` varchar(255) DEFAULT NULL,
`eway_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'eWAY Customer Token ID',
`active_campaign_id` int(11) UNSIGNED DEFAULT NULL COMMENT 'ActiveCampaign subscriber_id',
`level_id` tinyint(4) UNSIGNED NOT NULL DEFAULT '1',
`address_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
`delivery_area_id` int(10) UNSIGNED DEFAULT NULL,
`alt_address_id` int(10) UNSIGNED DEFAULT NULL,
`alt_delivery_area_id` int(10) UNSIGNED DEFAULT NULL,
`code_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
`period` tinyint(4) UNSIGNED NOT NULL DEFAULT '1' COMMENT '1 - Once a Week; 2 - Fortnightly',
`preferences` int(11) UNSIGNED NOT NULL DEFAULT '0',
`is_pick_up` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`pickup_address_id` tinyint(3) UNSIGNED DEFAULT NULL,
`is_active` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0 - Inactive; 1 - Active; -1 - Blocked',
`status` smallint(6) NOT NULL DEFAULT '10',
`type` tinyint(4) UNSIGNED NOT NULL COMMENT '0 - Set And Forget; 1 - On Demand; 2 - Trial; 3 - Potential;',
`first_delivery_date` date DEFAULT NULL,
`last_delivery_date` date DEFAULT NULL,
`change_delivery_date` date DEFAULT NULL,
`nf_pantry_list` tinyint(1) NOT NULL DEFAULT '1',
`nf_next_weeks_menu` tinyint(1) NOT NULL DEFAULT '1',
`nf_paused_reminder` tinyint(1) NOT NULL DEFAULT '1',
`nf_expected_delivery_time` tinyint(1) NOT NULL DEFAULT '1',
`nf_delivery` tinyint(1) NOT NULL DEFAULT '1',
`nf_welcome` tinyint(1) NOT NULL DEFAULT '1',
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`consecutive_orders` smallint(5) UNSIGNED DEFAULT NULL,
`orders_count` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
`rest_referral_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_box_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_mp_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_tup_discount` decimal(7,2) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_level` (
`id` int(11) NOT NULL,
`level` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`description` text NOT NULL,
`from` int(11) NOT NULL DEFAULT '0',
`to` int(11) NOT NULL DEFAULT '0',
`discount` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And indexes
--
-- Indexes table `address`
--
ALTER TABLE `address`
ADD PRIMARY KEY (`id`),
ADD KEY `location_id` (`location_id`),
ADD KEY `regular` (`regular`),
ADD KEY `topup` (`topup`),
ADD KEY `date_to` (`date_to`),
ADD KEY `date_from` (`date_from`);
--
-- Indexes table `address_pickup`
--
ALTER TABLE `address_pickup`
ADD PRIMARY KEY (`id`);
--
-- Indexes table `code`
--
ALTER TABLE `code`
ADD PRIMARY KEY (`id`),
ADD KEY `status` (`status`),
ADD KEY `valid_from` (`valid_from`),
ADD KEY `valid_until` (`valid_until`),
ADD KEY `date_created` (`date_created`),
ADD KEY `code` (`code`) USING BTREE,
ADD KEY `is_mswa` (`is_mswa`);
--
-- Indexes table `delivery_area`
--
ALTER TABLE `delivery_area`
ADD PRIMARY KEY (`id`),
ADD KEY `day` (`delivery_area_window_id`),
ADD KEY `location_id` (`location_id`) USING BTREE,
ADD KEY `friday_topup_available` (`topup_available`);
--
-- Indexes table `delivery_area_window`
--
ALTER TABLE `delivery_area_window`
ADD PRIMARY KEY (`id`),
ADD KEY `day` (`day`),
ADD KEY `pickup_only` (`pickup`);
--
-- Indexes table `gift`
--
ALTER TABLE `gift`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `token` (`token`),
ADD KEY `user_id` (`user_id`),
ADD KEY `recipient_id` (`recipient_id`),
ADD KEY `order_id` (`order_id`),
ADD KEY `is_redeemed` (`is_redeemed`);
--
-- Indexes table `location`
--
ALTER TABLE `location`
ADD PRIMARY KEY (`id`),
ADD KEY `idx_lon` (`lon`),
ADD KEY `idx_lat` (`lat`),
ADD KEY `type_id` (`type_id`),
ADD KEY `state_id` (`state_id`),
ADD KEY `postcode` (`postcode`),
ADD KEY `suburb_2` (`suburb`),
ADD KEY `postcode_suburb` (`postcode`,`suburb`) USING BTREE;
ALTER TABLE `location` ADD FULLTEXT KEY `suburb` (`suburb`);
--
-- Indexes table `location_state`
--
ALTER TABLE `location_state`
ADD PRIMARY KEY (`id`);
--
-- Indexes table `order`
--
ALTER TABLE `order`
ADD PRIMARY KEY (`id`),
ADD KEY `client_id` (`user_id`),
ADD KEY `status` (`status`),
ADD KEY `shipping_address_id` (`shipping_address_id`),
ADD KEY `preferences` (`preferences`),
ADD KEY `processing_status` (`processing_status`),
ADD KEY `date_created` (`date_created`),
ADD KEY `delivery_date` (`delivery_date`),
ADD KEY `pickup_address_id` (`pickup_address_id`),
ADD KEY `processing_status_edited` (`processing_status_edited`),
ADD KEY `retry_payment_sms` (`retry_payment_sms`),
ADD KEY `retry_payment` (`retry_payment`),
ADD KEY `retry_payment_email` (`retry_payment_email`),
ADD KEY `has_topup` (`has_topup`),
ADD KEY `is_topup_only` (`is_topup_only`),
ADD KEY `friday_address_id` (`topup_address_id`),
ADD KEY `code_id` (`code_id`),
ADD KEY `user_id` (`user_id`);
--
-- Indexes table `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `password_reset_token` (`password_reset_token`),
ADD KEY `level_id` (`level_id`),
ADD KEY `eway_id` (`eway_id`),
ADD KEY `password_hash` (`password_hash`),
ADD KEY `auth_key` (`auth_key`),
ADD KEY `email` (`email`),
ADD KEY `period` (`period`),
ADD KEY `preferences` (`preferences`),
ADD KEY `is_pick_up` (`is_pick_up`),
ADD KEY `is_active` (`is_active`),
ADD KEY `date_created` (`date_created`),
ADD KEY `phone` (`phone`) USING BTREE,
ADD KEY `active_campaign_id` (`active_campaign_id`),
ADD KEY `custom_ref_code` (`custom_ref_code`),
ADD KEY `consecutive_orders` (`consecutive_orders`),
ADD KEY `pickup_address_id` (`pickup_address_id`),
ADD KEY `rest_referral_discount` (`rest_referral_discount`),
ADD KEY `rest_code_discount` (`rest_code_discount`),
ADD KEY `rest_code_box_discount` (`rest_code_box_discount`),
ADD KEY `rest_code_mp_discount` (`rest_code_mp_discount`),
ADD KEY `delivery_area_id` (`delivery_area_id`),
ADD KEY `rest_code_tup_discount` (`rest_code_tup_discount`),
ADD KEY `orders_count` (`orders_count`),
ADD KEY `alt_delivery_address_id` (`alt_address_id`),
ADD KEY `alt_delivery_area_id` (`alt_delivery_area_id`),
ADD KEY `address_id` (`address_id`),
ADD KEY `code_id` (`code_id`),
ADD KEY `status` (`status`),
ADD KEY `type` (`type`),
ADD KEY `last_name` (`last_name`);
--
-- Indexes table `user_level`
--
ALTER TABLE `user_level`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT
--
ALTER TABLE `address`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `address_pickup`
MODIFY `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `code`
MODIFY `id` smallint(6) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `delivery_area`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `delivery_area_window`
MODIFY `id` tinyint(4) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `gift`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `location`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `location_state`
MODIFY `id` tinyint(4) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `order`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `user`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `user_level`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
For table order:
INDEX(status, code_id, order_id)
INDEX(status, order_id, code_id)
(I can't tell which would be better; the Optimizer can decide.)
Don't use LEFT JOIN when the 'right' table is not optional. It makes it tedious for a human (and the Optimizer) to figure the intent.
User needs
INDEX(type, status, code_id, code, id, last_name)
It is almost always beneficial to DROP INDEX(a) when you ADD INDEX(a,b). I bring that up because you probably have some one-column indexes.
If you need further help, please provide SHOW CREATE TABLE. I suspect that the query could be turned inside-out to great benefit. This involves finding the 15 ids first; then after that do all the JOINs.
That might have this as the first "derived" table since it generates only 15 rows, not the 9976 estimated in the Explain:
FROM ( SELECT id, `code`, code_id, last_name
FROM user
WHERE `type` = 0
AND `status` = 10
GROUP BY `id`
ORDER BY `last_name`
LIMIT 15
) AS u
JOIN ...

mysql 'slow queries log' return a huge number of rows examined but explain seems ok

The sql query
# Query_time: 16.536276 Lock_time: 0.000159 Rows_sent: 756 Rows_examined: 8392194
SET timestamp=1555422526;
SELECT c.id AS c__id, c.company_id AS c__company_id,
c.ordinary_price AS c__ordinary_price, c.nights AS c__nights,
c.food_type AS c__food_type, c.period AS c__period,
c.period_desc AS c__period_desc, c.extra AS c__extra,
c.coupons_bought AS c__coupons_bought, c.coupon_price AS c__coupon_price,
c.coordinates AS c__coordinates, c.best_price AS c__best_price,
c.from_price AS c__from_price, c.end_datetime AS c__end_datetime,
c.hide_clock AS c__hide_clock, c.hide_discount AS c__hide_discount,
c.booking_hotel_id AS c__booking_hotel_id, c.title AS c__title,
c.option_people AS c__option_people, c.option_room AS c__option_room,
c.option_period AS c__option_period, c.city AS c__city,
c2.id AS c2__id, c2.people AS c2__people, c2.room AS c2__room,
( SELECT c8.url AS c8__url
FROM campaign_images c8
WHERE (c8.campaign_id = c.id
AND c8.photo_type = 'list')
ORDER BY c8.ordering ASC LIMIT 1
) AS c__0
FROM campaign c
LEFT JOIN campaign_options c2 ON c.id = c2.campaign_id
AND (c2.active = 1)
LEFT JOIN city_in_campaign c3 ON c.id = c3.campaign_id
LEFT JOIN city c4 ON c3.city_id = c4.id
LEFT JOIN company c5 ON c.company_id = c5.id
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
LEFT JOIN campaign_in_group c7 ON c.id = c7.campaign_id
WHERE c.id IN ('13308', '13281', '13265')
AND (c.status IN ('published')
AND c.start_datetime <= NOW()
AND c.end_datetime >= NOW()
AND c5.id = '2111'
AND c.id != '14624'
AND (c7.group_id in (1)
OR c7.group_id is NULL
)
)
ORDER BY c.coupon_expire_datetime ASC;
create table campaign
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`source_id` int(11) DEFAULT NULL,
`secondary_source_id` int(11) DEFAULT NULL,
`source_hotel_id` int(11) DEFAULT NULL,
`booking_hotel_id` int(11) DEFAULT NULL,
`booking_board_id` int(11) DEFAULT NULL,
`booking_rate` varchar(100) DEFAULT NULL,
`ordinary_price` decimal(9,2) NOT NULL,
`best_price` tinyint(1) NOT NULL DEFAULT '0',
`from_price` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(10) DEFAULT 'draft',
`type` varchar(20) DEFAULT 'other',
`deal_status` varchar(20) DEFAULT 'in_progress',
`coupon_price` decimal(9,2) NOT NULL,
`cosmote_discount` decimal(5,2) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '0',
`period` varchar(255) DEFAULT NULL,
`period_desc` varchar(1000) DEFAULT NULL,
`hide_period` tinyint(1) NOT NULL DEFAULT '0',
`food_type` varchar(100) DEFAULT NULL,
`stars` varchar(100) DEFAULT NULL,
`adults` tinyint(1) DEFAULT NULL,
`childs` tinyint(1) DEFAULT NULL,
`extra` varchar(255) DEFAULT NULL,
`best_point` varchar(511) DEFAULT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_desc` varchar(500) DEFAULT NULL,
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`family_packages_desc` varchar(1000) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`start_datetime` datetime DEFAULT NULL,
`end_datetime` datetime DEFAULT NULL,
`coupon_expire_datetime` datetime DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`city` varchar(255) DEFAULT NULL,
`min_coupons` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(4) NOT NULL DEFAULT '0',
`hide_clock` tinyint(1) NOT NULL DEFAULT '0',
`hide_discount` tinyint(1) NOT NULL DEFAULT '0',
`hide_purchases` tinyint(1) NOT NULL DEFAULT '0',
`booking_enabled` tinyint(1) NOT NULL DEFAULT '0',
`booking_phone` varchar(50) DEFAULT NULL,
`refresh` tinyint(1) NOT NULL DEFAULT '0',
`installments` tinyint(1) NOT NULL DEFAULT '1',
`receipt` enum('0','1') NOT NULL DEFAULT '0',
`newsletters_sent` tinyint(4) NOT NULL DEFAULT '0',
`max_coupons` int(11) NOT NULL DEFAULT '0',
`max_coupons_per_user` int(11) DEFAULT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`fake_orders` int(11) DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`newsletter_title` varchar(500) DEFAULT NULL,
`linkwise_title` varchar(500) DEFAULT NULL,
`option_title` varchar(255) DEFAULT NULL,
`option_title_en` varchar(255) DEFAULT NULL,
`option_people` varchar(150) DEFAULT NULL,
`option_room` varchar(150) DEFAULT NULL,
`option_period` varchar(150) DEFAULT NULL,
`name` varchar(1200) DEFAULT NULL,
`description` text,
`highlights` text,
`coupon_instructions` text,
`show_in_recent_deals` tinyint(4) NOT NULL DEFAULT '1',
`youtube_video_id` varchar(100) DEFAULT NULL,
`in_side` tinyint(4) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`send_newsletter` tinyint(4) NOT NULL DEFAULT '1',
`resend_newsletter` tinyint(4) NOT NULL DEFAULT '0',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`ordering` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `company_id_idx` (`company_id`),
KEY `user_id_idx` (`user_id`),
KEY `status_indx` (`status`),
KEY `str_dt_indx` (`start_datetime`),
KEY `end_dt_indx` (`end_datetime`),
KEY `side_indx` (`in_side`),
KEY `ord_indx` (`ordering`),
KEY `global_indx` (`is_global`),
KEY `act_indx` (`active`),
KEY `coup_expr_index` (`coupon_expire_datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=14788 DEFAULT CHARSET=utf8
create table campaign_options
CREATE TABLE `campaign_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`people` varchar(100) DEFAULT NULL,
`room` varchar(100) DEFAULT NULL,
`food` varchar(100) DEFAULT NULL,
`period` varchar(100) DEFAULT NULL,
`coupon_price` decimal(9,2) NOT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) DEFAULT '0.00',
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`main` tinyint(1) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `active_indx` (`active`),
KEY `family_indx` (`family`),
KEY `main_indx` (`main`)
) ENGINE=InnoDB AUTO_INCREMENT=48990 DEFAULT CHARSET=utf8
create table city_in_campaign
CREATE TABLE `city_in_campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `city_id_idx` (`city_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=227176 DEFAULT CHARSET=utf8
create table city
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`is_active` tinyint(3) unsigned DEFAULT NULL,
`modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `country_id_idx` (`country_id`),
KEY `is_active_indx` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8
create table company
CREATE TABLE `company` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`ref_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`description` text,
`logo` varchar(255) DEFAULT NULL,
`phone_number` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
`skype_name` varchar(50) DEFAULT NULL,
`icq_number` varchar(255) DEFAULT NULL,
`payment_information` text,
`extra1` text,
`extra2` text,
`extra3` text,
`video` varchar(500) DEFAULT NULL,
`checked` tinyint(1) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `checked_indx` (`checked`),
KEY `ordering_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=2519 DEFAULT CHARSET=utf8
create table campaign_in_category
CREATE TABLE `campaign_in_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `category_id_idx` (`category_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=457080 DEFAULT CHARSET=utf8
create table campaign_in_group
CREATE TABLE `campaign_in_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`created_datetime` datetime DEFAULT NULL,
`modified_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `camp_group_indx` (`campaign_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1175 DEFAULT CHARSET=utf8
campaign total rows: 12,657
campaign_options total rows: 43,714
city_in_campaign total rows: 15,162
city total rows: 215
company total rows: 1,756
campaign_in_category total rows: 38,817
campaign_in_group total rows: 395
explain of the query
which ones have index
it looks you need composite index because file sorting.
usage:
CREATE INDEX index_name
ON table_name(c2,c3,c4);
You are getting above result because some time o/p of explain gives different result as response of query of prod.
For optimization of query you can't have a fix answer. it varies from case to case. As for this situation I think you need to perform performance testing of this query. You can do this by inserting some records in all referenced table and then checking it's performance using explain.
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
seems to be totally useless. But the Optimizer may not realize it. Remove it and any other dead code.

osticket how to get ticket id by the form entry value?

In osticket we have a number of custom form fields setup and I need help querying them. The data that I want to query is stored in form_entry.value.
SELECT * FROM `form_entry_values` fev, `form_entry` fe WHERE fev.value = '{$ibn}' AND fe.id = fev.entry_id
This is returning the correct data but how can I find the ticket ID if i know the form_id? I can't seem to find any joining tables.
This is the full db: https://github.com/osTicket/osTicket-1.8/blob/b1c845bf0591b1f5da593a55e462b07e5a4ee5de/setup/inc/streams/core/install-mysql.sql
DROP TABLE IF EXISTS `form`;
CREATE TABLE `form` (
`id` int(11) unsigned NOT NULL auto_increment,
`type` varchar(8) NOT NULL DEFAULT 'G',
`deletable` tinyint(1) NOT NULL DEFAULT 1,
`title` varchar(255) NOT NULL,
`instructions` varchar(512),
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_field`;
CREATE TABLE `form_field` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`type` varchar(255) NOT NULL DEFAULT 'text',
`label` varchar(255) NOT NULL,
`required` tinyint(1) NOT NULL DEFAULT 0,
`private` tinyint(1) NOT NULL DEFAULT 0,
`edit_mask` tinyint(1) NOT NULL DEFAULT 0,
`name` varchar(64) NOT NULL,
`configuration` text,
`sort` int(11) unsigned NOT NULL,
`hint` varchar(512),
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry`;
CREATE TABLE `form_entry` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`object_id` int(11) unsigned,
`object_type` char(1) NOT NULL DEFAULT 'T',
`sort` int(11) unsigned NOT NULL DEFAULT 1,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `entry_lookup` (`object_type`, `object_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry_values`;
CREATE TABLE `form_entry_values` (
-- references form_entry.id
`entry_id` int(11) unsigned NOT NULL,
`field_id` int(11) unsigned NOT NULL,
`value` text,
`value_id` int(11),
PRIMARY KEY (`entry_id`, `field_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list`;
CREATE TABLE `list` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`name_plural` varchar(255),
`sort_mode` enum('Alpha', '-Alpha', 'SortCol') NOT NULL DEFAULT 'Alpha',
`masks` int(11) unsigned NOT NULL DEFAULT 0,
`type` VARCHAR( 16 ) NULL DEFAULT NULL,
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list_items`;
CREATE TABLE `list_items` (
`id` int(11) unsigned NOT NULL auto_increment,
`list_id` int(11),
`status` int(11) unsigned NOT NULL DEFAULT 1,
`value` varchar(255) NOT NULL,
-- extra value such as abbreviation
`extra` varchar(255),
`sort` int(11) NOT NULL DEFAULT 1,
`properties` text,
PRIMARY KEY (`id`),
KEY `list_item_lookup` (`list_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket`;
CREATE TABLE `ticket` (
`ticket_id` int(11) unsigned NOT NULL auto_increment,
`number` varchar(20),
`user_id` int(11) unsigned NOT NULL default '0',
`user_email_id` int(11) unsigned NOT NULL default '0',
`status_id` int(10) unsigned NOT NULL default '0',
`dept_id` int(10) unsigned NOT NULL default '0',
`sla_id` int(10) unsigned NOT NULL default '0',
`topic_id` int(10) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`team_id` int(10) unsigned NOT NULL default '0',
`email_id` int(11) unsigned NOT NULL default '0',
`flags` int(10) unsigned NOT NULL default '0',
`ip_address` varchar(64) NOT NULL default '',
`source` enum('Web','Email','Phone','API','Other') NOT NULL default 'Other',
`isoverdue` tinyint(1) unsigned NOT NULL default '0',
`isanswered` tinyint(1) unsigned NOT NULL default '0',
`duedate` datetime default NULL,
`reopened` datetime default NULL,
`closed` datetime default NULL,
`lastmessage` datetime default NULL,
`lastresponse` datetime default NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`ticket_id`),
KEY `user_id` (`user_id`),
KEY `dept_id` (`dept_id`),
KEY `staff_id` (`staff_id`),
KEY `team_id` (`team_id`),
KEY `status_id` (`status_id`),
KEY `created` (`created`),
KEY `closed` (`closed`),
KEY `duedate` (`duedate`),
KEY `topic_id` (`topic_id`),
KEY `sla_id` (`sla_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_attachment`;
CREATE TABLE `ticket_attachment` (
`attach_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`file_id` int(10) unsigned NOT NULL default '0',
`ref_id` int(11) unsigned NOT NULL default '0',
`inline` tinyint(1) NOT NULL default '0',
`created` datetime NOT NULL,
PRIMARY KEY (`attach_id`),
KEY `ticket_id` (`ticket_id`),
KEY `ref_id` (`ref_id`),
KEY `file_id` (`file_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_lock`;
CREATE TABLE `ticket_lock` (
`lock_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`expire` datetime default NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`lock_id`),
UNIQUE KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_email_info`;
CREATE TABLE `ticket_email_info` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_id` int(11) unsigned NOT NULL,
`email_mid` varchar(255) NOT NULL,
`headers` text,
PRIMARY KEY (`id`),
KEY `email_mid` (`email_mid`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_event`;
CREATE TABLE `ticket_event` (
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL,
`team_id` int(11) unsigned NOT NULL,
`dept_id` int(11) unsigned NOT NULL,
`topic_id` int(11) unsigned NOT NULL,
`state` enum('created','closed','reopened','assigned','transferred','overdue') NOT NULL,
`staff` varchar(255) NOT NULL default 'SYSTEM',
`annulled` tinyint(1) unsigned NOT NULL default '0',
`timestamp` datetime NOT NULL,
KEY `ticket_state` (`ticket_id`, `state`, `timestamp`),
KEY `ticket_stats` (`timestamp`, `state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_status`;
CREATE TABLE IF NOT EXISTS `ticket_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL DEFAULT '',
`state` varchar(16) DEFAULT NULL,
`mode` int(11) unsigned NOT NULL DEFAULT '0',
`flags` int(11) unsigned NOT NULL DEFAULT '0',
`sort` int(11) unsigned NOT NULL DEFAULT '0',
`properties` text NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `state` (`state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_priority`;
CREATE TABLE `ticket_priority` (
`priority_id` tinyint(4) NOT NULL auto_increment,
`priority` varchar(60) NOT NULL default '',
`priority_desc` varchar(30) NOT NULL default '',
`priority_color` varchar(7) NOT NULL default '',
`priority_urgency` tinyint(1) unsigned NOT NULL default '0',
`ispublic` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`priority_id`),
UNIQUE KEY `priority` (`priority`),
KEY `priority_urgency` (`priority_urgency`),
KEY `ispublic` (`ispublic`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_thread`;
CREATE TABLE `ticket_thread` (
`id` int(11) unsigned NOT NULL auto_increment,
`pid` int(11) unsigned NOT NULL default '0',
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL default '0',
`user_id` int(11) unsigned not null default 0,
`thread_type` enum('M','R','N') NOT NULL,
`poster` varchar(128) NOT NULL default '',
`source` varchar(32) NOT NULL default '',
`title` varchar(255),
`body` mediumtext NOT NULL,
`format` varchar(16) NOT NULL default 'html',
`ip_address` varchar(64) NOT NULL default '',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`),
KEY `pid` (`pid`)
) DEFAULT CHARSET=utf8;
CREATE TABLE `ticket_collaborator` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`isactive` tinyint(1) NOT NULL DEFAULT '1',
`ticket_id` int(11) unsigned NOT NULL DEFAULT '0',
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
-- M => (message) clients, N => (note) 3rd-Party, R => (reply) external authority
`role` char(1) NOT NULL DEFAULT 'M',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `collab` (`ticket_id`,`user_id`)
) DEFAULT CHARSET=utf8;
In the values table, there is a field object_id this is the ticket ID.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON(fev.entry_id = fe.id)
LEFT JOIN ticket t ON(fe.object_id = t.ticket_id)
WHERE fev.value = '123'
in the Osticket 1.15 version, the object_id in the form_entry has a relation to the user_id.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON fev.entry_id = fe.id
LEFT JOIN ticket t ON fe.object_id = t.user_id

Indexes in Mysql table

I have such tables:
CREATE TABLE `skadate_newsfeed_action` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`entityId` int(11) NOT NULL,
`entityType` varchar(100) NOT NULL,
`feature` varchar(100) NOT NULL,
`data` longtext NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'active',
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`visibility` int(11) NOT NULL,
`privacy` enum('everybody','friends_only') NOT NULL DEFAULT 'everybody',
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `privacy` (`visibility`),
KEY `updateTime` (`updateTime`),
KEY `entity` (`entityType`,`entityId`)
) ENGINE=MyISAM;
CREATE TABLE `skadate_profile` (
`profile_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(40) NOT NULL,
`sex` bigint(20) DEFAULT NULL,
`match_sex` bigint(20) DEFAULT NULL,
`birthdate` date NOT NULL DEFAULT '0000-00-00',
`headline` varchar(128) DEFAULT '',
`general_description` text,
`match_agerange` varchar(6) DEFAULT NULL,
`custom_location` varchar(255) DEFAULT NULL,
`country_id` char(2) NOT NULL DEFAULT '',
`zip` varchar(10) DEFAULT NULL,
`state_id` varchar(5) DEFAULT NULL,
`city_id` int(11) DEFAULT '0',
`join_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`activity_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`membership_type_id` int(10) unsigned NOT NULL DEFAULT '18',
`affiliate_id` int(8) unsigned NOT NULL DEFAULT '0',
`email_verified` enum('undefined','yes','no') NOT NULL DEFAULT 'undefined',
`reviewed` enum('n','y') NOT NULL DEFAULT 'n',
`has_photo` enum('n','y') NOT NULL DEFAULT 'n',
`has_media` enum('n','y') NOT NULL DEFAULT 'n',
`status` enum('active','on_hold','suspended') NOT NULL DEFAULT 'active',
`featured` enum('n','y') NOT NULL DEFAULT 'n',
`register_invite_score` tinyint(3) NOT NULL DEFAULT '0',
`rate_score` tinyint(3) unsigned NOT NULL DEFAULT '0',
`rates` bigint(20) unsigned NOT NULL DEFAULT '0',
`language_id` int(10) unsigned NOT NULL DEFAULT '0',
`join_ip` int(11) unsigned NOT NULL DEFAULT '0',
`neigh_location` enum('country','state','city','zip') DEFAULT NULL,
`neigh_location_distance` int(10) unsigned NOT NULL DEFAULT '0',
`bg_color` varchar(32) DEFAULT NULL,
`bg_image` varchar(32) DEFAULT NULL,
`bg_image_url` varchar(255) DEFAULT NULL,
`bg_image_mode` tinyint(1) DEFAULT NULL,
`bg_image_status` enum('active','approval') NOT NULL DEFAULT 'active',
`has_music` enum('n','y') NOT NULL DEFAULT 'n',
`is_private` tinyint(1) NOT NULL DEFAULT '0',
`subscription_id_offerit` text,
PRIMARY KEY (`profile_id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `membership_id` (`membership_type_id`),
KEY `zip` (`zip`),
KEY `country_id` (`country_id`),
KEY `state_id` (`state_id`),
KEY `city_id` (`city_id`),
KEY `sex` (`sex`),
KEY `match_sex` (`match_sex`),
KEY `activity_stamp` (`activity_stamp`),
KEY `join_stamp` (`join_stamp`),
KEY `birthdate` (`birthdate`),
KEY `featured` (`featured`,`has_photo`,`activity_stamp`)
) ENGINE=MyISAM;
And try to perform this query:
SELECT DISTINCT `na`.*
FROM `skadate_newsfeed_action` AS `na`
LEFT JOIN `skadate_profile` AS `profile` ON ( `na`.`userId` = `profile`.`profile_id` )
WHERE ( profile.email_verified='yes' OR profile.email_verified='no' OR profile.email_verified='undefined' )
AND `profile`.`status`='active' AND `na`.`status`='active' AND `na`.`privacy`='everybody'
AND `na`.`visibility` & 1 AND `na`.`updateTime` < 1455885224
ORDER BY `na`.`updateTime` DESC, `na`.`id` DESC
LIMIT 0, 10
But when I see EXPLAIN:
Maybe someone can help me, how I can improve this query?
If you want records from only one table, then use exists rather than a join and select distinct. So:
SELECT na.*
FROM `skadate_newsfeed_action` na
WHERE EXISTS (SELECT 1
FROM skadate_profile p
WHERE na.userId = p.profile_id AND
p.email_verified IN ('yes', 'no', 'undefined') AND
p.status = 'active'
) AND
na.status = 'active' AND
na.privacy = 'everybody' AND
na.visibility & 1 > 0 AND
na.updateTime < 1455885224
ORDER BY na.`updateTime` DESC, na.`id` DESC
LIMIT 0, 10;
For this query, you want an index on skadate_profile(profile_id, status, verified). Also, the following index is probably helpful: skadate_newsfeed_action(status, privacy, updateTime, visibility, userId).
This is probably because of the DISTICT keyword. To remove duplicates MySQL needs to sort the result by every selected column.

How to improve a MYSQL Query with multiple JOINS

I have 2 search pages that are super slow in getting results.
I did not write the queries, but I know they are not written in an efficient way; I just do not have enough practice in MYSQL to figure out how to make them more efficient.
What should I do to improve the following query?
SELECT DISTINCT m.id AS memberID , m.login , m.age , p.gender
, p.name AS header , p.id AS profileID , p.city , p.state , p.lastlogin
, o.login AS online , c.name AS country , ph.filename_1 AS pic
FROM dt_members AS m
INNER JOIN dt_profile_approved AS p ON m.id=p.member_id
LEFT JOIN dt_privacy AS pv ON m.id=pv.member_id
INNER JOIN dt_countries AS c ON c.id=p.country
LEFT JOIN dt_photos AS ph ON m.id=ph.member_id
LEFT JOIN dt_usersonline AS o ON m.login=o.login
WHERE p.status=1 AND (pv.unsearchable IS NULL OR pv.unsearchable='')
AND p.gender='Female' AND m.age BETWEEN 25 AND 40
ORDER BY p.lastlogin DESC
LIMIT 0, 21;
it is terribly slow and showing 500 error often.
Output of explain:
Show Create Table:
CREATE TABLE `dt_members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(25) DEFAULT NULL,
`pswd` varchar(20) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`looking_for` varchar(255) DEFAULT NULL,
`ip_addr` varchar(15) DEFAULT NULL,
`reg_date` int(11) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`system_status` int(11) DEFAULT '0',
`system_status_end` int(11) DEFAULT NULL,
`unlimited` int(11) DEFAULT '0',
`unlimited_end` int(11) DEFAULT NULL,
`matchfinder` int(1) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `login` (`login`),
KEY `pswd` (`pswd`),
KEY `email` (`email`),
KEY `name` (`name`),
KEY `gender` (`gender`),
KEY `age` (`age`),
KEY `country` (`country`),
KEY `looking_for` (`looking_for`),
KEY `ip_addr` (`ip_addr`),
KEY `reg_date` (`reg_date`),
KEY `status` (`status`),
KEY `system_status` (`system_status`),
KEY `system_status_end` (`system_status_end`),
KEY `unlimited` (`unlimited`),
KEY `unlimited_end` (`unlimited_end`),
KEY `matchfinder` (`matchfinder`)
) ENGINE=MyISAM AUTO_INCREMENT=29150 DEFAULT CHARSET=latin1
CREATE TABLE `dt_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) DEFAULT NULL,
`country` int(11) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`gender` varchar(20) DEFAULT NULL,
`birth_day` int(11) DEFAULT NULL,
`birth_month` varchar(6) DEFAULT NULL,
`birth_year` int(11) DEFAULT NULL,
`marital_status` int(11) DEFAULT NULL,
`children` int(11) DEFAULT NULL,
`drinking` int(11) DEFAULT NULL,
`smoking` int(11) DEFAULT NULL,
`food` int(11) DEFAULT NULL,
`eye_color` int(11) DEFAULT NULL,
`hair_color` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL,
`body_type` int(11) DEFAULT NULL,
`race` int(11) DEFAULT NULL,
`religion` int(11) DEFAULT NULL,
`occupation` int(11) DEFAULT NULL,
`education` int(11) DEFAULT NULL,
`lang_1` int(11) DEFAULT NULL,
`lang_1_rate` int(11) DEFAULT NULL,
`lang_2` int(11) DEFAULT NULL,
`lang_2_rate` int(11) DEFAULT NULL,
`lang_3` int(11) DEFAULT NULL,
`lang_3_rate` int(11) DEFAULT NULL,
`lang_4` int(11) DEFAULT NULL,
`lang_4_rate` int(11) DEFAULT NULL,
`looking_for` varchar(10) DEFAULT NULL,
`age_from` int(11) DEFAULT NULL,
`age_to` int(11) DEFAULT NULL,
`general_info` text,
`appearance_info` text,
`looking_for_info` text,
`status` int(11) DEFAULT NULL,
`finish_status` int(11) DEFAULT NULL,
`not_newbie` int(11) DEFAULT NULL,
`lastlogin` int(10) NOT NULL DEFAULT '0',
`zipcode` varchar(5) NOT NULL DEFAULT '',
`longitude` double DEFAULT NULL,
`latitude` double DEFAULT NULL,
`photo_pass` varchar(25) NOT NULL DEFAULT '',
`view_count` int(11) DEFAULT '0',
`wants_kids` int(11) DEFAULT NULL,
`kids_okay` int(11) DEFAULT NULL,
`relocate_domestic` int(11) DEFAULT NULL,
`relocate_international` int(11) DEFAULT NULL,
`pioneer` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `member_id` (`member_id`),
KEY `country` (`country`),
KEY `state` (`state`),
KEY `city` (`city`),
KEY `email` (`email`),
KEY `name` (`name`),
KEY `gender` (`gender`),
KEY `birth_day` (`birth_day`),
KEY `birth_month` (`birth_month`),
KEY `birth_year` (`birth_year`),
KEY `marital_status` (`marital_status`),
KEY `children` (`children`),
KEY `drinking` (`drinking`),
KEY `smoking` (`smoking`),
KEY `food` (`food`),
KEY `eye_color` (`eye_color`),
KEY `hair_color` (`hair_color`),
KEY `height` (`height`),
KEY `body_type` (`body_type`),
KEY `race` (`race`),
KEY `religion` (`religion`),
KEY `occupation` (`occupation`),
KEY `education` (`education`),
KEY `lang_1` (`lang_1`),
KEY `lang_1_rate` (`lang_1_rate`),
KEY `lang_2` (`lang_2`),
KEY `lang_2_rate` (`lang_2_rate`),
KEY `lang_3` (`lang_3`),
KEY `lang_3_rate` (`lang_3_rate`),
KEY `lang_4` (`lang_4`),
KEY `lang_4_rate` (`lang_4_rate`),
KEY `looking_for` (`looking_for`),
KEY `age_from` (`age_from`),
KEY `age_to` (`age_to`),
KEY `status` (`status`),
KEY `finish_status` (`finish_status`),
KEY `not_newbie` (`not_newbie`),
KEY `lastlogin` (`lastlogin`),
KEY `zipcode` (`zipcode`),
KEY `longitude` (`longitude`),
KEY `latitude` (`latitude`),
KEY `photo_pass` (`photo_pass`),
KEY `view_count` (`view_count`),
KEY `wants_kids` (`wants_kids`),
KEY `kids_okay` (`kids_okay`),
KEY `relocate_domestic` (`relocate_domestic`),
KEY `relocate_international` (`relocate_international`),
KEY `pioneer` (`pioneer`)
) ENGINE=MyISAM AUTO_INCREMENT=18389 DEFAULT CHARSET=latin1
CREATE TABLE `dt_privacy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) DEFAULT NULL,
`online_yn` char(1) DEFAULT NULL,
`vkiss_yn` char(1) DEFAULT NULL,
`profiles_yn` char(1) DEFAULT NULL,
`IM_yn` char(1) DEFAULT NULL,
`featured_yn` char(1) DEFAULT NULL,
`HL_messaged_yn` char(1) DEFAULT NULL,
`HL_im_yn` char(1) DEFAULT NULL,
`HL_viewed_yn` char(1) DEFAULT NULL,
`HL_kissed_yn` char(1) DEFAULT NULL,
`HL_favorite_yn` char(1) DEFAULT NULL,
`unsearchable` char(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `member_id` (`member_id`),
KEY `online_yn` (`online_yn`),
KEY `vkiss_yn` (`vkiss_yn`),
KEY `profiles_yn` (`profiles_yn`),
KEY `IM_yn` (`IM_yn`),
KEY `featured_yn` (`featured_yn`),
KEY `HL_messaged_yn` (`HL_messaged_yn`),
KEY `HL_im_yn` (`HL_im_yn`),
KEY `HL_viewed_yn` (`HL_viewed_yn`),
KEY `HL_kissed_yn` (`HL_kissed_yn`),
KEY `HL_favorite_yn` (`HL_favorite_yn`),
KEY `unsearchable` (`unsearchable`)
) ENGINE=MyISAM AUTO_INCREMENT=26305 DEFAULT CHARSET=latin1
CREATE TABLE `dt_countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=226 DEFAULT CHARSET=latin1
CREATE TABLE `dt_photos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` varchar(255) DEFAULT NULL,
`filename_1` varchar(255) DEFAULT NULL,
`filename_2` varchar(255) DEFAULT NULL,
`filename_3` varchar(255) NOT NULL DEFAULT '',
`filename_4` varchar(255) NOT NULL DEFAULT '',
`filename_5` varchar(255) NOT NULL DEFAULT '',
`filename_6` varchar(255) NOT NULL DEFAULT '',
`filename_7` varchar(255) NOT NULL DEFAULT '',
`filename_8` varchar(255) NOT NULL DEFAULT '',
`filename_9` varchar(255) NOT NULL DEFAULT '',
`filename_10` varchar(255) NOT NULL DEFAULT '',
`filename_11` varchar(255) NOT NULL DEFAULT '',
`filename_12` varchar(255) NOT NULL DEFAULT '',
`filename_13` varchar(255) NOT NULL DEFAULT '',
`filename_14` varchar(255) NOT NULL DEFAULT '',
`filename_15` varchar(255) NOT NULL DEFAULT '',
`filename_16` varchar(255) NOT NULL DEFAULT '',
`filename_17` varchar(255) NOT NULL DEFAULT '',
`filename_18` varchar(255) NOT NULL DEFAULT '',
`filename_19` varchar(255) NOT NULL DEFAULT '',
`filename_20` varchar(255) NOT NULL DEFAULT '',
`private_1` tinyint(1) NOT NULL DEFAULT '0',
`private_2` tinyint(1) NOT NULL DEFAULT '0',
`private_3` tinyint(1) NOT NULL DEFAULT '0',
`private_4` tinyint(1) NOT NULL DEFAULT '0',
`private_5` tinyint(1) NOT NULL DEFAULT '0',
`private_6` tinyint(1) NOT NULL DEFAULT '0',
`private_7` tinyint(1) NOT NULL DEFAULT '0',
`private_8` tinyint(1) NOT NULL DEFAULT '0',
`private_9` tinyint(1) NOT NULL DEFAULT '0',
`private_10` tinyint(1) NOT NULL DEFAULT '0',
`private_11` tinyint(1) NOT NULL DEFAULT '0',
`private_12` tinyint(1) NOT NULL DEFAULT '0',
`private_13` tinyint(1) NOT NULL DEFAULT '0',
`private_14` tinyint(1) NOT NULL DEFAULT '0',
`private_15` tinyint(1) NOT NULL DEFAULT '0',
`private_16` tinyint(1) NOT NULL DEFAULT '0',
`private_17` tinyint(1) NOT NULL DEFAULT '0',
`private_18` tinyint(1) NOT NULL DEFAULT '0',
`private_19` tinyint(1) NOT NULL DEFAULT '0',
`private_20` tinyint(1) NOT NULL DEFAULT '0',
`password` varchar(255) NOT NULL DEFAULT '',
`description_1` varchar(255) DEFAULT NULL,
`description_2` varchar(255) DEFAULT NULL,
`description_3` varchar(255) NOT NULL DEFAULT '',
`description_4` varchar(255) NOT NULL DEFAULT '',
`description_5` varchar(255) NOT NULL DEFAULT '',
`description_6` varchar(255) NOT NULL DEFAULT '',
`description_7` varchar(255) NOT NULL DEFAULT '',
`description_8` varchar(255) NOT NULL DEFAULT '',
`description_9` varchar(255) NOT NULL DEFAULT '',
`description_10` varchar(255) NOT NULL DEFAULT '',
`description_11` varchar(255) NOT NULL DEFAULT '',
`description_12` varchar(255) NOT NULL DEFAULT '',
`description_13` varchar(255) NOT NULL DEFAULT '',
`description_14` varchar(255) NOT NULL DEFAULT '',
`description_15` varchar(255) NOT NULL DEFAULT '',
`description_16` varchar(255) NOT NULL DEFAULT '',
`description_17` varchar(255) NOT NULL DEFAULT '',
`description_18` varchar(255) NOT NULL DEFAULT '',
`description_19` varchar(255) NOT NULL DEFAULT '',
`description_20` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `filename_12` (`filename_12`),
KEY `filename_13` (`filename_13`),
KEY `filename_14` (`filename_14`),
KEY `filename_15` (`filename_15`),
KEY `filename_16` (`filename_16`),
KEY `filename_17` (`filename_17`),
KEY `filename_18` (`filename_18`),
KEY `filename_19` (`filename_19`),
KEY `filename_20` (`filename_20`),
KEY `member_id` (`member_id`),
KEY `filename_1` (`filename_1`),
KEY `filename_2` (`filename_2`),
KEY `filename_3` (`filename_3`),
KEY `filename_4` (`filename_4`),
KEY `filename_5` (`filename_5`),
KEY `filename_6` (`filename_6`),
KEY `filename_7` (`filename_7`),
KEY `filename_8` (`filename_8`),
KEY `filename_9` (`filename_9`),
KEY `filename_10` (`filename_10`),
KEY `private_1` (`private_1`),
KEY `private_2` (`private_2`),
KEY `private_3` (`private_3`),
KEY `private_4` (`private_4`),
KEY `private_5` (`private_5`),
KEY `private_6` (`private_6`),
KEY `private_7` (`private_7`),
KEY `private_8` (`private_8`),
KEY `private_9` (`private_9`),
KEY `private_10` (`private_10`),
KEY `private_11` (`private_11`),
KEY `private_12` (`private_12`),
KEY `private_13` (`private_13`),
KEY `private_14` (`private_14`),
KEY `private_15` (`private_15`),
KEY `private_16` (`private_16`),
KEY `private_17` (`private_17`),
KEY `private_18` (`private_18`),
KEY `private_19` (`private_19`),
KEY `private_20` (`private_20`),
KEY `password` (`password`),
KEY `description_1` (`description_1`),
KEY `description_2` (`description_2`),
KEY `description_3` (`description_3`),
KEY `description_4` (`description_4`),
KEY `description_5` (`description_5`),
KEY `description_6` (`description_6`),
KEY `description_7` (`description_7`),
KEY `description_8` (`description_8`),
KEY `description_9` (`description_9`),
KEY `description_10` (`description_10`),
KEY `description_11` (`description_11`),
KEY `description_12` (`description_12`),
KEY `description_13` (`description_13`),
KEY `description_14` (`description_14`),
KEY `description_15` (`description_15`),
KEY `description_16` (`description_16`),
KEY `description_17` (`description_17`),
KEY `description_18` (`description_18`),
KEY `description_19` (`description_19`),
KEY `description_20` (`description_20`),
KEY `filename_10_2` (`filename_10`),
KEY `filename_10_3` (`filename_10`)
) ENGINE=MyISAM AUTO_INCREMENT=11174 DEFAULT CHARSET=latin1
CREATE TABLE `dt_usersonline` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`timestamp` int(15) NOT NULL DEFAULT '0',
`ip` varchar(40) NOT NULL DEFAULT '',
`login` varchar(25) NOT NULL DEFAULT '',
`userid` int(10) NOT NULL DEFAULT '0',
`session_id` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_2` (`id`),
KEY `timestamp` (`timestamp`),
KEY `ip` (`ip`),
KEY `login` (`login`),
KEY `userid` (`userid`),
KEY `session_id` (`session_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4424348 DEFAULT CHARSET=latin1
CREATE TABLE `dt_members` (
`id` int(11) NOT NULL AUTO_INCREMENT,...
CREATE TABLE `dt_photos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` varchar(255) DEFAULT NULL,..
You are joining on your query as:
LEFT JOIN dt_photos AS ph ON m.id=ph.member_id
Notice how every other time you join on member_id to m.id the column looks like member_id int(11) DEFAULT NULL. Your join criteria should be on the same data type, otherwise it looks like it is doing a full table scan. It's not using the key because it believes that it has to scan the varchar field rather than utilizing indexes for on clause.
Try Altering column member_id to that of m.id from dt_members
It is interesting to note, had you actually tried to create a foreign key constraint, it would not have allowed you to because of the mismatched data types...
Warning: MYISAM table will be locked during the duration of the later table process.
Try moving where conditions into the corresponding join condition:
SELECT DISTINCT m.id AS memberID , m.login , m.age , p.gender
, p.name AS header , p.id AS profileID , p.city , p.state , p.lastlogin
, o.login AS online , c.name AS country , ph.filename_1 AS pic
FROM dt_members AS m
INNER JOIN dt_profile_approved AS p ON m.id=p.member_id
AND p.status=1 AND p.gender='Female' -- Moved from WHERE clause
LEFT JOIN dt_privacy AS pv ON m.id=pv.member_id
AND (pv.unsearchable IS NULL OR pv.unsearchable='') -- Moved from WHERE clause
INNER JOIN dt_countries AS c ON c.id=p.country
LEFT JOIN dt_photos AS ph ON m.id=ph.member_id
LEFT JOIN dt_usersonline AS o ON m.login=o.login
WHERE m.age BETWEEN 25 AND 40
ORDER BY p.lastlogin DESC
LIMIT 0, 21;
While where conditions are made after all joins, join conditions are evaluated during the join, so vast numbers of unnecessary joins can be avoided early.
Although theoretically the query optimizer should do this for you, I have found the mysql can be particularly dense in this regard.
It looks like most of the selectivity on your query is on your dt_profile_approved table. Try creating a compound index on (status, gender, lastlogin)
This should allow an index-only selection and ordering.
Your query does the notorious SELECT a lot ORDER BY something DESC LIMIT tinynumber pattern. This is expensive. Try a deferred join. Start by getting the interesting items from your dt_profile_approved table, like this:
SELECT member_id, lastlogin
FROM dt_profile_approved
WHERE status=1
AND gender='Female'
ORDER BY lastlogin DESC
This subquery can be optimized very cleanly with a compound index on (status, gender, lastlogin, member_id). That's called a covering index. It has a great benefit: no extra sorting is required, because the index is already sorted.
Based on the exact query you showed us, the order in the index of gender and status doesn't matter. But I guess you have another query that looks for males, and you may have one that omits that. So status is likely to be the more selective field across all your queries. (Guessing.)
Then, join that subquery into the rest of your query... that will look something like this.
SELECT DISTINCT
m.id AS memberID , m.login , m.age,
p.gender. p.name AS header , p.id AS profileID ,
p.city , p.state , p.lastlogin,
o.login AS online ,
c.name AS country , ph.filename_1 AS pic
FROM (
SELECT member_id, lastlogin
FROM dt_profile_approved
WHERE status=1
AND gender='Female'
ORDER BY lastlogin DESC
) AS sel
INNER JOIN dt_profile_approved AS p ON sel.member_id = p.member_id
INNER JOIN dt_members AS m ON m.id=sel.member_id
LEFT JOIN dt_privacy AS pv ON m.id=pv.member_id
INNER JOIN dt_countries AS c ON c.id=p.country
LEFT JOIN dt_photos AS ph ON m.id=ph.member_id
LEFT JOIN dt_usersonline AS o ON m.login=o.login
WHERE (pv.unsearchable IS NULL OR pv.unsearchable='')
AND m.age BETWEEN 25 AND 40
ORDER BY sel.lastlogin DESC
LIMIT 0, 21;
If this works, it will be because it can limit the labor of sorting using the index.