Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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 为什么使用WHERE IN子句的长查询比短查询执行得快?_Mysql_Where In - Fatal编程技术网

Mysql 为什么使用WHERE IN子句的长查询比短查询执行得快?

Mysql 为什么使用WHERE IN子句的长查询比短查询执行得快?,mysql,where-in,Mysql,Where In,我试图弄明白为什么长查询比短查询运行得快。这可能是因为一些索引吗 此查询大约需要半秒钟 SELECT * FROM (SELECT `positions`.`id`, `positions`.`keyword_id`, `positions`.`position`, @rank := IF(@group = keyword_id, @rank + 1, 1) AS

我试图弄明白为什么长查询比短查询运行得快。这可能是因为一些索引吗

此查询大约需要半秒钟

SELECT * 
FROM   (SELECT `positions`.`id`, 
               `positions`.`keyword_id`, 
               `positions`.`position`, 
               @rank := IF(@group = keyword_id, @rank + 1, 1) AS 
               rank_08b5648409889380fe88d86c30ff4c8d, 
               @group := keyword_id                           AS 
                      group_08b5648409889380fe88d86c30ff4c8d 
        FROM   (SELECT @rank := 0, 
                       @group := 0) AS vars, 
               positions 
        ORDER  BY `keyword_id` ASC, 
                  `created_at` DESC) AS positionsA 
       LEFT JOIN `positions` 
              ON `positionsa`.`id` = `positions`.`id` 
WHERE  `rank_08b5648409889380fe88d86c30ff4c8d` <= '2' 
       AND `positions`.`keyword_id` IN ( '1', '2', '3', ..., '1879', '1880' ) 
所有性能测试均在PHPmyAdmin中完成

以下是解释返回的内容:

更快、更长的查询:

较慢、较短的查询:

show create table positions
返回以下内容:

CREATE TABLE `positions` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `keyword_id` int(11) NOT NULL,
 `position` int(11) NOT NULL,
 `url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`) USING BTREE,
 UNIQUE KEY `keyword_id_created_at_index` (`keyword_id`,`created_at`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=467923 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
表中的索引如下:


对两个查询运行解释,并将输出复制到问题中。“没有这些,我们只能猜测。”我的猜测是,戈达迪·普米亚德明的分数份额。其他人在“做事情”@Shadow,好的,我添加了每个人的解释屏幕截图。@Drew,它在我的本地计算机上运行,因此与GoDaddy无关。@Drew,我添加了
show create table positions
。对两个查询运行解释,并将输出复制到问题中。“没有这些,我们只能猜测。”我的猜测是,戈达迪·普米亚德明的分数份额。其他人在“做事情”@Shadow,好的,我添加了每个人的解释屏幕截图。@Drew,它在我的本地计算机上运行,所以与GoDaddy无关。@Drew,我添加了
显示创建表位置的结果。
CREATE TABLE `positions` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `keyword_id` int(11) NOT NULL,
 `position` int(11) NOT NULL,
 `url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`) USING BTREE,
 UNIQUE KEY `keyword_id_created_at_index` (`keyword_id`,`created_at`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=467923 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci