优化简单的 SQL 查询?

发布于 2024-08-23 22:59:55 字数 587 浏览 4 评论 0原文

问候朋友,

在我的 MySQL 数据库中,我有“MAB”表,其中包含有关基因 ID (GI_ID) 的信息,以及一些其他基因相关信息。

“MAB_CAN”表可以包含仅与癌症相关的基因 ID (GI_ID)。

我使用以下 SQL 查询从 MAB 表中获取癌症相关信息:

SELECT * FROM MAB WHERE `GI_ID` IN (SELECT `GI ID` FROM `MAB_CAN`)

此查询大约需要 14 秒,太长了。(1605 条记录)。

但接下来的独立查询则需要很短的时间。

MAB_CAN WHERE 1中选择GI_ID

  • 0.0005 秒(1,605 条记录)

MAB WHERE 1中选择*

  • 0.0007 秒(31,043 条记录)

优化我的第一个查询有什么建议吗?

Greetings friends,

In my MySQL database, I have 'MAB' table which contains information about Gene Ids (GI_ID),and some other gene related information.

'MAB_CAN' table can contains the Gene Ids (GI_ID) only relevant to Cancer.

I use following SQL query to get cancer related information from MAB table :

SELECT * FROM MAB WHERE `GI_ID` IN (SELECT `GI ID` FROM `MAB_CAN`)

It takes about 14 Seconds for this query, which is too long.(1605 records).

But the following independent queries it takes short time.

SELECT GI_ID FROM MAB_CAN WHERE 1

  • 0.0005 secs (1,605 records)

SELECT * FROM MAB WHERE 1

  • 0.0007 sec (31,043 records)

Any tips of optimizing my first query ?

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

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

发布评论

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

评论(3

高冷爸爸 2024-08-30 22:59:55

尝试一下,

SELECT * FROM MAB
   INNER JOIN MAB_CAN ON MAB.GI_ID = MAB_CAN.GI_ID

然后您需要查看需要返回哪些列,您永远不应该使用 SELECT * 而是返回所需列名称的不同列表,例如 SELECT Col1、Col2、Col3 FROM... 这样您就可以最小化返回的数据。

正如其他答案所指出的,如果查询仍然很慢,您需要考虑在 MAB 表中的 GI_ID 字段上放置索引。

Try this

SELECT * FROM MAB
   INNER JOIN MAB_CAN ON MAB.GI_ID = MAB_CAN.GI_ID

You then need to look at what columns you need to return you should never really use SELECT * instead return a distinct list of required column names e.g. SELECT Col1, Col2, Col3 FROM... that way you minimize the returned data.

As the other answers have indicated if the query is still slow you need to look at placing an index on the GI_ID field in the MAB table.

罗罗贝儿 2024-08-30 22:59:55
  1. 将 * 替换为列名
  2. 在 GI_ID 上创建非聚集索引
  1. replace * with column names
  2. create non-clustered index on GI_ID
迷迭香的记忆 2024-08-30 22:59:55

您可以从在 MAB 表中的 GI_ID 列上创建索引开始。

You can start with creating an index on GI_ID column in the MAB table.

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