我一直在運行在MySQL慢日誌由於生產問題和頭號查詢:我能做些什麼來使這個SQL更有效? (表有850K行)
select * from feeditem feeditem0_ where feeditem0_.importance=0 and feeditem0_.company_id=N limit 21;
我已經縮寫選擇(N是一個id爲FK )作爲它從休眠生成的,它只是選擇該表中的所有字段。當我做一個mysql解釋我得到:
explain select * from feeditem feeditem0_ where feeditem0_.importance=0 and feeditem0_.company_id=5045 limit 21 \G;;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: feeditem0_
type: index_merge
possible_keys: FKF49961B13D5FD8EF,importance
key: FKF49961B13D5FD8EF,importance
key_len: 9,5
ref: NULL
rows: 2422
Extra: Using intersect(FKF49961B13D5FD8EF,importance); Using where
該表中約有850K行。
的模式是:
CREATE TABLE `feeditem` (
`DTYPE` varchar(31) NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`dateCreated` datetime DEFAULT NULL,
`endSentance` varchar(255) DEFAULT NULL,
`importance` int(11) DEFAULT NULL,
`startSentance` varchar(255) DEFAULT NULL,
`summary` varchar(255) DEFAULT NULL,
`summaryComplete` bit(1) NOT NULL,
`targetId` bigint(20) DEFAULT NULL,
`targetSentance` text,
`type` varchar(255) NOT NULL,
`hasRead` bit(1) DEFAULT NULL,
`teamProject_id` bigint(20) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL,
`usertoread_id` bigint(20) DEFAULT NULL,
`contentType` varchar(255) DEFAULT NULL,
`company_id` bigint(20) DEFAULT NULL,
`updated` int(1) unsigned DEFAULT NULL,
`feedType` varchar(255) DEFAULT NULL,
`extraInfo` varchar(255) DEFAULT NULL,
`extraTargetId` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKF49961B1B74A2DA5` (`user_id`),
KEY `FKF49961B17CE9E5EF` (`teamProject_id`),
KEY `FKF49961B137B7D1B4` (`usertoread_id`),
KEY `FKF49961B13D5FD8EF` (`company_id`),
KEY `importance` (`importance`),
KEY `dateCreated` (`dateCreated`)
) ENGINE=InnoDB AUTO_INCREMENT=956498 DEFAULT CHARSET=utf8
有什麼辦法,我得到制止被掃描的2400個奇數行?這是從慢日誌(使用mysqlsla)摘要:
Count : 61 (53.98%)
Time : 523 s total, 8.57377 s avg, 6 s to 19 s max (54.03%)
95% of Time : 456 s total, 8 s avg, 6 s to 14 s max
Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%)
95% of Lock : 0 total, 0 avg, 0 to 0 max
Rows sent : 34 avg, 21 to 51 max (38.69%)
Rows examined : 3.49k avg, 40 to 8.89k max (0.00%)
Users :100.00% (61) of query, 100.00% (113) of all users
感謝
更新1:我加2山坳指數(稱爲feedquery),但它似乎對優化器選擇不使用指數:
mysql> explain select id from feeditem feeditem0_ where feeditem0_.importance=0 and feeditem0_.company_id=5045 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: feeditem0_
type: index_merge
possible_keys: FKF49961B13D5FD8EF,importance,feedquery
key: FKF49961B13D5FD8EF,feedquery
key_len: 9,14
ref: NULL
rows: 2753
Extra: Using intersect(FKF49961B13D5FD8EF,feedquery); Using where; Using index
如果我忽略指數:
explain select id from feeditem feeditem0_ ignore index (FKF49961B13D5FD8EF) where feeditem0_.importance=0 and feeditem0_.company_id=5045 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: feeditem0_
type: ref
possible_keys: importance,feedquery
key: feedquery
key_len: 14
ref: const,const
rows: 8496
Extra: Using where; Using index
表:
CREATE TABLE `feeditem` (
.....
PRIMARY KEY (`id`),
KEY `FKF49961B1B74A2DA5` (`user_id`),
KEY `FKF49961B17CE9E5EF` (`teamProject_id`),
KEY `FKF49961B137B7D1B4` (`usertoread_id`),
KEY `FKF49961B13D5FD8EF` (`company_id`),
KEY `importance` (`importance`),
KEY `dateCreated` (`dateCreated`),
KEY `feedquery` (`importance`,`company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=999359 DEFAULT CHARSET=utf8
更新2: @Salman一個
SHOW profile;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000342 |
| checking permissions | 0.000024 |
| Opening tables | 0.000053 |
| System lock | 0.000027 |
| init | 0.000166 |
| optimizing | 0.000068 |
| statistics | 0.012869 |
| preparing | 0.000202 |
| executing | 0.000008 |
| Sending data | 0.332767 |
| end | 0.000022 |
| query end | 0.000009 |
| closing tables | 0.000016 |
| freeing items | 0.000040 |
| logging slow query | 0.000005 |
| cleaning up | 0.000014 |
+----------------------+----------+
ibdata1中大約是1.5 GB
你可以發佈'SET profiling = 1;/*您的查詢在這裏* /; SHOW個人資料;'。 –
另外,桌子的實際大小是多少?幾MB?或者更多? –