在 sql 中哪个更快:搜索 table 或在 table 中搜索数据?

Which is faster in sql: searching for a table, or searching for data in table?

假设数据库中有 1,000 个 table,每个 table 有 1,000 行。当我从这1000个table中搜索单个table时,搜索时间是否与在其中一个table中搜索数据所需的时间相同?

换句话说,SQL 使用相同的搜索算法从 1,000 个 table 中查找 table 是否与从 table 中获取数据相同有 1,000 行?

不,MySQL 不使用相同的搜索算法来查找 table。

MySQL 维护内存中的“数据字典”,因此当您 运行 命名特定 table 的查询时,它会非常快速地查找 table . MySQL 识别 table 比在 table 中搜索数据要快得多。例如,我在工作中维护的数据库服务器有超过 150,000 个 table,这不是问题。

这是否意味着您应该将数据拆分成许多 table 以使其 运行 更快? -- 这通常不是一个好的权衡。它使您的应用程序代码更加复杂,因为您的代码需要选择要查询的 table。如果您必须在许多 table 中搜索结果,您可能还会发现您希望数据集中在一个 table 中的情况。

这里有几个要遵循的原则:

"Everything should be made as simple as possible, but not simpler." (attributed to Albert Einstein)

"First make it work, then make it right, and, finally, make it fast." (Stephen C. Johnson and Brian W. Kernighan, 1983)