我有这些表格:
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;
有人可以帮我创建哪些索引以防止使用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
1, 'SIMPLE', 'W0', 'ref', 'ix_dstat_nt_word_id_order,ix_dstat_word_id_order,ix_dstat_nt_grp_no_word_id_order,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, ''
答案 0 :(得分:0)
看起来你应该在cstat.id_cstat上有一个索引 和一个关于dstat for word,和name_type
答案 1 :(得分:0)
更改您的表格,并在表格index
上的id_cstat
栏上添加dstat
ALTER TABLE dstat ADD INDEX tb_idx(id_cstat)
因此不需要全表扫描,也需要表cstat
ALTER TABLE cstat ADD INDEX tba_idx(`order`)
答案 2 :(得分:0)
通过添加以下索引解决了问题:
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`);