我有这样的查询(由Hibernate生成 - 为了更清晰而编辑 - 下面你会找到完整的查询):
select
visitgroup0_.VISIT_ID as col_0_0_ ...
from
ADM_VISIT_GROUP visitgroup0_,
ADM_CONTACT_OWNER contactown1_
where
visitgroup0_.TIME>'2012-02-14 02:59:24' and
visitgroup0_.VENDOR_ID='***' and
visitgroup0_.CONTACT_STATE='PROSPECT' and
contactown1_.CONTACT_ID=visitgroup0_.CONTACT_ID and
contactown1_.OWNER_ID='***' order by visitgroup0_.TIME desc limit 30;
简单地说,我正在我们的网站上记录客户访问,每个客户都分配了所有者 - 销售代表。我需要向所有者访问他的联系人。
访问存储在ADM_VISIT_GROUP表中 - > 200 000行,访问的contact_id列指向ADM_CONTACT。 联系人存储在ADM_CONTACT表中 - 大约500 000行。 联系人的所有权存储在ADM_CONTACT_OWNER表中 - 与ADM_CONTACT中的行数或多或少相同。
解释说明:
+----+-------------+--------------+--------+----------------------------------------------------------------------------------------------------------------------------------+----------------------+---------+------------------------------------------------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+--------+----------------------------------------------------------------------------------------------------------------------------------+----------------------+---------+------------------------------------------------+-------+--------------------------+
| 1 | SIMPLE | visitgroup0_ | range | FK_VISIT_GROUP_VENDOR_ID,IDX_VISIT_GROUP_VENDOR_ID_CONTACT_ID,IDX_VISIT_GROUP_TIME,IDX_VISIT_GROUP_CONTACT_ID | IDX_VISIT_GROUP_TIME | 9 | NULL | 19640 | Using where |
| 1 | SIMPLE | contactown1_ | eq_ref | CONTACT_ID,FK_CONTACT_OWNER_CONTACT_ID,FK_CONTACT_OWNER_OWNER_ID,IDX_CONTACT_OWNER_CONTACT_OWNER,IDX_CONTACT_OWNER_OWNER_DELETED | CONTACT_ID | 1534 | salesmanago_main.visitgroup0_.CONTACT_ID,const | 1 | Using where; Using index |
+----+-------------+--------------+--------+----------------------------------------------------------------------------------------------------------------------------------+----------------------+---------+------------------------------------------------+-------+--------------------------+
使用以下方法创建ADM_VISIT_GROUP表:
CREATE TABLE `ADM_VISIT_GROUP` (
`ID` char(128) COLLATE utf8_polish_ci NOT NULL,
`UUID` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`CONTACT_STATE` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`VISIT_ID` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`HOST` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`IP` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`LOCATION` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`URI` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`TIME` datetime DEFAULT NULL,
`CONVERSATION` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`DURATION` bigint(20) DEFAULT NULL,
`VISIT_SOURCE` varchar(255) COLLATE utf8_polish_ci NOT NULL,
`VISIT_SOURCE_HOST` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`VISIT_SOURCE_KEYWORDS` varchar(1024) COLLATE utf8_polish_ci DEFAULT NULL,
`VISIT_SOURCE_DETAILS` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`VISIT_SCORE` bigint(20) DEFAULT NULL,
`CLIENT` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`EMAIL` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`CONTACT_ID` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`CONVERSATION_ID` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`IP_ORGANIZATION` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`ISP_ONLY` tinyint(1) DEFAULT NULL,
`VENDOR_ID` varchar(255) COLLATE utf8_polish_ci NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK_VISIT_GROUP_VENDOR_ID` (`VENDOR_ID`),
KEY `IDX_VISIT_GROUP_VENDOR_ID_CONTACT_ID` (`VENDOR_ID`,`CONTACT_ID`),
KEY `IDX_VISIT_GROUP_ISP_ONLY_VENDOR_ID_CONTACT_ID` (`ISP_ONLY`,`VENDOR_ID`,`CONTACT_ID`),
KEY `IDX_VISIT_GROUP_TIME` (`TIME`),
KEY `IDX_VISIT_GROUP_EMAIL` (`EMAIL`),
KEY `IDX_VISIT_GROUP_CONTACT_ID` (`CONTACT_ID`),
CONSTRAINT `FK_VISIT_GROUP_VENDOR_ID` FOREIGN KEY (`VENDOR_ID`) REFERENCES `ADM_VENDOR` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci
表ADM_CONTACT_OWNER:
CREATE TABLE `ADM_CONTACT_OWNER` (
`ID` char(128) COLLATE utf8_polish_ci NOT NULL,
`VERSION` int(11) NOT NULL,
`CONTACT_ID` varchar(255) COLLATE utf8_polish_ci NOT NULL,
`OWNER_ID` varchar(255) COLLATE utf8_polish_ci NOT NULL,
`CREATED_ON` datetime NOT NULL,
`OWNERSHIP_RIGHTS` varchar(255) COLLATE utf8_polish_ci NOT NULL,
`GRANTED_BY_ID` varchar(255) COLLATE utf8_polish_ci NOT NULL,
`MODIFIED_ON` datetime NOT NULL,
`DELETED` tinyint(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `CONTACT_ID` (`CONTACT_ID`,`OWNER_ID`),
KEY `FK_CONTACT_OWNER_GRANTED_BY_ID` (`GRANTED_BY_ID`),
KEY `FK_CONTACT_OWNER_CONTACT_ID` (`CONTACT_ID`),
KEY `FK_CONTACT_OWNER_OWNER_ID` (`OWNER_ID`),
KEY `IDX_CONTACT_OWNER_CONTACT_OWNER` (`CONTACT_ID`,`OWNER_ID`),
KEY `IDX_CONTACT_OWNER_OWNER_DELETED` (`OWNER_ID`,`DELETED`),
CONSTRAINT `FK_CONTACT_OWNER_CONTACT_ID` FOREIGN KEY (`CONTACT_ID`) REFERENCES `ADM_CONTACT` (`ID`),
CONSTRAINT `FK_CONTACT_OWNER_GRANTED_BY_ID` FOREIGN KEY (`GRANTED_BY_ID`) REFERENCES `ADM_USER_ACCOUNT` (`ID`),
CONSTRAINT `FK_CONTACT_OWNER_OWNER_ID` FOREIGN KEY (`OWNER_ID`) REFERENCES `ADM_USER_ACCOUNT` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci
完整查询:
select
visitgroup0_.VISIT_ID as col_0_0_,
visitgroup0_.DURATION as col_1_0_,
visitgroup0_.TIME as col_2_0_,
visitgroup0_.VISIT_SOURCE as col_3_0_,
visitgroup0_.VISIT_SOURCE_HOST as col_4_0_,
visitgroup0_.VISIT_SOURCE_KEYWORDS as col_5_0_,
visitgroup0_.EMAIL as col_6_0_,
visitgroup0_.IP_ORGANIZATION as col_7_0_,
visitgroup0_.CLIENT as col_8_0_,
visitgroup0_.HOST as col_9_0_,
visitgroup0_.IP as col_10_0_,
visitgroup0_.LOCATION as col_11_0_,
visitgroup0_.URI as col_12_0_,
visitgroup0_.UUID as col_13_0_,
visitgroup0_.VISIT_SCORE as col_14_0_,
visitgroup0_.CONVERSATION as col_15_0_,
visitgroup0_.CONTACT_ID as col_16_0_,
visitgroup0_.CONVERSATION_ID as col_17_0_
from
ADM_VISIT_GROUP visitgroup0_,
ADM_CONTACT_OWNER contactown1_
where
visitgroup0_.TIME>'2012-02-14 02:59:24' and
visitgroup0_.VENDOR_ID='3739d7a7-2e8f-4409-bd2d-2b505b5e7749' and
visitgroup0_.CONTACT_STATE='PROSPECT' and
contactown1_.CONTACT_ID=visitgroup0_.CONTACT_ID and
contactown1_.OWNER_ID='3f440a3e-a55e-44f5-ac75-d30bd27f4f97'
order by visitgroup0_.TIME desc limit 30;
任何人都可以帮助我提高查询速度吗?
答案 0 :(得分:1)
它必须手动过滤掉很多行,因为mysql只能在TIME上使用索引(IDX_VISIT_GROUP_TIME)
因为MySQL在查询中每个表只能使用一个索引,所以请尝试在上面创建组合索引:
VENDOR_ID
CONTACT_STATE
CONTACT_ID
TIME
列的顺序在索引中很重要!如果所有子句都在查询中,则只能从第一个子句到最后一个子句使用索引。并使用>或类似的东西很可能会使索引的其余部分无法使用。这就是TIME在索引中排在最后的原因。
但是某个列没有出现在你的where子句中,它无法到达TIME列的索引部分,索引的效果要差很多。这就是为什么在这种情况下你会想要用这些列创建一个单独的索引。
答案 1 :(得分:0)
我会:
OWNER_ID
,VENDOR_ID
和CONTACT_ID
数据类型从VARCHAR
更改为UNSIGNED INTEGER
或任何其他数字类型JOIN
和ADM_VISIT_GROUP
ADM_CONTACT_OWNER
CONTACT_STATE
更改为STATE
表的外键或使用数字字段INDEX
创作