- 论坛徽章:
- 2
|
本帖最后由 cenalulu 于 2012-10-10 10:23 编辑
下面SQL- select spc_results.*, spc_word_mst.WORD_NAME
- from spc_results
- LEFT JOIN spc_word_mst
- ON spc_results.WORD_ID = spc_word_mst.WORD_ID
- WHERE spc_results.KRNT_CD = 'apr'
- AND CONCAT(spc_results.SEARCH_DATE, ' ', spc_results.SEARCH_TIME) >=
- '2012/10/05 00:00:00'
- AND CONCAT(spc_results.SEARCH_DATE, ' ', spc_results.SEARCH_TIME) <
- '2012/10/05 24:00:00'
- ORDER BY spc_results.SEARCH_DATE desc,
- spc_results.SEARCH_TIME desc,
- spc_results.BAITAI_ID asc,
- RESULTS_ID asc LIMIT 0,
- 100
复制代码 spc_results表里数据几十万。
spc_word_mst数据几百条。
在不改变SQL的情况下,往哪个字段上加索引,会改善性能。
建表SQL- CREATE TABLE `spc_results` (
- `RESULTS_ID` bigint(20) NOT NULL AUTO_INCREMENT ,
- `KRNT_CD` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
- `WORD_ID` bigint(20) NOT NULL ,
- `BAITAI_ID` char(3) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
- `SEARCH_DATE` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `SEARCH_TIME` char(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
- `SPONSOR_URL` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `SPONSOR_TITLE` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `SPONSOR_INFORMATION` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `EVIDENCE_ADDRESS` bigint(20) NULL DEFAULT NULL ,
- `CREATED_AT` datetime NULL DEFAULT NULL ,
- `CREATED_USER` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `CREATED_IP` varchar(39) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `UPDATED_AT` datetime NULL DEFAULT NULL ,
- `UPDATED_USER` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `UPDATED_IP` varchar(39) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `AD_URL` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- PRIMARY KEY (`RESULTS_ID`),
- INDEX `KRNT_CD` USING BTREE (`KRNT_CD`) ,
- INDEX `dateindex` USING BTREE (`SEARCH_DATE`, `SEARCH_TIME`)
- )
- ENGINE=InnoDB
- DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
- AUTO_INCREMENT=1413495
- ROW_FORMAT=COMPACT
- ;
- CREATE TABLE `spc_word_mst` (
- `KRNT_CD` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
- `WORD_ID` bigint(20) NOT NULL ,
- `WORD_NAME` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `WORD_FLG` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `CREATED_AT` datetime NULL DEFAULT NULL ,
- `CREATED_USER` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `CREATED_IP` varchar(39) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `UPDATED_AT` datetime NULL DEFAULT NULL ,
- `UPDATED_USER` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- `UPDATED_IP` varchar(39) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
- PRIMARY KEY (`KRNT_CD`, `WORD_ID`),
- INDEX `WORD_ID` USING BTREE (`WORD_ID`)
- )
- ENGINE=InnoDB
- DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
- ROW_FORMAT=COMPACT
- ;
复制代码 我在spc_results_jh表的KRNT_CD字段上加了索引,spc_word_mst表的WORD_ID上加了索引。虽然type都变成了ref,不过速度提升不明显。Status的Handler_read_next高的吓人,11万多,这个数字跟只加spc_results.KRNT_CD = 'apr'条件所查出来的数据个数一样。是不是索引根本没起作用。
执行计划:
id table type possibale_key key ken_len ref rows extra
1 spc_results ref KRNT_CD KRNT_CD 18 const 131418 Using where; Using filesort
1 spc_word_mst ref WORD_ID WORD_ID 8 spc_results.WORD_ID 1
请高手指点。
|
|