Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 - Fatal编程技术网

MySQL查询速度慢,按+;分组

MySQL查询速度慢,按+;分组,mysql,Mysql,自从添加了orderby子句以来,我有一个执行缓慢的查询。这似乎与分组依据和订单依据的组合有关,但我不确定从何处开始优化。groups表当前有大约100k行,联接中使用的所有id都被索引 SELECT groups.name,groups.account_id,groups.description,groups.id, GROUP_CONCAT(DISTINCT categories.name) as cat, GROUP_CONCAT(DISTINCT group_tags.name) as

自从添加了
orderby
子句以来,我有一个执行缓慢的查询。这似乎与
分组依据
订单依据
的组合有关,但我不确定从何处开始优化。groups表当前有大约100k行,联接中使用的所有id都被索引

SELECT groups.name,groups.account_id,groups.description,groups.id, GROUP_CONCAT(DISTINCT categories.name) as cat, GROUP_CONCAT(DISTINCT group_tags.name) as tag
FROM groups 
LEFT JOIN group_categories ON groups.id = group_categories.group_id
LEFT JOIN categories ON group_categories.cat_id = categories.id
LEFT JOIN group_tags ON groups.id = group_tags.group_id
GROUP BY groups.id
ORDER BY case when groups.account_id = 0 then 1 else 0 end, groups.name
解释输出:

SIMPLE  groups              index   NULL        PRIMARY     4    NULL                       100843  Using temporary; Using filesort
SIMPLE  group_categories    ref     group_id    group_id    4    groups.id                  1   
SIMPLE  categories          eq_ref  PRIMARY     PRIMARY     4    group_categories.cat_id    1   
CREATE TABLE `groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `last_updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `accountid` (`account_id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=109850 DEFAULT CHARSET=utf8;
创建表格:

SIMPLE  groups              index   NULL        PRIMARY     4    NULL                       100843  Using temporary; Using filesort
SIMPLE  group_categories    ref     group_id    group_id    4    groups.id                  1   
SIMPLE  categories          eq_ref  PRIMARY     PRIMARY     4    group_categories.cat_id    1   
CREATE TABLE `groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `last_updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `accountid` (`account_id`),
  KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=109850 DEFAULT CHARSET=utf8;

看看“Using temporary;Using filesort”:这意味着整个结果集在开始发送之前都会被提取并排序。罪魁祸首是按大小写排序的
,它不能在mySQLTry
中按顺序(groups.account\u id=0)
索引。不确定它是否有帮助,但它可以,而且它比较短。@JanDvorak不幸的是,即使我将其更改为简单的
按组排序。name
它仍然执行得很差,因此在删除案例时确实会去掉文件排序,但不会加快速度:-),那么它很可能缺少不同的索引。当您按组进行排序时,解释会说什么?