mysql中多列索引比单列索引慢

发布于 2024-12-12 05:45:08 字数 1001 浏览 0 评论 0原文

我有一个 myisam 表,其主键跨越 5 列。我对这 5 列中的每一列都使用 WHERE 进行选择。使用主键(多列索引)需要 25 秒,使用其中一列的单索引需要 1 秒。我做了一个分析,大部分 25 秒是在“发送数据”阶段拍摄的。主键的基数约为 7M,单列基数约为 80。我是否遗漏了什么?

CREATE TABLE `mytable` (
  `a` int(11) unsigned NOT NULL,
  `b` varchar(2) NOT NULL,
  `c` int(11) unsigned NOT NULL,
  `d` varchar(560) NOT NULL,
  `e` varchar(45) NOT NULL,
  PRIMARY KEY (`a`,`e`,`d`,`b`,`c`),
  KEY `d` (`d`),
  KEY `e` (`e`),
  KEY `b` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


EXPLAIN SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY PRIMARY 4   const   5912231 Using where


EXPLAIN SELECT * FROM mytable
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY,d,e,b   d   562 const   158951  Using where

I have a myisam table with a primary key spanning 5 columns. I do a select using a WHERE on every of those 5 columns ANDed. Using the primary key (multicolumn index) it takes 25s, using a single index in one of the columns it takes 1 sec. I did a profiling and most of the 25s is taken in “Sending data” stage. The primary key has cardinality of about 7M and the single column about 80. Am i missing somehting?

CREATE TABLE `mytable` (
  `a` int(11) unsigned NOT NULL,
  `b` varchar(2) NOT NULL,
  `c` int(11) unsigned NOT NULL,
  `d` varchar(560) NOT NULL,
  `e` varchar(45) NOT NULL,
  PRIMARY KEY (`a`,`e`,`d`,`b`,`c`),
  KEY `d` (`d`),
  KEY `e` (`e`),
  KEY `b` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


EXPLAIN SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY PRIMARY 4   const   5912231 Using where


EXPLAIN SELECT * FROM mytable
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY,d,e,b   d   562 const   158951  Using where

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-12-19 05:45:08

该问题是由铸造引起的,
尝试引用每个 varchar 列 b,d,e

SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e='1319677200' AND d='69.171.242.53' AND b='*' AND c=0;

The problem is caused by casting,
try quote every varchar column b,d,e

SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e='1319677200' AND d='69.171.242.53' AND b='*' AND c=0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文