通过 BOOLEAN 值优化 SQL (mySQL)

发布于 2024-12-12 01:41:53 字数 544 浏览 0 评论 0原文

我正在做一个副业项目,这是一项艰巨的任务;我的问题是关于使用布尔值来确定是否需要进一步的数据处理时获得的效率。

例如:如果我有一张列出所有生物的表格。在另一个本质上相关的表格中列出了他们的冬眠期,以及冬眠期间每天消耗的卡路里。

在 (Creatures) 表中设置“hibernates”BOOLEAN 值是否有效?

如果为 true,则转到“hibernation_creature_info_relations”表并找到具有该 ID 的生物并返回该信息。

这意味着对于所有“hibernates”值为 false 的生物,将阻止 SQL 搜索“hibernation_creature_info_relations”的大表。

或者,当使用 ID 时,检查“hibernation_creature_info_relations”表的过程如此之快,以至于必须根据 hibernation 的值设置为 true 或 false 来处理执行操作的参数,实际上会对性能产生更大的影响?

我希望这些信息足以帮助您理解我的要求,如果没有,请告诉我,以便我可以重新措辞或包含更多详细信息。

I am working on a side project that is quite an undertaking; my question regards the efficiency gained when using a BOOLEAN value to determine whether or not further data processing is required.

For example: If I had a table that listed all the creatures. In another table that was relational in nature listed their hibernation period, and calories consumed each day during hibernation.

Is it efficient to have inside the (Creatures) table a value for "hibernates" BOOLEAN.

If true then go to the "hibernation_creature_info_relations" table and find the creature with that ID and return that information.

This means that for all the creatures whose value for "hibernates" = false will prevent SQL from having to search through the large table of "hibernation_creature_info_relations."

Or when using ID's is the process so fast in checking the "hibernation_creature_info_relations" table so fast that there will actually be a larger impact on performance by having to process the argument of doing what based on if the value of hibernation is set to true or false?

I hope this was enough information to help you understand what I am asking, if not please let me know so I can rephrase or include more details.

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

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

发布评论

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

评论(2

蛮可爱 2024-12-19 01:41:53

不,这不是一个好方法。

使用可以为 null 的普通字段。

示例

table creatures
---------------
id     name      info_id

1      dino      null
2      dog       1
3      cat       2

table info
--------------
id     info_text

1      dogs bark
2      cats miauw

现在您可以只进行连接:

SELECT c.name, i.info_text
FROM creature c
LEFT JOIN info i ON (c.info_id = i.id)

如果您这样做,SQL 就可以使用索引。
没有 SQL 数据库会在布尔字段上创建索引。
该字段的基数太低,在低基数字段上使用索引会减慢速度而不是加快速度。

请参阅:MySQL:低基数/选择性列=如何索引?

No, that is not a good way to do things.

Use a normal field that can be null instead.

Example

table creatures
---------------
id     name      info_id

1      dino      null
2      dog       1
3      cat       2

table info
--------------
id     info_text

1      dogs bark
2      cats miauw

Now you can just do a join:

SELECT c.name, i.info_text
FROM creature c
LEFT JOIN info i ON (c.info_id = i.id)

If you do it like this, SQL can use an index.
No SQL database will create an index on a boolean field.
The cardinality of that field is too low and using indexes on low cardinality fields slows things down instead of speeding things up.

See: MySQL: low cardinality/selectivity columns = how to index?

转瞬即逝 2024-12-19 01:41:53

如果您想使用“hibernates”列only来防止 SQL 必须搜索其他表,那么您应该遵循@Johan,否则您可以在该表上创建索引列“休眠”它将缩短执行时间。 但请记住@Johan 想要告诉您的内容。

If you want to use the column "hibernates" only to prevent the SQL from having to search through the other table then you should follow @Johan otherwise you can create index on the column "hibernates" it will improve the execution time. But keep in mind what @Johan is trying to tell you.

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