我有这些表:
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。
我的查询看起来像这样:
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;
有人可以帮助我创建什么索引以防止使用FileSort?我相信我已经尝试了几乎所有事情,但是我失败了。此基本查询有更多修改(表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
看起来您应该在cstat.id_cstat上有一个索引一个在dstat上的Word,name_type
更改表格,并在表id_cstat
上的CC_3上添加index
ALTER TABLE dstat ADD INDEX tb_idx(id_cstat)
因此,它不需要全表扫描,然后在表cstat
ALTER TABLE cstat ADD INDEX tba_idx(`order`)
通过添加以下索引来解决问题:
键`ix_dstat_nt_word_order_id_grp_grp_no`(`name_type`,w word`,`cstat_order`,'id_cstat`,word_grp`,word_no`);键`ix_dstat_word_order_id_grp_no`(w word`,`cstat_order',`iD_cstat`,`word_grp`,`word_no`);