1

我看到以下(简化的)查询出现在我的慢日志 mysql 文件中,它是由搜索引擎爬虫从分页网站爬取所有页面的。

select * from listing 
where 
   active=1 
   and approved=1 
   and deleted=0 
   and enabled=1 
order by 
   full_listing desc,  
   picture_count*rating/rating_count desc, 
   rating desc 
limit 21230, 10;

我很惊讶在只有 60,000 条记录的表上处理需要超过 8 秒。

解释计划看起来像这样

1   SIMPLE  listing0_   index_merge listing_active,listing_approved,listing_enabled,listing_deleted,sort_index  listing_active,listing_approved,listing_enabled,listing_deleted 1,1,1,1     3102    Using intersect(listing_active,listing_approved,listing_enabled,listing_deleted); Using where; Using filesort

我可以创建什么索引来提高其性能?

表结构:

'CREATE TABLE `listing` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `account_id` bigint(20) DEFAULT NULL,
  `domain_id` bigint(20) DEFAULT NULL,
  `active` bit(1) NOT NULL,
  `approved` bit(1) NOT NULL,
  `denied` bit(1) NOT NULL,
  `deleted` bit(1) NOT NULL,
  `enabled` bit(1) NOT NULL,
  `full_listing` bit(1) DEFAULT b''0'',
  `public_id` varchar(255) DEFAULT NULL,
  `name` varchar(100) NOT NULL,
  `rating` int(11) NOT NULL,
  `rating_count` int(11) NOT NULL,
  `rating_enabled` bit(1) NOT NULL,
  `picture_count` int(11) DEFAULT ''0'',
  `createdAt` datetime NOT NULL,
  `createdBy` varchar(15) NOT NULL,
  `updatedAt` datetime NOT NULL,
  `updatedBy` varchar(15) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `listing_public_id` (`public_id`),
  KEY `FKB4DC521D9306A80C` (`account_id`),
  KEY `FKB4DC522D7A66E1A8` (`domain_id`),
  KEY `listing_active` (`active`),
  KEY `listing_approved` (`approved`),
  KEY `listing_enabled` (`enabled`),
  KEY `listing_deleted` (`deleted`),
  KEY `listing_picture_count` (`picture_count`),
  KEY `listing_rating` (`rating`),
  KEY `listing_rating_count` (`rating_count`),
  KEY `listing_full_listing` (`full_listing`),
  CONSTRAINT `listing_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`),
  CONSTRAINT `listing_ibfk_2` FOREIGN KEY (`domain_id`) REFERENCES `domain` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=59512 DEFAULT CHARSET=utf8'
4

1 回答 1

1

Using filesort 可能是一个原因。什么是索引?就我个人而言,我会为你正在做的事情使用位移,例如列状态包含一个 int

像这样用右手位

0000 活跃 | 批准 | 已删除 | 启用

所以在你的例子中我会有where status = 1011 or status = 11(当位被转换回整数时)

然后你只有 1 个索引和所有组合(使用 0 和 1 有时可以让 mysql 忽略索引)

于 2013-01-07T11:35:17.880 回答