简体   繁体   English

在极少数情况下,即使正在使用索引,MySQL的查询也会变慢

[英]MySQL slow query in rare cases even though index is being used

I have a query in a large MySQL table (>4 million rows). 我在一个大型MySQL表(> 400万行)中有一个查询。 This query is used in a stored procedure and it searches by surname and another numeric field. 此查询在存储过程中使用,并按姓和另一个数字字段搜索。 When I use different combinations of these search parameters, I get quick results (between 1 and 2s) but with some particular values, I get a query which takes 9s to return results on the production website. 当我使用这些搜索参数的不同组合时,会得到快速的结果(介于1s和2s之间),但是具有一些特定的值,我会得到一个查询,该查询需要9s才能在生产网站上返回结果。 The following is what I got out of the EXPLAIN statement: 以下是我从EXPLAIN语句中得到的结果:

id, select_type, table, type, possible_keys, key,       key_len, ref,   rows, Extra
--------------------------------------------
1,  SIMPLE,      Names, ref,  IX_Name,       IX_Name,   17,      const, 3173, Using where

Surame is declared as varchar(40) and the other field is unsigned smallint(6). Surame声明为varchar(40),另一个字段为unsigned smallint(6)。 The index which is being used is IX_Name which consists of the prefixes of surname(15) and forename(10) 正在使用的索引是IX_Name,它由surname(15)和forename(10)的前缀组成

I am not sure what I can do to improve the performance. 我不确定该如何提高性能。 Is there anything noticeably wrong with the EXPLAIN output above? 上面的EXPLAIN输出是否有明显的错误? The query runs slowly only on rare combinations of the surname and the other field, but it is consistent in that they are always the same slow queries. 该查询仅在姓氏和其他字段的稀有组合上缓慢运行,但这是一致的,因为它们始终是相同的缓慢查询。

I tried dropping all indices and recreated them but this did not solve the problematic queries. 我尝试删除所有索引并重新创建它们,但这不能解决有问题的查询。

The query plan shows that the index is being used and on different surnames (and various values for the other numeric field) the query can be instantaneous. 查询计划表明正在使用索引,并且对于不同的姓(以及其他数字字段的各种值),查询可以是瞬时的。 However, it doesn't like when I search for the surname "Fischer" and a particular value for the numeric field (this is one particular slow query). 但是,当我搜索姓氏“ Fischer”和数字字段的特定值(这是一个特殊的慢速查询)时,它不一样。 Could this be because there are many "Fischer" matching names in my table? 可能是因为我的表中有许多“ Fischer”匹配名称吗? (about 3500). (大约3500)。 But in that case, what can be done to optimize this query? 但是在那种情况下,可以做什么来优化此查询? Below is the schema of my table. 下面是我的表的架构。

namenumber: Primary Key,int,unsigned,not null,auto inc; 名称编号:主键,int,无符号,不为null,auto inc; surname: varchar(40); 姓:varchar(40); forename: varchar(40); 姓氏:varchar(40); f3: varchar(40); f3:varchar(40); f4: varchar(40); f4:varchar(40); f5: smallint(6),unsigned; f5:smallint(6),无符号; f6: smallint(6),unsigned; f6:smallint(6),无符号; f7: varchar(40); f7:varchar(40); f8: varchar(40); f8:varchar(40); f9: smallint(6); f9:smallint(6); f10: varchar(10); f10:varchar(10); f11: tinyint(4); f11:tinyint(4); f12: smallint(6),unsigned; f12:smallint(6),未签名; f13: text F13:文字

The query that I am running is the following: 我正在运行的查询如下:

SELECT namenumber,surname,forename,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13
FROM names IGNORE INDEX (IX_IGNORE1,IX_IGNORE2,IX_IGNORE3)
WHERE ((surname = 'fischer')) AND (f12 = 270)
LIMIT 0,14

MySQL uses the following index (IX_Name) which consists of the following fields: surname(15),forename(10) ie a 15 char prefix of surname and 10 char prefix of forename. MySQL使用以下索引(IX_Name),该索引包含以下字段:surname(15),forename(10),即姓氏的15个字符的前缀和姓氏的10个字符的前缀。

For most queries this is very fast, but for the rare ones, it takes about 9 seconds to return results to the web page, showing the output of EXPLAIN as above. 对于大多数查询来说,这是非常快的,但是对于少数查询,大约需要9秒才能将结果返回到网页,显示上面的EXPLAIN的输出。

Any ideas? 有任何想法吗?

Thanks in advance, TM 预先感谢,TM

Have you tried to modify the index IX_Name using only the surname. 您是否尝试仅使用姓氏来修改索引IX_Name。 It could be possible that the index causes the delay - because he returns the rows according to surname and forename (which is not part of your query) and needs for both only portions of the real column-content - which needs time to calculate. 索引有可能引起延迟-因为他根据姓和名返回行(这不是查询的一部分),并且只需要真实列内容的两个部分-这需要时间来计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM