繁体   English   中英

MySQL查询优化-索引

[英]MySQL query optimization - index

我有这些表:

CREATE TABLE  `cstat` (
  `id_cstat` bigint(20) NOT NULL,
  `lang_code` varchar(3) NOT NULL,
  `description` varchar(255) NOT NULL,
  `description_tr` varchar(255) NOT NULL,
  `id_ccountry` varchar(3) NOT NULL,
  `geometry_point` point DEFAULT NULL,
  `geometry_poly` polygon DEFAULT NULL,
  `name_type` varchar(1) NOT NULL,
  `bb_min_lat` double DEFAULT NULL,
  `bb_min_lon` double DEFAULT NULL,
  `bb_max_lat` double DEFAULT NULL,
  `bb_max_lon` double DEFAULT NULL,
  `has_ex` tinyint(1) NOT NULL DEFAULT '0',
  `order` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_cstat`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE  `dstat` (
  `id_cstat` bigint(20) NOT NULL,
  `lang_code` varchar(3) NOT NULL,
  `word` varchar(30) NOT NULL,
  `word_no` tinyint(3) unsigned NOT NULL,
  `word_cnt` tinyint(3) unsigned NOT NULL,
  `word_grp` tinyint(3) unsigned NOT NULL,
  `name_type` char(1) CHARACTER SET ascii NOT NULL,
  `cstat_order` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

我需要使用dstat中的条件并按cstat.order或dstat.cstat_order的顺序从cstat中选择一条记录。

我的查询如下所示:

SELECT cstat.ID_CSTAT, cstat.LANG_CODE, cstat.DESCRIPTION, cstat.DESCRIPTION_TR, cstat.ID_CCOUNTRY, AsBinary(cstat.GEOMETRY_POINT) AS GEOMETRY_POINT, cstat.NAME_TYPE, cstat.BB_MIN_LAT, cstat.BB_MIN_LON, cstat.BB_MAX_LAT, cstat.BB_MAX_LON, cstat.HAS_EX
FROM cstat cstat
JOIN dstat W0
ON (W0.ID_CSTAT = cstat.ID_CSTAT)
where
  (W0.WORD = 'ceska') AND
  (W0.NAME_TYPE = 'B')
ORDER BY W0.CSTAT_ORDER;

谁能帮助我创建防止使用文件排序的索引? 我相信我已经尝试了几乎所有方法,但都失败了。 此基本查询还有更多修改(表dstat的另一个一个或多个联接,但这现在不重要)。

非常感谢你的帮助。

编辑:说实话-我已经尝试了很多索引。 我声明的基数是cstat_id_cstat上的主键。 目前,我有以下索引:

KEY `ix_dstat_nt_word_id_order` (`name_type`,`word`,`id_cstat`,`cstat_order`);
KEY `ix_dstat_word_id_order` (`word`,`id_cstat`,`cstat_order`);
KEY `ix_dstat_nt_grp_no_word_id_order` (`name_type`,`word_grp`,`word_no`,`word`,`id_cstat`,`cstat_order`);
KEY `ix_dstat_grp_no_word_id_order` (`word_grp`,`word_no`,`word`,`id_cstat`,`cstat_order`);
KEY `ix_dstat_nt_grp_word_id_order` (`name_type`,`word_grp`,`word`,`id_cstat`,`cstat_order`);

这部分解决了我的索引阅读问题。 但是排序总是使用filesort进行的。

EXPLAIN

1, 'SIMPLE', 'W0', 'ref', 'ix_dstat_nt_word_id_order,ix_dstat_word_id_order,ix_dstat_nt_grp_no_word_id_ord‌​er,ix_dstat_nt_grp_word_id_order', 'ix_dstat_nt_word_id_order', '93', 'const,const', 1, 'Using where; Using index; Using filesort' 
1, 'SIMPLE', 'cstat', 'eq_ref', 'PRIMARY,ix_cstat_id_order', 'PRIMARY', '8', 'search_2012_q3_cze_svk_dev.W0.id_cstat', 1, ''

看起来您应该在cstat.id_cstat上有一个索引,在dstat上有一个word和name_type索引

改变你的表,添加index的列id_cstat上表dstat

ALTER TABLE dstat ADD INDEX tb_idx(id_cstat)

因此它不需要全表扫描,然后在表cstat也可以

ALTER TABLE cstat ADD INDEX tba_idx(`order`)

通过添加以下索引解决了问题:

KEY `ix_dstat_nt_word_order_id_grp_no` (`name_type`,`word`,`cstat_order`,`id_cstat`,`word_grp`,`word_no`);
KEY `ix_dstat_word_order_id_grp_no` (`word`,`cstat_order`,`id_cstat`,`word_grp`,`word_no`);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM