通过 BOOLEAN 值优化 SQL (mySQL)
我正在做一个副业项目,这是一项艰巨的任务;我的问题是关于使用布尔值来确定是否需要进一步的数据处理时获得的效率。
例如:如果我有一张列出所有生物的表格。在另一个本质上相关的表格中列出了他们的冬眠期,以及冬眠期间每天消耗的卡路里。
在 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这不是一个好方法。
使用可以为
null
的普通字段。示例
现在您可以只进行连接:
如果您这样做,SQL 就可以使用索引。
没有 SQL 数据库会在布尔字段上创建索引。
该字段的基数太低,在低基数字段上使用索引会减慢速度而不是加快速度。
请参阅:MySQL:低基数/选择性列=如何索引?
No, that is not a good way to do things.
Use a normal field that can be
null
instead.Example
Now you can just do a join:
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?
如果您想使用“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.