我在“收藏夹”表(300万行)和“项目”表(60万行)之间进行连接。
查询的时间从0.3秒到2秒不等,我希望可以对它进行一些优化。
Favorites.faver_profile_id和Items.id被索引。
我没有使用faver_profile_id索引,而是在(faver_profile_id,id)上创建了一个新索引,该索引消除了按id排序时所需的文件排序。不幸的是,此索引根本没有帮助,我可能会将其删除(是的,还有3个小时的停机时间来删除索引…)
关于如何优化这个查询有什么想法吗?
如果有帮助:
Favorite.removed和Item.removed占总时间的“0”98%。
Favorite.collection_id在80%的情况下为空。

SELECT `Item`.`id`, `Item`.`source_image`, `Item`.`cached_image`, `Item`.`source_title`, `Item`.`source_url`, `Item`.`width`, `Item`.`height`, `Item`.`fave_count`, `Item`.`created`
FROM `favorites` AS `Favorite`
LEFT JOIN `items` AS `Item`
ON (`Item`.`removed` = 0 AND `Favorite`.`notice_id` = `Item`.`id`)
WHERE ((`faver_profile_id` = 1) AND (`collection_id` IS NULL) AND (`Favorite`.`removed` = 0) AND (`Item`.`removed` = '0'))
ORDER BY `Favorite`.`id` desc LIMIT 50;

+----+-------------+----------+--------+----------------------------------------------------- ----------+------------------+---------+-----------------------------------------+------+-------------+
| id | select_type | table    | type   | possible_keys                                                 | key              | key_len | ref                                     | rows | Extra       |
+----+-------------+----------+--------+---------------------------------------------------------------+------------------+---------+-----------------------------------------+------+-------------+
|  1 | SIMPLE      | Favorite | ref    | notice_id,faver_profile_id,collection_id_idx,idx_faver_idx_id | idx_faver_idx_id |       4 | const                                   | 7910 | Using where |
|  1 | SIMPLE      | Item     | eq_ref | PRIMARY                                                       | PRIMARY          |       4 | gragland_imgfavebeta.Favorite.notice_id |    1 | Using where |
+----+-------------+----------+--------+---------------------------------------------------------------+------------------+---------+-----------------------------------------+------+-------------+

+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| favorites | CREATE TABLE `favorites` (
             `id` int(11) NOT NULL auto_increment COMMENT 'unique identifier',
             `faver_profile_id` int(11) NOT NULL default '0',
             `collection_id` int(11) default NULL,
             `collection_order` int(8) default NULL,
             `created` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'date this record was created',
             `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'date this record was modified',
             `notice_id` int(11) NOT NULL default '0',
             `removed` tinyint(1) NOT NULL default '0',
              PRIMARY KEY  (`id`),
              KEY `notice_id` (`notice_id`),
              KEY `faver_profile_id` (`faver_profile_id`),
              KEY `collection_id_idx` (`collection_id`),
              KEY `idx_faver_idx_id` (`faver_profile_id`,`id`)
              ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+


+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| items |CREATE TABLE `items` (
         `id` int(11) NOT NULL auto_increment COMMENT 'unique identifier',
         `submitter_id` int(11) NOT NULL default '0' COMMENT 'who made the update',
         `source_image` varchar(255) default NULL COMMENT 'update content',
         `cached_image` varchar(255) default NULL,
         `source_title` varchar(255) NOT NULL default '',
         `source_url` text NOT NULL,
         `width` int(4) NOT NULL default '0',
         `height` int(4) NOT NULL default '0',
         `status` varchar(122) NOT NULL default '',
         `popular` int(1) NOT NULL default '0',
         `made_popular` timestamp NULL default NULL,
         `fave_count` int(9) NOT NULL default '0',
         `tags` text,
         `user_art` tinyint(1) NOT NULL default '0',
         `nudity` tinyint(1) NOT NULL default '0',
         `created` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'date this record was created',
         `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'date this record was modified',
         `removed` int(1) NOT NULL default '0',
         `nofront` tinyint(1) NOT NULL default '0',
         `test` varchar(10) NOT NULL default '',
         `recs` text,
         `recs_data` text,
         PRIMARY KEY  (`id`),
         KEY `notice_profile_id_idx` (`submitter_id`),
         KEY `content` (`source_image`),
         KEY `idx_popular` (`popular`),
         KEY `idx_madepopular` (`made_popular`),
         KEY `idx_favecount_idx_id` (`fave_count`,`id`)
         ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

最佳答案

首先,按favorites.id排序,favorites.id是favorites表中的集群主键。您不必加入favoritesitems而不是itemsfavorites
其次,(Itemremoved='0')在WHERE中是多余的,因为JOIN中已经使用了相同的条件。
第三,将join中的条件顺序更改为:

`Favorite`.`notice_id` = `Item`.`id` AND `Item`.`removed` = 0

优化器将能够使用主键作为索引。您甚至可以考虑在items表上创建(id,removed)索引。
接下来,在favorites中创建(faver_profile_id,removed)索引(或者更好地更新faver_profile_id索引),并将其中的条件顺序更改为:
(`faver_profile_id` = 1)
AND (`Favorite`.`removed` = 0)
AND (`collection_id` IS NULL)

抱歉,我错过了你已经加入favoritesitems的消息。那么就不需要按顺序了。您应该得到如下结果:
SELECT
    `Item`.`id`,
    `Item`.`source_image`,
    `Item`.`cached_image`,
    `Item`.`source_title`,
    `Item`.`source_url`,
    `Item`.`width`,
    `Item`.`height`,
    `Item`.`fave_count`,
    `Item`.`created`
FROM `favorites` AS `Favorite`
LEFT JOIN `items` AS `Item`
ON (`Favorite`.`notice_id` = `Item`.`id` AND `Item`.`removed` = 0)
WHERE `faver_profile_id` = 1
    AND `Favorite`.`removed` = 0
    AND `collection_id` IS NULL
LIMIT 50;

还有一件事,当您拥有KEYidx_faver_idx_idfaver_profile_idid)时,您不需要KEYfaver_profile_idfaver_profile_id),因为第二个索引只复制了idx_faver_idx_id的一半。我希望你能像我建议的那样扩大第二个索引。

关于mysql - 需要帮助以联接优化MYSQL查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2598097/

10-11 05:24