mysql中的嵌套选择加载时间

发布于 2024-07-17 04:45:13 字数 592 浏览 10 评论 0原文

我有一桌数据包
数据包包含2个字段id和total
它们都是 bigint 长度 20 和主键
只有id是自增的
每条记录总计 = id
我有 10000 条记录,假设 id = 1 - 10000 且总计 = 1 - 10000

我有 2 个几乎相同的查询,其中 1 个导致加载时间非常长,一个几乎立即完成。

非常非常长的加载时间:

set @i = 0; 
SELECT *
FROM packets as p1
WHERE p1.total in ( SELECT p2.total
FROM packets as p2 where ( ( @i := ( @i + 1 ) )

非常短的加载时间:

set @i = 0;
SELECT *
FROM packets as p1
WHERE p1.id in ( SELECT p2.id
FROM packets as p2 where ( ( @i := ( @i + 1 ) )

当 id 和total 的值相同时会发生这种情况。

I have one table packets
packets contains 2 fields id and total
they are both bigint length 20 and primary keys
only id is auto increment
in every record total = id
i got 10000 records let's say id = 1 - 10000 and total = 1 - 10000

i got 2 almost identical queries 1 results in a very very long loading time and one completes almost instantly.

very very long loading time:

set @i = 0; 
SELECT *
FROM packets as p1
WHERE p1.total in ( SELECT p2.total
FROM packets as p2 where ( ( @i := ( @i + 1 ) )

very short loading time:

set @i = 0;
SELECT *
FROM packets as p1
WHERE p1.id in ( SELECT p2.id
FROM packets as p2 where ( ( @i := ( @i + 1 ) )

this happens while the values of id and total are identical.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-07-24 04:45:13

它们都是bigint长度20并且是主键

您不能在一个表中有两个主键。

您似乎没有 packets.total 上的索引

创建它并查看它是否有帮助:

CREATE INDEX ix_packets_total ON packets (total)

they are both bigint length 20 and primary keys

You cannot have two primary keys in one table.

It seems that you don't have an index on packets.total

Create it and see if it helps:

CREATE INDEX ix_packets_total ON packets (total)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文