gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:15:11 25 4
gpt4 key购买 nike

我在一个大型 MySQL 表(> 400 万行)中有一个查询。此查询在存储过程中使用,它按姓氏和另一个数字字段进行搜索。当我使用这些搜索参数的不同组合时,我会很快得到结果(在 1 到 2 秒之间),但对于某些特定值,我会得到一个需要 9 秒才能在生产网站上返回结果的查询。以下是我从 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). The index which is being used is IX_Name which consists of the prefixes of surname(15) and forename(10)

I am not sure what I can do to improve the performance. Is there anything noticeably wrong with the EXPLAIN output above? 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). Could this be because there are many "Fischer" matching names in my table? (about 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; surname: varchar(40); forename: varchar(40); f3: varchar(40); f4: varchar(40); f5: smallint(6),unsigned; f6: smallint(6),unsigned; f7: varchar(40); f8: varchar(40); f9: smallint(6); f10: varchar(10); f11: tinyint(4); f12: smallint(6),unsigned; f13: text

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 使用以下索引 (IX_Name),它由以下字段组成:surname(15),forename(10),即 15 个字符的姓氏前缀和 10 个字符的名字前缀。

对于大多数查询,这是非常快的,但对于极少数查询,大约需要 9 秒才能将结果返回到网页,如上所示显示 EXPLAIN 的输出。

有什么想法吗?

提前致谢,TM

最佳答案

您是否尝试过仅使用姓氏来修改索引 IX_Name。索引可能会导致延迟 - 因为他根据姓氏和名字(这不是你的查询的一部分)返回行并且只需要实际列内容的两个部分 - 这需要时间来计算。

关于即使正在使用索引,在极少数情况下 MySQL 查询也会变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1299048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com