为什么“where id in (n1, n2, n3, ..., n2000)"慢得难以置信?

Why does the amp;quot;where id in (n1, n2, n3, ..., n2000)amp;quot; incredibly slow?(为什么“where id in (n1, n2, n3, ..., n2000)慢得难以置信?)
本文介绍了为什么“where id in (n1, n2, n3, ..., n2000)"慢得难以置信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个大约有一亿行的表,列 'id' 是主键,它是表中唯一的键.

I have a table which has about one hundred million rows, and the column 'id' is the primary key, and it is the only key in the table.

我进行了如下查询:

SELECT id,name FROM table WHERE id IN (id1, id2, id3, id4, ..., id1000);

IN"中的这 1000 个 id 实际上是由程序预先计算的常量整数.

These 1000 ids inside "IN" are actually const integers which are pre-caculated by a program.

但是Mysql每次查询都要花一分钟左右的时间.它并不慢,但它非常慢.条款有什么问题?非常感谢!

But Mysql spends about one minute to do the query every time. It is not slow, but it is incredibly slow. What's wrong with the clause? Thank you very much!

表定义:

CREATE TABLE mytable
(
    id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
    catid smallint(5) unsigned NOT NULL DEFAULT '0',
    name char(39) NOT NULL,
    originalname varchar(255) NOT NULL,
    thumb varchar(255) NOT NULL DEFAULT '',
    description varchar(255) NOT NULL DEFAULT '',
    status tinyint(2) unsigned NOT NULL DEFAULT '1',
    creationtime int(11) unsigned NOT NULL DEFAULT '0',
    updatetime int(11) unsigned NOT NULL DEFAULT '0',
    score int(11) unsigned NOT NULL
    PRIMARY KEY (id)
)
ENGINE=MyISAM
AUTO_INCREMENT=13074618
DEFAULT CHARSET=utf8

推荐答案

在 IN 'list' 达到一定大小后,MySQL 将切换到 TABLE/INDEX SCAN,这可能会非常慢.

After the IN 'list' reaches a certain size, MySQL will swap to a TABLE/INDEX SCAN, this has the possibility of being terribly slow.

您可以重写查询以使用 TEMPORARY TABLE 或 JOIN (SELECT UNION),看看这是否有助于提高性能.

You can rewrite the query to use a TEMPORARY TABLE, or JOIN (SELECT UNION), to see if that helps performance.

考虑运行 EXPLAIN EXTENDED 看看是什么地方变慢了下来.

Consider running EXPLAIN EXTENDED to see what slows it down.

这篇关于为什么“where id in (n1, n2, n3, ..., n2000)"慢得难以置信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Union in SQL while creating XML file(创建 XML 文件时在 SQL 中联合)
How to update a SQL table column with XML data(如何使用 XML 数据更新 SQL 表列)
How To Save XML Query Results to a File(如何将 XML 查询结果保存到文件)
Extracting XML sub-tags from a clob in Oracle via SQL(通过 SQL 从 Oracle 中的 clob 中提取 XML 子标签)
installed Xampp on Windows 7 32-bit. Errors when starting(在 Windows 7 32 位上安装 Xampp.启动时的错误)
Mysql lower case table on Windows xampp(Windows xampp 上的 Mysql 小写表)