MySql:在 select 语句中使用 @variable 需要花费数百倍的时间
我试图理解在等效代码中看到的巨大性能差异。或者至少我认为代码是等效的。
我有一张表,上面有大约 1000 万条记录。它包含一个字段,该字段的索引定义为: USPatentNum char(8)
如果我使用 MySql 将变量设置为一个值,则需要超过 218 秒。使用字符串文字执行完全相同的查询只需不到 1/4 秒。
在下面的代码中,第一个 select 语句(其中 USPatentNum = @pn;)永远持续下去,但第二个 select 语句使用文字值 (其中 USPatentNum = '5288812';)几乎是即时的
mysql> select @pn := '5288812'; +------------------+ | @pn := '5288812' | +------------------+ | 5288812 | +------------------+ 1 row in set (0.00 sec) mysql> select patentId, USPatentNum, grantDate from patents where USPatentNum = @pn; +----------+-------------+------------+ | patentId | USPatentNum | grantDate | +----------+-------------+------------+ | 306309 | 5288812 | 1994-02-22 | +----------+-------------+------------+ 1 row in set (3 min 38.17 sec) mysql> select @pn; +---------+ | @pn | +---------+ | 5288812 | +---------+ 1 row in set (0.00 sec) mysql> select patentId, USPatentNum, grantDate from patents where USPatentNum = '5288812'; +----------+-------------+------------+ | patentId | USPatentNum | grantDate | +----------+-------------+------------+ | 306309 | 5288812 | 1994-02-22 | +----------+-------------+------------+ 1 row in set (0.21 sec)
两个问题:
为什么 @pn 的使用速度这么慢? 我可以更改 select 语句以使性能相同吗?
I'm trying to understand a huge performance difference that I'm seeing in equivalent code. Or at least code I think is equivalent.
I have a table with about 10 million records on it. It contains a field, which is indexed defined as:
USPatentNum char(8)
If I set a variable withing MySql to a value, it takes over 218 seconds. The exact same query with a string literal takes under 1/4 of a second.
In the code below, the first select statement (with where USPatentNum = @pn;) takes forever, but the second, with the literal value
(where USPatentNum = '5288812';) is nearly instant
mysql> select @pn := '5288812'; +------------------+ | @pn := '5288812' | +------------------+ | 5288812 | +------------------+ 1 row in set (0.00 sec) mysql> select patentId, USPatentNum, grantDate from patents where USPatentNum = @pn; +----------+-------------+------------+ | patentId | USPatentNum | grantDate | +----------+-------------+------------+ | 306309 | 5288812 | 1994-02-22 | +----------+-------------+------------+ 1 row in set (3 min 38.17 sec) mysql> select @pn; +---------+ | @pn | +---------+ | 5288812 | +---------+ 1 row in set (0.00 sec) mysql> select patentId, USPatentNum, grantDate from patents where USPatentNum = '5288812'; +----------+-------------+------------+ | patentId | USPatentNum | grantDate | +----------+-------------+------------+ | 306309 | 5288812 | 1994-02-22 | +----------+-------------+------------+ 1 row in set (0.21 sec)
Two questions:
Why is the use of the @pn so much slower?
Can I change the select statement so that the performance will be the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在设置其值之前将 @pn 声明为 char(8)。
我怀疑它会像你现在所做的那样是一个 varchar 。如果是这样,性能损失是因为 MySql 无法将索引与您的变量进行匹配。
Declare @pn as char(8) before setting its value.
I suspect it will be a varchar as you do it now. If so, the performance loss is because MySql can't mach the index with your variable.
使用常量还是@var 并不重要。你会得到不同的结果,因为 MySQL 第二次从缓存中获取结果。如果您再次执行您的场景,但使用 const 和 @var 交换位置查询,您将得到相同的结果(但具有另一个值)。第一个会变慢,第二个会很快。
希望有帮助
It doesn't matter whether you use constant or @var. You get different result because the second time MySQL gets results from cache. If you execute once again your scenario but trade places queries with const and with @var you will get them same results (but with another value). First will be slowed, second will be fast.
Hope it helps