Mysql表

时间:2016-02-19 12:49:42

标签: mysql sql indexing

我有这样的表:

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;

并尝试执行此查询:

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

但是当我看到EXPLAIN: enter image description here

也许有人可以帮助我,我如何改进this查询?

2 个答案:

答案 0 :(得分:1)

如果您只想要一个表中的记录,请使用exists而不是joinselect distinct。所以:

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;

对于此查询,您需要skadate_profile(profile_id, status, verified)上的索引。此外,以下索引可能有用:skadate_newsfeed_action(status, privacy, updateTime, visibility, userId)

答案 1 :(得分:0)

这可能是因为DISTICT关键字。要删除重复项,MySQL需要按每个选定的列对结果进行排序。