Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql表中的索引_Mysql_Sql_Indexing - Fatal编程技术网

Mysql表中的索引

Mysql表中的索引,mysql,sql,indexing,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 N

我有这样的表格:

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
但当我看到解释时:


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

如果您只需要一个表中的记录,那么使用exists而不是join并选择distinct。因此:


对于这个查询,您需要一个关于skadate_profileprofile_id、status、verified的索引。此外,以下索引可能很有用:skadate\u newsfeed\u actionstatus、privacy、updateTime、visibility、userId。

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

为pair email\u verified and status、profile\u id-PRIMARY创建索引,但MySQL似乎不想使用此索引:请向我解释为什么要加负号,谢谢。你真的需要留下吗?若否,请把它移走;;然后优化器可以选择从profile开始。
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;