gpt4 book ai didi

mysql - 增加WHERE子句的条件会提高搜索速度吗?

转载 作者:行者123 更新时间:2023-11-29 11:48:57 25 4
gpt4 key购买 nike

我有一个这样的表:

// mytable
+----+-------+---------+
| id | name | code |
+----+-------+---------+
| 1 | jack | 1 |
| 2 | acton | 1 |
| 3 | aiken | 1 |
| 4 | peter | null |
| 5 | ash | null |
| 6 | fabia | 2 |
| 7 | ubon | null |
| 8 | taavi | null |
| 9 | wade | 2 |
| 10 | ian | 1 |
+----+-------+---------+

我想选择id为4的行。这是我的查询:

select * from mytable where id = 4

我还知道该行的 code 值为 null。现在我想知道,如果我添加这个...并且code = null会提高搜索速度吗?

最佳答案

如果添加:

and code = null

那么您将不会返回任何行,因为 = null 始终返回 nullwhere 过滤掉值不为 true 的行。

相反,你会:

and code is null

据推测,id 上有一个索引(所有主键都有一个索引)。如果是这样,SQL 引擎可能会在获取该行后再进行一次比较,以确保 code 与条件匹配。考虑到获取数据的所有其他工作,这是非常非常可以忽略不计的。包含附加条件很可能不会对性能产生明显影响。

关于mysql - 增加WHERE子句的条件会提高搜索速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com